asp.net mvc dropdownlist onchange routing - asp.net-mvc

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

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)) %>

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

MVC DropDownList SelectedItem Value in ActionLink

I'm a bit confused and sorry if this question is repeated elsewhere, I did check and it didnt seem to be here yet.
Is there a way, (without use of JavaScript) to get the currently selected item of a DropDownList and say send it off to an ActionLink?
<%= Html.DropDownList("Elements") %>
<%=Html.ActionLink("Add Authorization Element", "AddElement", new {elementGuid = ??? }) %>
The bit I am looking for is something to replace:
???
Thanks,
Ric
Not without JavaScript, no. Of course, it's trivial with JavaScript.
If you want to do both, add JavaScript to the drop down, then put a submit button inside a noscript tag. Users without JavaScript will have to click the button. Users with JavaScript won't see it.

Using HTML forms in ASP.NET MVC?

It seems like everything I look up on this subject has either changed since the release or is wildly different from eachother.
I just want to create a simple form in my view.
Should I be using the Html.BeginForm()/TextBox()/EndForm() methods or should I be using a plain-jane HTML form? Which is preferred?
This is what I have so far:
<%=Html.BeginForm("Create", "Product", FormMethod.Post); %>
<%=Html.TextBox("productTextBox", "Enter a shoe name"); %>
<input type="submit" name="createButton" value="Create Me!" />
<%=Html.EndForm(); %>
What is the "correct" way to create a simple form with a button and textbox in ASP.NET MVC and allow me to submit the data in the form to the /Product/Create action?
How do I then access the form data from within that method? Some people seem to use a "FormCollection" and others just do a Request.Form method. Which way should I use?
Can someone enlighten me?
The Form helpers are the recommended way because it allows you to provide a controller, action and other route data and the URL is auto-generated based on your routes (in Global.asax). The advantage is, if you decide to change your routes, you don't have to update every URL in your site.
The only reason I'd use an actual "<form>" tag was if I needed extra control over the markup that I couldn't get from Html.Form (I can't think of an example right now). Even if you choose to do that, you should use the "Url.Action" helper to get a URL from routing data. For example:
<form action="<%= Url.Action("Create") %>">
As for your second question, I'd suggest using the Model Binder. Check out ScottGu's Blog for some details on this.
Have a look at Link.
It's German text but the code should be understandable.
Have you looked at this:
http://weblogs.asp.net/scottgu/archive/2009/03/10/free-asp-net-mvc-ebook-tutorial.aspx
It's from the horse's mouth, and is up-to-date with the final release.

ActionLink fails with ViewData parameter in ASP.NET MVC Beta

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.

Resources