如何通过POST发送textBox值
问题描述:
我正在使用Html.BeginForm并尝试将提供的textBox“archName”的值传递给帖子,我该怎么做? 我的意思是我应该添加什么而不是“someString”?如何通过POST发送textBox值
<% using (Html.BeginForm("addArchive", "Explorer", new { name = "someString" }, FormMethod.Post)) { %>
<%= Html.TextBox("archName")%>
答
您所指的名称是表单HTML元素的名称属性,未发布值。在你的控制器上你可以使用几种方法。
随着控制器的方法没有参数:
[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();
}