自定义视图模型在TextBoxFor TextBoxFor不显示值(型号=> Model.Object1.Name) [英] Custom ViewModel not displaying values in TextBoxFor TextBoxFor(Model => Model.Object1.Name)

查看:111
本文介绍了自定义视图模型在TextBoxFor TextBoxFor不显示值(型号=> Model.Object1.Name)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义模式,其中包含另一个自定义对象(Objects1.Object2),我正确地填充在视图中显示之前的目标以及

I have a custom Model, which contains another custom object (Objects1.Object2), I am correctly populating the object prior to display in the view and

<%:Model.Object1.Name%>显示数据尚未正确
<%:Html.TextBoxFor(型号=> model.Object1.Name)%>显示的数据。

<%: Model.Object1.Name %> displays the data correctly yet <%: Html.TextBoxFor(model => model.Object1.Name) %> displays no data.

我是新来的MVC,很想来解决这个问题,因为它是一个停车点,以创建自定义数据模型。

I am new to MVC and would love to get around this issue as it is a stopping point to creating custom data model.

任何信息是很大的AP preciated。

Any info is greatly appreciated.

推荐答案

您想在一个POST操作来修改呢?如果你再注意,HTML助手,如 TextBoxFor 会先读取模型的状态,并在此之后,从模型数据。所以,如果你的帖子动作看起来是这样的:

Are you trying to modify this in a POST action? If you are then note that HTML helpers such as TextBoxFor will first read data from model state and after this from the model. So if your post action looks like this:

[HttpPost]
public ActionResult Index(SomeViewModel model)
{
    model.Object1.Name = "some new value";
    return View(model);
}

您需要从模型状态中删除它,或者你总是会得到旧值:

you need to remove it from model state or you will always get old value:

[HttpPost]
public ActionResult Index(SomeViewModel model)
{
    ModelState.Remove("Object1.Name");
    model.Object1.Name = "some new value";
    return View(model);
}

如果你在GET行动做这个不应该有绝对的显示值的任何问题:

If you are doing this in the GET action there shouldn't be absolutely any problems displaying the value:

public ActionResult Index()
{
    var model = new SomeViewModel
    {
        Object1 = new TypeOfObject1
        {
            Name = "foo bar"
        }
    };
    return View(model);
}

,然后在视图:

<%= Html.TextBoxFor(x => x.Object1.Name) %>

应该显示正确的值。

should display the proper value.

这篇关于自定义视图模型在TextBoxFor TextBoxFor不显示值(型号=&GT; Model.Object1.Name)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆