ASP.NET MVC表单post参数

时间:2011-10-06 10:12:39

标签: asp.net-mvc

我在MVC中有一个表单:

<% using (Html.BeginForm("Get", "Person"))
  { %>
      <%= Html.TextBox("person_id")%>
      <input type="submit" value="Get Person" />
  <% } %>

这会将我重定向到Person/Get。好的。问题是:

如何制作此表单,以便将其重定向到Person/Get/{person_id}

修改

<% using (Html.BeginForm("Get", "Person", new { id = ??? }))
  { %>
      <%= Html.TextBox("person_id")%>
      <input type="submit" value="Get Person" />
  <% } %>

我在???

写什么

2 个答案:

答案 0 :(得分:1)

我认为最困难的方法是使用javascript客户端。

更直接的方法是在操作Person/Get上检索它并从那里返回指向Person/Get/{person_id}

的RedirectResult
[HttpPost]
public ActionResult Get(string person_id)
{
    return RedirectToAction("Get", "Person", new { id = person_id });
}

[HttpGet]
public ActionResult Get(string id)
{
     //Do your thing
}

重定向通常很快,用户永远不会注意到。他/她将到达/ Person / Get / {person_id}

答案 1 :(得分:0)

您要做的是将路由值指定为BeginForm方法的第三个参数。

<% using (Html.BeginForm("Get", "Person", **new { person_id = this.Model}**))
{ %>
   <%= Html.TextBox("person_id")%>
   <input type="submit" value="Get Person" />
<% } %>

然后您的控制器操作看起来像这样

public ActionResult Get(int person_id)
{
    return View(person_id);            
}
相关问题