这个问题我以前玩过,你可以通过Ajax.BeginForm来解决它。
举个例子:
我有一个模型Person,它是名称的一个字符串属性。
查看
@model Application.Models.Person
<fieldset>
<legend>Form</legend>
@using (Ajax.BeginForm("SendUp", "Home", new AjaxOptions
{
HttpMethod = "POST",
OnComplete = "window.location.href = 'Index'"
}))
{
<p>
@Html.LabelFor(model => model.Name)
@Html.EditorFor(model => model.Name)
</p>
<p>
@Html.TextArea("ta")
</p>
<p>
<input type="submit" value="Submit" />
</p>
}
</fieldset>
控制器
[HttpPost]
public ActionResult SendUp(string Name, string ta)
{
string s = ta;
// Process stuff here
// Go to another action or whatever
return RedirectToAction("Index");
}
使用Ajax.BeginForm() 允许您将数据发送到控制器即使它没有绑定到页面上的模型。您需要确保 Html 元素的 name 属性与控制器所需的参数名称相同。
所以如果你有
的控制器方法
public ActionResult SendUp(string Name, string ta)
您需要在Ajax.BeginForm() 内有一个名为Name 和ta 的Html 元素。
为此,您可以写出整个元素:
<input type="text" id="Name" name="Name" />
或者您可以使用@Html 助手。
@Html.Editor("Name")
当您为 @Html 帮助程序提供名称时,它将将该值设置为 id 属性和 name 属性。