ASP.NET MVC Preview 4 - Stop Url.RouteUrl (), etc. Using existing parameters

I have an action like this:

public class News : System.Web.Mvc.Controller
{
    public ActionResult Archive(int year)
    {
       / *** /
    }
}

With this route:

routes.MapRoute(
           "News-Archive",                                              
           "News.mvc/Archive/{year}",                           
           new { controller = "News", action = "Archive" }
       );

URL I am in:

News.mvc/Archive/2008

I have a form on this page:

<form>
    <select name="year">
        <option value="2007">2007</option>
    </select>
</form>

Form submission should go to News.mvc / Archive / 2007 if "2007" is selected on the form.

This requires the "action" attribute of the "News.mvc / Archive" form.

However, if I declare a form like this:

<form method="get" action="<%=Url.RouteUrl("News-Archive")%>">

It is displayed as:

<form method="get" action="/News.mvc/Archive/2008">

Can someone please let me know what I am missing?

+5
source share
3 answers

You have a couple of problems, I think.

-, "", URL- "/News.mvc/Archive" .

-, , , , HTML. , URL- "? = 2007". , GET HTML.

, - , .

  • , - , URL- , Javascript ( submit URL-).
  • /News.mvc/Archive?year=2007 URL-, {year} . "int year" , .
+2

, , - {year}, ..

- ?

0

, , ( , ).

1) :

routes.MapRoute(
       "News-Archive",                                              
       "News.mvc/Archive/{year}",                           
       new { controller = "News", action = "Archive", year = 0 }
   );

2) GET, URL.

public ActionResult Archive(int year)
{
   if (!String.IsNullOrEmpty(Request["year"]))
   {
       return RedirectToAction("Archive", new { year = Request["year"] });
   }
}

3) , " ". .

public ActionResult Archive(int year)
{
   if (!String.IsNullOrEmpty(Request["year"]))
   {
       return RedirectToAction("Archive", new { year = Request["year"] });
   }
   if (year == 0)
   {
       /* ... */
   }
   /* ... */
}

3) Url.RouteUrl():

Url.RouteUrl("News-Archive", new { year = 0 })
0
source

All Articles