Asp.Net MVC 4表单字段作为URL ID

时间:2014-07-20 22:57:33

标签: c# asp.net-mvc asp.net-mvc-4

我在asp.net mvc 4上有一个搜索表单(方法:GET)。

有我的表格:

@using (Html.BeginForm("Questions", "Article", FormMethod.Get))
            {
                <input type="hidden" name="order" value="Desc" />
                <input type="text" name="id" id="id" />
            }

我需要传递输入值(#id)作为路由ID。

我的路线是:

            routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

我的行动:

public ActionResult Questions(string id)
    {
        ...
    }

假装我的控制器被称为 Stackoverflow ,我的行动问题我需要的网址是:

localhost:port / Stackoverflow / Questions /(#id [input-value])

本地主机:端口/#2 /问题/ MVC

1 个答案:

答案 0 :(得分:0)

因为您没有为表单使用Model,所以您需要在get操作中从HttpContext中读取ID。

[HttpGet]
public ActionResult Questions(string id)
{
    string _id = Request.Form["id"];
}

您可能希望在此时使用您的ID重定向。

return RedirectToAction("DoSomethingWithId", _id);