简体   繁体   English

值未显示在视图中

[英]Value doesn't appear filled in the View

I have a strange problem here. 我这里有一个奇怪的问题。

In the Model (.cs): 在模型(.cs)中:

    public long? ContactoAdditionalId { get; set; }
    public long Test1 { get; set; }
    public long? Test2 { get; set; }
    public string Test3 { get; set; }

In the Controller (at the end of the action just before the view): 在Controller中(在操作之前,在视图之前):

    [HttpPost]
    [AllowAnonymous]
    public ActionResult Index( Contacto model )
    {
        DoSomeStuff( model, ... );

        (...)

        model.Test1 = model.ContactoAdditionalId.HasValue ? model.ContactoAdditionalId.Value : -1;
        model.Test2 = model.ContactoAdditionalId;
        model.Test3 = model.ContactoAdditionalId.HasValue ? model.ContactoAdditionalId.Value.ToString() : "No value!";
        return View( model );
    }

Finally in the View (.cshtml): 最后在视图(.cshtml)中:

@using MyApp.Models;

@model Contacto

@using ( Html.BeginForm( "Index", "Contacto" ) )
{
    @Html.HiddenFor( model => model.ContactoAdditionalId )
    @Html.HiddenFor( model => model.Test1 )
    @Html.HiddenFor( model => model.Test2 )
    @Html.HiddenFor( model => model.Test3 )
    (...)

As you may have guessed, the ContactoAdditionalId (I left it with the real name, in case THAT is the problem) value is NOT appearing in the View. 正如你可能已经猜到了,ContactoAdditionalId(我把它用真名,如果是问题)值在视图中出现。

When I press F12 in Chrome to open the Developer Tools and inspect the HTML, there is no value for the ContactoAdditionalId , but there ARE values for Test1, Test2 and Test3. 当我按下F12在Chrome中打开开发者工具和检查HTML, 没有为ContactoAdditionalId没有价值 ,但也测试1,Test2的和Test3的值。

Note that I used a long, a long? 注意我用了很久,很久吗? and a string to check if the problem was something to do with the types, but all three show value in the HTML. 和一个用于检查问题是否与类型有关的字符串,但是所有这三个字符均在HTML中显示值。

Of course, the purpose of this hidden field is to get that ContactoAdditionalId back when POST'ing. 当然,此隐藏字段的目的是在发帖时取回ContactoAdditionalId。 This way it doesn't work and its model property is null! 这样,它不起作用,并且其model属性为null!

I also noticed something strange: if I add in the View, just before the HiddenFor: 我还注意到了一些奇怪的事情:如果在View中添加HiddenFor之前:

@Html.DisplayFor( model => model.ContactoAdditionalId, ... )

It suddenly works and now the hidden field has the value in it! 它突然起作用,现在隐藏字段具有价值!

What is happening here? 这是怎么回事 I ran debug step-to-step in Visual Studio 2017 and when I reached the end of the action code, it started stepping in the cshtml View, so no additional code (as far as I know, at least not our project's code) is being executed after the action and before the view is transformed in HTML. 我在Visual Studio 2017中逐步进行了调试,当我到达动作代码结尾时,它开始单步进入cshtml视图,因此没有其他代码(据我所知,至少不是我们项目的代码)动作之后并且在视图中HTML变换前被执行。

You probably have a parameter in ModelState with the same name than the one you don't see the data. 您可能在ModelState中有一个参数,名称与您看不到数据的名称相同。 If you are changing that value in the Model, without clearing it from the ModelState, HiddenFor will pick the ModelState field first. 如果要在Model中更改该值,而没有从ModelState中清除该值,则HiddenFor将首先选择ModelState字段。 Either do a ModelState.Clear(), or remove only the field with ModelState.Remove( "ContactoAdditionalId" )" 请执行ModelState.Clear()或仅删除具有ModelState.Remove(“ ContactoAdditionalId”)“的字段

Also, you can use ModelState.Clear(). 另外,您可以使用ModelState.Clear()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM