ActionLink fails with ViewData parameter in ASP.NET MVC Beta - asp.net-mvc

I have ran into an odd problem with the ActionLink method in ASP.NET MVC Beta. When using the Lambda overload from the MVC futures I cannot seem to specify a parameter pulled from ViewData.
When I try this:
<%= Html.ActionLink<PhotoController>(p => p.Upload(((string)ViewData["groupName"])), "upload new photo") %>
The HTML contains a link with an empty URL.
upload new photo
However if I hard code the parameter, like this:
<%= Html.ActionLink<PhotoController>(p => p.Upload("groupA"), "upload new photo") %>
The output contains an actual URL.
upload new photo
I assume this probably has something to do with the visibility and availability of the ViewData, and it not being there when the Lambda gets evaluated by the internals of the framework. But that is just a guess.
Am I doing something incorrect in the first sample to cause this, or is this some short of bug?
Update: I am using the latest version of the MVC futures. It has been pointed out that this works for some people. Since it doesn't work for me this makes me think that it is something specific to what I am doing. Does anybody have any suggestion for what to look at next, because this one really has me stumped.

Have you updated your version of the Microsoft.Web.Mvc.dll where the Strongly typed actionlink resides.
Apparently this dll has been updated for the Beta release. The function may have been slightly modified.
I just tried this
<%= Html.ActionLink<HomeController>(x=>x.Search((string)ViewData["search"]), "search?") %>
and it worked fine.

Ok, I figured out what my problem was.
Apparently I was not even setting the ViewData slot that I was trying to read from in the view, resulting in it being a null value.
So effectually I was writing:
<%= Html.ActionLink<PhotoController>(p => p.Upload(null), "upload new photo") %>
I think the ultimate kicker to this whole thing was the fact that the parameter (groupname) represents a non-defaultable value in my routing table.
routes.MapRoute(
"Group",
"group/{groupname}/{controller}/{action}/{id}",
new {controller = "Photos", action = "View", Id = ""});
So according to the routing rule the property groupname has to be present, but according go the Lambda gropname was omitted (null). This resulted in the MVC framework being unable to find a route that satisfied my query, and just returning null.

Related

How to generate a URL for ASP.NET MVC without string names

How to generate a URL for ASP.NET MVC without string names? I.e. I have Index action in HomeController. Using strings I would go for
#Url.Action("Index","Home")
but I would like to avoid use of string. I remember seeing a new way how to do this without strings, but I cannot find it anywhere now. The only code that I have found was
#(Url.Action<HomeController>(x=>x.Index()))
but that does not work for me. Error given is
The non-generic method 'System.Web.Mvc.UrlHelper.Action()' cannot be used with type arguments.
My ASP.NET MVC version is 5.2.3. If the above is only for 6+, is there any way to make achieve what I want?
You might want to check out T4MVC: https://github.com/T4MVC/T4MVC
You need a nuget package for that:https://www.nuget.org/packages/Microsoft.AspNet.Mvc.Futures/
this way you can do things like this:
<%= Html.ActionLink<MyController>(x => x.MyMethod(a), "text") %>
<%= Html.BuildUrlFromExpression<MyController>(x => x.MyMethod(a)) %>

ASP.NET MVC2 Html.Checkbox not checking when isChecked=true

I have several checkboxes on a form. Only one of them is checked when it needs to be; all the others are unchecked regardless of whether the isChecked parameter is passed in as either true or false.
The checkboxes are coded like this:
<%= Html.CheckBox("cbName",Model.checkvalue)%>
<%= Html.CheckBox("cbName1",Model.checkvalue1)%>
I have stepped through the code and Model.checkValue and Model.checkValue1 are both true, but cbName is not checked and cbName1 is checked (in fact, in my actual app' there are several more CheckBoxes and none are checked -except the second one in the form- although the Model properties are all true in the test I ran).
Has anyone come across this (mis)behavior before & can you let me know where I am going wrong, please? I can't find a similar question anywhere, so I am hoping I am just making a simple error that will be quick to fix...
what about use different way of rendering the code ( of course you can later simplify this code or create your own helper):
<%
if(Model.checkvalue1){
%>
<%= Html.CheckBox("name", new {checked =checked }) %>
<%}else{%>
<%= Html.CheckBox("name", null) %>
<%}%>
idea 2
make sure that the value you are passing in is boolean: therefore cast is as boolean
<%= Html.CheckBox("cbName1",(bool)Model.checkvalue1)%>
idea 3
before using the code
<% bool myTempValue = Model.checkvalue1; %>
<%= Html.CheckBox("cbName1",myTempValue)%>
There could be some reasons for this behavior:
Is there any query string parameter named cbName in URL of the page? Or, is it a POST request of the form? Query string and form (POST) data take precedence over explicit values set in code.
What browser are you using? FireFox sometimes preserves form data over page reloads. If you check a checkbox and refresh the page, FireFox checks the checkbox again, even when there is no "checked" attribute in HTML input element.
It was because the underlying data uses a nullable boolean.
I switched the CheckBox to a CheckBoxFor and got the error outlined in this question and this told me that the problem is the fact that the underlying data currently uses a nullable boolean. Since this data type will be switched for a not null boolean once ready I don't need to work around this.
I hope this answer helps someone else.
Thank you for everyone's contributions.

ASP.NET MVC Appends ' ?RouteValueDictionary ' to my routes

I'm doing some changes on my routes, and suddenly the following is appearing in my url's as a querystring:
?RouteValueDictionary=System.Web.Routing.RouteValueDictionary
So, my url's now look like
http://localhost:20367/Search/AdvancedSearchResults?RouteValueDictionary=System.Web.Routing.RouteValueDictionary
How do I make it disappear?
I know the routing puts it in there, because it cannot find route values for the viewmodel that's passed along, but I can't seem to fix it...
I suspect your call to Html.ActionLink is picking the wrong overload. You might need to add an extra parameter at the end to force .NET to pick the right overload:
Html.ActionLink("click here, "SomeAction", "SomeController", routeValues, null)

Form in ASP.NET MVC (1.0) does not fire if "id" attribute is present (jQuery Validation requirement)

<% Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "form_logon" }); %>
or
<form id = "form_logon", action="/Home/Index" method="post">
just don't work.
Pressing a submit button if ID is present does nothing, while id is required for jQuery validation plugin.
Are you sure you don't have any javascript errors on your page? I use the first format pretty extensively with no problems, with and without the Validation plugin. Note that your second example has an extra comma, but I'm guessing that's a transcription error. I'd look to make sure that you don't have a javascript error that is halting all javascript on the page (though that wouldn't explain a plain, old submit button not working).
I got similar issue and couldn't get any proper solution to this. However, since I only had one form in my MVC page, I reverted back to Html.BeginForm() without any parameters, and in the jQuery code, I just used the following:
$("form").validate(/* my rules and messages */);
instead of:
$("#userForm").validate(/* my rules and messages */);
Hope this helps.
Regards
Naweed

asp.net mvc dropdownlist onchange routing

I have a problem with transfer of my project to iis server. At the asp.net development server all routing went smoothly. Now i want to migrate to iis server and my onchange attribute on select tag doesnt work. When i try to get from https://www.web.com/Manager/Authorized/Accounts/0 using the
onchange="location.href='<%= Manager.MyConfiguration.Prefix %>/Authorized/Authorized/Accounts/'+this.value"
at the obvious page, i end up at https://www.web.com/Manager/Authorized/Accounts/0/Manager/Authorized/Authorized/Accounts/13424
The same problem was with every href attribute and the Html.Actionlink pretty much solved most of it, but i still dont know, how to route onchange or onclick.
<%= Manager.MyConfiguration.Prefix %> was my idea how to add the /Manager/ part of the route there (in order not to use it on the local devserver). Without this part, i get just https://www.web.com/Authorized/Accounts/13424 which is also wrong.
?:
<%= Url.Content("~/Authorized/Authorized/Accounts/") %>
I'm not quite sure if I fully understand the problem, but it seems to be you could use the VirtualPathUtility class and do something like
'<%=VirtualPathUtiliy.ToAbsolute("~/Authorized/Authorized/Accounts/")%>'
+ this.value

Resources