如何通过POST发送textBox值

时间:2011-04-29 20:12:12

标签: asp.net-mvc html-post

我正在使用Html.BeginForm并尝试将提供的textBox“archName”值传递给帖子,我该怎么做? 我的意思是我应该添加什么而不是“someString”?

<% using (Html.BeginForm("addArchive", "Explorer", new { name = "someString" }, FormMethod.Post)) { %> 
    <%=  Html.TextBox("archName")%>

1 个答案:

答案 0 :(得分:1)

您要引用的名称是表单HTML元素的name属性,而不是已发布的值。在您的控制器上,您可以通过几种方式访问​​。

控制器方法中没有参数:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult addArchive()
{
    string archName = HttpContext.Reqest.Form["archName"]
    return View();
}

在控制器方法中使用FormCollection作为参数:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult addArchive(FormCollection form)
{
    string archName = form["archName"];
    return View();
}

使用一些模型绑定:

//POCO
class Archive
{
    public string archName { get; set; }
}

//View
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<Namespace.Archive>" %>    
<%= Html.TextBoxFor(m => m.archName) %>

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult addArchive(Archive arch)
{
    string archName = arch.archName ;
    return View();
}
相关问题