Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 5 Html.HiddenFor renders incorrect property value but Model.Property renders correct value

  • Visual Studio 2013 Professional
  • C#
  • .NET 4.5 Framework
  • ASP.NET MVC 5

In my view-model class, I have a property called SerializedSelf that is used to represent itself as a serialized string and also de-serialize, but only over any properties that aren't already initialized. I use this to simplify the round-tripping of my view-model by having a single hidden input with this value. Any other inputs related to the model will get populated in the model by the framework before getting passed to the controller method.

The problem I'm having is that in my razor view code this isn't rendering properly:

 @Html.HiddenFor(model => model.SerializedSelf)

I haven't determined if it's serializing a completely uninitialized MyViewModel object or if it's an instance from somewhere else.

But this works properly:

 <input type="hidden" name="SerializedSelf" id="SerializedSelf" value="@Model.SerializedSelf" />

So I'm guessing it might have something to do with the lambda expression and enclosures??? When I step through in source code, the SerializedSelf has the proper value right before I call return View("myView", model).

Here is the view-model code:

using Newtonsoft.Json;
using System;
using System.Text;

public class MyViewModel
{
    [JsonIgnore]
    public string SerializedSelf
    {
        get
        {
            return JsonConvert.SerializeObject(this);
        }
        set
        {
            if (string.IsNullOrWhiteSpace(value)) return;

            string json = value;
            MyViewModel copy = JsonConvert.DeserializeObject<MyViewModel>(json);
            if (Message == null) Message = copy.Message;
            if (Phone == null) Phone = copy.Phone;
            // ...
        }
    }

    public string Message { get; set; }
    public string Phone { get; set; }
    // ...

}

What would be the difference between:

@Html.HiddenFor(model => model.SerializedSelf)

and

@Model.SerializedSelf

and what would cause the former to be inaccurate?

UPDATE 2013/12/5: I removed the logic from SerializedSelf and explicitly serialize/deserialize to/from SerializedSelf at the appropriate times to test if this has any impact. It has no impact and the behavior is as I have described above.

UPDATE 2013/12/5: Removed HTML encoding and decoding. Was left over from previous attempt at problem solving.

UPDATE 2013/12/5: I don't have time to look into this today, but it occurred to me that perhaps the [JsonIgnore] attribute on the SerializedSelf property is the culprit and it's interfering with something in the MVC framework. Will look into as soon as I can and update.

like image 550
MikeJansen Avatar asked Dec 05 '22 08:12

MikeJansen


1 Answers

If this is happening after rendering a view in response to a POST request, you should be aware that the HTML helpers use the old values because they think there is a validation error. You can find a detailed explanation in this blog post.

like image 151
Ufuk Hacıoğulları Avatar answered Mar 09 '23 00:03

Ufuk Hacıoğulları