asp.net mvc 3 - MVC routing problem -


I have a search form on my page and I want to submit the form using GET. The search form looks like this:

  @using (Html.BeginForm ("list", "search", FormMethod.Get)) {@ Html.HiddenFor (model => model. CategoryName) @ Html.HiddenFor (model => model.RegionName) & lt; Table class = "plain" & gt; & Lt; TR & gt; & Lt; TD & gt; & Lt; / TD & gt; & Lt; Td> @ Html.TextBoxFor (model => model.FromDate) & lt; / Td> & Lt; / TR & gt; & Lt; TR & gt; & Lt; TD & gt; & Lt; / TD & gt; & Lt; Td> @ Html.TextBoxFor (model => Model ToDate) & lt; / Td> & Lt; / TR & gt; & Lt; TR & gt; & Lt; Td colspan = "2" & gt; & Lt; Input type = "submit" id = "search" value = "search" /> & Lt; / TD & gt; & Lt; / TR & gt; & Lt; / Table & gt; }   

With a visual model like this:

  Public category SideNavModel {public string categoryName {get; Set; } The name of the public string field {get; Set; } Public string FromDate {get; Set; } Public string todate {get; Set; }}   

I have established routes like this:

  route. MapRoute ("Search2", "Search / {class name} / {RegionName} / {FromDate} / {toDate}", new {controller = "search", action = "list", category name = "all", area name = "all ", From-date =" ", ToDate =" "}}); ("Search", "search / {class name} / {domain name}", new {controller = "search", action = "list", category name = "all", field name = "all"});   

Therefore, when I actually search, the URL is sent to:

  / search? Category name = all and area names = some rules and dates from = 20110301 & amp; ; ToDate = 20110317   

Where exactly I want to: / search / all / some rules / 20110301/20110317

I am not sure that this is important, but the way it has been installed, the search must be seen on every page. Therefore, in the _layout.cshtml 'masterpage', I have this:

  @ {Html.RenderAction ("LoadNavigationSide")}}   

and in Base controller, this:

  Return the partial view ("Navigation side", model) to public partial voyeur loadenance side (sidewindow model) {// other code //; }   

(It should be like this because it's just a bit more than just returning the model - so it can not be a simple @teather.bird call ("Navigation Side")

If you use method = "GET" on your form Then the HTML specification states that the user agent should send a GET request for the URL used in the verb attribute and the query string argument Value must be added in the form.

If you use method = "POST" the HTML specification on your form indicates that a POST request to the user agent for the action attribute Must send and write the value of the form field in the POST request body. Conclusion: The one you are trying to achieve is impossible with plain HTML.

One way to get this will be to use Javascript and manually create this form after the formation of the onsubmit request URL and cancel the default behavior. Example with jQuery:

  $ ('form'). Submit (function () {var url = this.action + '/' + $ ('# fromDate'). Val () + '/' + $ ('# toDate'). Val (); window.location Href = url; return false;});   

But personally I would like to be with a post verb.

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

java - Why my included JSP file won't get processed correctly? -