1

Action in Controller:

    public JsonResult AccountEdit(Guid? id) //id = 4a01aadd-c61a-4524-95f6-e88a665c0745
    {
        //ModelState.Clear(); <------- strange code
        var model = DAL.AccountGet(id); <-- get model from database

        return new JsonResult
        {
            Data = new
            {
                html = this.RenderPartialView("View", model),
            },
            JsonRequestBehavior = JsonRequestBehavior.AllowGet
        };
    }

The rendered View:

<div>@Model.Id</div> d2afb9a8-fb43-4237-9f34-abc7e5b59a41
<div>@Html.TextBoxFor(model => model.Id)</div> 4a01aadd-c61a-4524-95f6-e88a665c0745
<div>@Html.TextBoxFor(model => Model.Id)</div> 4a01aadd-c61a-4524-95f6-e88a665c0745
@Html.HiddenFor(model => model.Id) 4a01aadd-c61a-4524-95f6-e88a665c0745

Only in line1 is the correct ID

a) If uncomment ModelState.Clear() in all lines will be "d2afb9a8-fb43-4237-9f34-abc7e5b59a41" - correct value.

b) if change AccountEdit(Guid? id) to AccountEdit(Guid? id2)

c) other property of model (missing in the parameters) rendered correct

My question is why?

Sample solution

5
  • What is the logic within the class (get property Id)? Commented Oct 23, 2015 at 18:45
  • No any logic public Guid Id { get; set; } Model.Id in view is correct. Helpers ....For() work strange Commented Oct 23, 2015 at 18:48
  • Interesting, do you have a mini project where you isolated this behavior ? I could not replicate this Commented Oct 23, 2015 at 19:30
  • 2
    HTML helpers give precedence to values in the ModelState over values in the view model. If you want to prevent that, that's what ModelState.Clear()is for. See also: stackoverflow.com/questions/7164235/… Commented Oct 23, 2015 at 19:53
  • github.com/AlexandrM/asp.mvc.model.bug Commented Oct 23, 2015 at 20:29

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.