Hi I'm pretty new with MVC2 or MVC in general. If it's one thing I get caught up with it's routes. Like now I got this scenario. Im going from the regular site to Admin. My navigation is the same partialview on both I just do a check which data to render something like this.
<% if (!Request.RawUrl.Contains("Admin")){%>
<% foreach (var site in Model) { %>
<%= Html.MenuItem(site.BelongSite, "Sida", "Site", site.BelongSite) %> |
<%} %>
<%} else {%>
<%= Html.ActionLink("Konfig", "Konfigurera", "Admin") %>
<% } %>
My route looks like this
routes.MapRoute(
"Admin", // Route name
"Admin/{action}/{name}", // URL with parameters
new { controller = "Admin", action = "konfigurera", name = UrlParameter.Optional } // Parameter defaults
);
On my View called Konfigurera I got Edit sites and they use the route above and it works great. The navigation tho dont get no action assigned to it. It's just <a href='Admin/'>
The navigation is in the shared folder, and it is a strongly typed. Any Ideas? I been struggling with this for about a hour now
Thanks for any input
EDIT
this is just one of those omg I'm so stupid moments. Ofcause the the link gone be only <a href='Admin/' because it's the default link (as the default values of the route). I noticed this as I put more links to the Admin navigation and they was ok. Well hope this can help anybody in the same situation as I had.
I wonder how I should do if I come up with the solution myself?
Ofcause the the link gone be only <a href='Admin/' because it's the default link (as the default values of the route). So really no problem here Index always gone result in a "empty" link.
Related
I'm developing a job board website with ASP.Net MVC and want to block any email or website addresses from being visible to unregistered users.
So for example if an employer posts a job and includes something in the description like 'To apply send an email to myemail#email.com', I want it to display 'To apply send an email to hidden link - please login or register' or words to that effect. When they login the links then become available.
I'm still very much at the learner stage with ASP.Net MVC so I'm not really sure where to start with this. I'm not sure if this is the right thing to do or if there is an alternative approach.
Many thanks for any help.
only777,
you may be best served by creating an DisplayTemplate for email addresses and running your code thro those. here's an example of what i mean. Inside your view:
<%: Html.DisplayFor( m => m.Email, "ViewIfAuthenticatedTemplate" ) %>
and then under views/shared/DisplayTemplates, add an item called ViewIfAuthenticatedTemplate.ascx and add the following code to it:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<string>" %>
<% if(Request.IsAuthenticated) { %>
<%: Model %>
<% } else {%>
<%: Html.ActionLink("hidden link - please login or register", "MyAction", "MyController") %>
<% } %>
not sure if the syntax is 100% correct, but you get the gist..
[edit] - in fact, you could use this DisplayTemplate for ANY text that had to be hidden if not authenticated.
Use a regex search to look for email addresses in the posting and replace that with the link you want for the unregistered users.
[Updated]
public ActionResult ShowPosting()
{
var description = descriptionFromDB();
if(!registeredUser)
{
model.Description = logicToReplaceTheEmailAddress(description);
}
else
{
model.Description= description;
}
return View(model);
}
Hoping someone can help me solve this issue.
I'm using Ajax.BeginForm quite often when I need to update a part of my actual webpage. But if I have a second button in the web form where I need go to a complete different page, and for example do a search and get some values that I need to complete the form. The page turns up in the update panel (updateTargetID) instead of in a complete new View. That is happening even id a do a RedirectToAction in the controller. Of course the ajax.beginform does what I accept it to do, in other words update the panel that shall be updated. But is there a way to get ajax.Beginform to use redirectToaction without updating when the user is choosing to do for example a search instead of sending the form?
I'm working with asp.net MVC,
Problem;
<% using (Ajax.BeginForm(new AjaxOptions() { LoadingElementId = "loadingElement", UpdateTargetId = "SearchResult", InsertionMode = InsertionMode.Replace}))
{%>
<% Html.RenderPartial("Searchfields", Model); %>
<div>
<%:Html.ActionLink("blank", "Index")%>
</div>
<div id="loadingElement" style="display: none">
Load data...
</div>
<div id="SearchResult">
<% if (Model.SearchResult != null)
{
Html.RenderPartial("SearchResult", Model.SearchResult);
} %>
</div>
<% } %>
In the controller (post) I do this among other stuff;
if (Request.IsAjaxRequest())
{
return PartialView("SearchResult", data.SearchResult);
}
But before this I need to do:
if (data.SearchResult.Count() == 1)
{
return RedirectToAction("Edit", "Person", new { Id = data.SearchResult.First).Id});
}
but ofcourse if I do that the hole page i rendered in the corresponding updateTargetId,
I need to render a new view instead. So how can I for exampel use javascript to redirect oncomplete and have the values from the model sent to the script to for exampel do a window.location.replace?
I'm struggling with javascript, so please help me!
First, I use Asp.Net MVC 2 RC 2.
What I want to do is to list a comment view and below this view being able to add comment (with validations). For example, something like when you add comment in stackoverflow. Except that my page should work with or without javascript enabled.
So to solve that problem I use the new RenderAction and it partially solved my problem. I got my list view that calls my addcomment usercontrol with RenderAction.
The validations work. My problem occurs when I try to add a comment that it's valid. The page didn't refresh correctly. If I got in the database, my comment is added, but it's doesn't have been refresh in my list view and the add comment form isn't clear.
I think it's because of how the workflow is rendered.
Maybe if someone got a example or blog about this, it's can help me to get it right...
At the bottom of my Comment/List.aspx
<% Html.RenderAction("Create", "Comment"); %>
In Comment/Create.ascx
<% using (Html.BeginForm(
ViewContext.ParentActionViewContext.RouteData
.Values["action"].ToString(),
ViewContext.ParentActionViewContext.RouteData
.Values["controller"].ToString(),
FormMethod.Post, new { id = "createForm" })){ %>
You can force the parent View to refresh itself with a small hack involving ViewContext.ParentActionViewContext.
In your CommentController class:
public ActionResult Create(Comment comment)
{
...
if (isValid) // Comment entered in form is valid
{
ControllerContext.ParentActionViewContext.ViewData["SuccessfullCreate"] = true;
}
...
}
And in your Comment/List.aspx page (view) :
<% Html.RenderAction("Create", "Comment"); %>
<%
if (ViewContext.ViewData["SuccessfulCreate"] != null)
{
string action = ViewContext.RouteData.Values["action"].ToString();
string controller = ViewContext.RouteData.Values["controller"].ToString();
string url = "/" + controller + "/" + action;
Response.Redirect(url);
}
%>
So basically, what's happening is that the child action is "telling" the parent action to refresh itself by using the parent's ViewData.
It's kind of a hack, but it works fine for what's you're doing.
I have a UserControl that I call with Html.RenderAction(...), so far so good..
Then I want to specify in the user control, which action should be used
Html.BeginForm("DeleteComment", "Comments", new { Id = "frmDelete" }, FormMethod.Post);%>
<%= Html.SubmitImage( "imgbtnDelete", "/image.png", new { ... })%>
<% Html.EndForm(); %>
And therein lies my problem; this calls the user control's controller/action.
What I want to happen is to call the pages action first and then be able to specify what action to call on the user control's controller.
Is this possible??
Thanks from an MVC noob
A simple solution would be to set the action of the form to be /{sub-controller}/{action-method}
Something like
Html.BeginForm("DeleteComment", "Comments", new { Id = "frmDelete", action="/{sub-controller}/{action-method}" }, FormMethod.Post);%>
HTH,
Dan
Let say I'm on the page "Home/Index" and I want to go to the page MyOtherController/Index/1
How can I do this ?
I try :
<%= Html.ActionLink("Test", "Index", "MyOtherController", new { id=item.Id }) %>
Did I also have to add a route in Global.aspx file ?
One option is to specify the name of the controller in the list of routevalues:
<%= Html.ActionLink("Test", "Index"
, new { controller = "MyOtherController", id = item.Id }) %>
An alternative is to use the overload of ActionLink with htmlAttributes = null:
<%= Html.ActionLink("Test", "Index"
, "MyOtherController", new { id = item.Id }, null) %>
The default route in the ASP.NET MVC template takes care of the routing in this case.
I don't believe ActionLink has an overload matching that particular signature. You would need to add "null" after your route values to find a matching one (for htmlAttributes). Ole's solution would be cleaner though so it's really a matter of preference. It also will help with readability so you don't have to guess whether each parameter is link text, an action/controller, etc.