ViewModel 没有使用 HttpPost Create 方法在模型中设置属性

发布于 2024-11-11 18:16:49 字数 547 浏览 0 评论 0原文

我有一个带有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愁杀 2024-11-18 18:16:49

这可能是因为在您的编辑表单中没有相应的值。因此,如果您的视图强类型化为 LogViewModel,则表单输入名称必须适当命名:

@model LogViewModel
@using (Html.BeginForm())
{
    <div>
        @Html.LabelFor(x => x.Log.Name)
        @Html.EditorFor(x => x.Log.Name)
    </div>

    <div>
        @Html.LabelFor(x => x.Log.SomeOtherProperty)
        @Html.EditorFor(x => x.Log.SomeOtherProperty)
    </div>

    ...

    <input type="submit" value="OK" />
}

这样,当提交表单时,POSTed 值如下所示:

Log.Name=foo&Log.SomeOtherProperty=bar

现在默认模型绑定器将能够成功绑定你的视图模型。还要确保您尝试分配的属性是公共的并且具有设置器。

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:

@model LogViewModel
@using (Html.BeginForm())
{
    <div>
        @Html.LabelFor(x => x.Log.Name)
        @Html.EditorFor(x => x.Log.Name)
    </div>

    <div>
        @Html.LabelFor(x => x.Log.SomeOtherProperty)
        @Html.EditorFor(x => x.Log.SomeOtherProperty)
    </div>

    ...

    <input type="submit" value="OK" />
}

sop that when the form is submitted the POSTed values look like this:

Log.Name=foo&Log.SomeOtherProperty=bar

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.

神经大条 2024-11-18 18:16:49

控制器方法应该有一个名为 model 的属性

[HttpPost]
public ActionResult Create(LogViewModel **model**){
  **model**.Log.Name == "MyTestvalue"; //true    }

The controller method should have a property named model

[HttpPost]
public ActionResult Create(LogViewModel **model**){
  **model**.Log.Name == "MyTestvalue"; //true    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文