我看到了成千上万的例子使用剃须刀BeginForm,我知道它自动绑定控制器输入到提交的信息.无论如何,我想知道在正常情况下是否存在相同的自动绑定。
<form method="get" action="@Url.Action("Index","Home")"> <input type="text" name="foo"> </form>如果它不..。我怎么能把它绑起来..。请不要告诉我使用html助手..。
发布于 2017-05-21 15:10:41
是的,您可以使用普通html进行bind model,但是名称attribute的值必须与model的property的名称相同。
public class viewmodel
{
public string FirstName {get; set;}
public string LastName {get; set;}
}所以你的html
@model viewmodel
<form method="get" action="@Url.Action("Index","Home")">
<input type="text" name="FirstName" value="@model.FirstName"> //name should be of same name as property name
<input type="text" name="LastName" value="@model.LastName">
<input type='submit' value='Submit'/>
</form>最后,您的action将是
public ActionResult Index(viewmodel model)
{
return View(model);
}https://stackoverflow.com/questions/44097759
复制相似问题