0
我是初学者,我认为我的问题非常明显,但我陷入了困境!如何将自定义模型绑定到TextBoxFor
在MVC中,并试图将不是我视图的主模型的自定义模型(类)传递给我的控制器。 我的剃刀代码如下:
@helper CreatForm()
{
MyViewModel myModel = new MyViewModel();
using (Html.BeginUmbracoForm("Post", "ShowPost", FormMethod.Post, new { @class = "formCM" }))
{
myModel.PostNode = Node.GetCurrent();
<div class="row">
@Html.TextBoxFor(x => myModel.Name, new Dictionary<string, object> { { "placeholder", "Name" } })
</div>
<div class="row">
@Html.TextBoxFor(x => myModel.Email, new Dictionary<string, object> { { "placeholder", "Email Address" } })
</div>
<div class="row">
@Html.TextBoxFor(x => myModel.Website, new Dictionary<string, object> { { "placeholder", "Website" } })
</div>
<div class="row tall">
@Html.TextAreaFor(x => myModel.Comment, new Dictionary<string, object> { { "placeholder", "Comment" } })
</div>
<div class="row">
<input type="submit" id="Submit" name="Submit" value="Submit" />
</div>
}
}
点击提交按钮会带我到控制器,但是模型总是空空的。
我控制器是象下面这样:
[HttpPost]
public ActionResult Post(MyViewModel model)
{
return this.View();
}
有什么建议?我必须将此MyModel属性添加到当前页面的ViewData吗?
感谢您的回复。我用接近你的建议的方法排列了这个问题。 –