我在mvc 3 razor中有这个代码
@using (Html.BeginForm("MyAction", "MyController"))
{
<input type="text" id="txt" name="txt"/>
<input type="image" src="image.gif" alt="" />
}
控制器中的我有这个代码
[HttpPost]
public ActionResult MyAction(string text)
{
//TODO something with text and return value...
}
现在,如何发送一个新值,例如对于Action结果的例如?感谢
答案 0 :(得分:4)
您使用视图模型:
public class MyViewModel
{
public string Text { get; set; }
// some other properties that you want to work with in your view ...
}
然后将此视图模型传递给视图:
public ActionResult MyAction()
{
var model = new MyViewModel();
model.Text = "foo bar";
return View(model);
}
[HttpPost]
public ActionResult MyAction(MyViewModel model)
{
// remove the Text property from the ModelState if you intend
// to modify it in the POST controller action or HTML helpers will
// use the old value
ModelState.Remove("Text");
model.Text = "some new value";
return View(model);
}
然后视图强烈输入此模型:
@model MyViewModel
@using (Html.BeginForm("MyAction", "MyController"))
{
@Html.EditorFor(x => x.Text)
<input type="image" src="image.gif" alt="" />
}