在MVC2中路由。不显示属性 [英] Routing in MVC2. Attributes are not displayed

查看:60
本文介绍了在MVC2中路由。不显示属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好时光!在输入URL浏览器中显示路径时出现问题。到网站上的搜索页面。搜索本身工作正常 - 传递密钥,显示找到的列表。控制器中的搜索方法采用类型为字符串的参数进行搜索:





  public  ActionResult SearchAllByKey( string  key)

{

// logic

return 查看(< list_of_found>);

}







在Global.asax规定的路线中:





 routes.MapRoute(

搜索

搜索/ {键}

new {controller = controller_name,action = SearchAllByKey,key = UrlParameter.Optional}

);







从视图发送编辑值到方法的表单:





 <%使用(Html.BeginForm(  SearchAl lByKey  controller_name,FormMethod.Post, new  {enctype =   multipart / form-data}))

{%>

<%:Html.ValidationSummary( true %>

< input type = text id = keyValue name = key />

< input type = submit value = Go! />

<%} %>







当你点击Go!时。到一个搜索结果页面,但URL(输入行浏览器)显示:





http:// localhost:港口/搜索





而不是:





http:// localhost:PORT / Search / SOME_KEY





如何确保在URL-e中可见key?在此先感谢

解决方案

如果FormMethod.Post和方法之前放置[HttpGet],URL看起来:



localhost:PORT / Search?key = nature



但应该是:



localhost:PORT / Search / nature


解决方案:

In查看:
< pre lang = c# > <% 使用(Html.BeginForm ( 搜索 Home,FormMethod.Post))
{%>
<% Html.ValidationSummary( true ); %>
< input type = text id = key name = key < span class =code-attribute> value = / >
< 输入 类型 = 提交 value = Go! / >
<%} %>





向控制器添加方法:



<前lang =c#> [HttpPost]
public ActionResult搜索(FormCollection表单)
{
return RedirectToAction( SearchAllByKey new {key = form [ key]});
}





谢谢LeftyX。


Good time! There was a problem displaying the route in the input URL browser. To the search page on the site. The search itself is working fine - the "key" is passed, the list of found displayed. Search method in the controller takes an argument of type string for which to search:


public ActionResult SearchAllByKey(string key)

            {

                //logic

                return View(<list_of_found>);

            } 




In Global.asax prescribed route:


routes.MapRoute(

                      "Search",

                      "Search/{key}",

                      new { controller = "controller_name", action = "SearchAllByKey", key = UrlParameter.Optional }

                  );




Form which sends the value of Edit to method from View:


<% using (Html.BeginForm("SearchAllByKey", "controller_name", FormMethod.Post, new { enctype = "multipart/form-data" }))

                               {%>

                            <%: Html.ValidationSummary(true) %>

                            <input type="text" id="keyValue" name="key" />

                            <input type="submit" value="Go!" />

                            <% } %>




When you click on "Go!". to a page of search results, but the URL (input line browser) shows:


http://localhost:PORT/Search


instead of:


http://localhost:PORT/Search/SOME_KEY


How to make sure that was visible "key" in the URL-e? Thanks in advance

解决方案

If the FormMethod.Post and before the method put [HttpGet], the URL looks:

localhost:PORT/Search?key=nature

but should be:

localhost:PORT/Search/nature


Solution:

In View:
<pre lang="c#"><% using (Html.BeginForm("Search", "Home", FormMethod.Post))
{%>
    <% Html.ValidationSummary(true); %>
    <input type="text" id="key" name="key" value="" />
    <input type="submit" value="Go!" />
<% } %>



Add method to controller:

[HttpPost]
public ActionResult Search(FormCollection form)
{
    return RedirectToAction("SearchAllByKey",  new { key = form["key"] });
}



Thanks LeftyX.


这篇关于在MVC2中路由。不显示属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆