ViewModel 没有使用 HttpPost Create 方法在模型中设置属性
我有一个带有 EF4 模型
Log
.Name
.CreatedDate
.LogTypeId
LogTypes
.Id
.Description
和 ViewModel 的
LogViewModel
Log MyLog
List<SelectListItem> Options
LogViewModel(){
Log = new Log();
}
简单 MVC3 应用程序,它可以正确显示在我的视图中,我可以编辑/更新值、显示下拉列表并将名称设置为“MyTestValue”。
但是,在我的控制器的 HttpPost Create 方法中,未设置 logVm.Log 的属性?
[HttpPost]
public ActionResult Create(LogViewModel logVm){
logVm.Log.Name == "MyTestvalue"; //false - in fact its null
}
我做错了什么?
I have a simple MVC3 app with an EF4 Model
Log
.Name
.CreatedDate
.LogTypeId
LogTypes
.Id
.Description
and a ViewModel
LogViewModel
Log MyLog
List<SelectListItem> Options
LogViewModel(){
Log = new Log();
}
This displays in my view correctly and I can edit/update the values, display the drop down list and set the name to "MyTestValue".
However, in my controller's HttpPost Create method the properties for logVm.Log are not set?
[HttpPost]
public ActionResult Create(LogViewModel logVm){
logVm.Log.Name == "MyTestvalue"; //false - in fact its null
}
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能是因为在您的编辑表单中没有相应的值。因此,如果您的视图强类型化为
LogViewModel
,则表单输入名称必须适当命名:这样,当提交表单时,POSTed 值如下所示:
现在默认模型绑定器将能够成功绑定你的视图模型。还要确保您尝试分配的属性是公共的并且具有设置器。
That's probably because in your edit form you don't have corresponding values. So if yuor view is strongly typed to
LogViewModel
the form input names must be appropriately named:sop that when the form is submitted the POSTed values look like this:
Now the default model binder will be able to successfully bind your view model. Also make sure that the properties you are trying to assign are public and that have a setter.
控制器方法应该有一个名为 model 的属性
The controller method should have a property named model