1

Previous question

Sample solution

Demo site

Very simple Controller

    public JsonResult A1(Guid? id)
    {
        var model = new MyModel { Id = g2, Id2 = g2, Name = "A1" };

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

    public JsonResult A2(Guid? id)
    {
        ModelState.Clear();
        var model = new MyModel { Id = g2, Id2 = g2, Name = "A2" };

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

Very simple View with two buttons and script

    <p><a class="btn btn-default" href="#" onclick="F1(1)">NO ModelState.Clear()</a></p>
    <p><a class="btn btn-default" href="#" onclick="F1(2)">ModelState.Clear()</a></p>
function F1(a) {
    $.ajax({
        type: 'GET',
        url: '@Url.Content("~/Home/A")' + a,
        data: {
            id: '@ModelBug.Controllers.HomeController.g1'
        },
        dataType: 'json',
        beforeSend: function (xhr) {
        },
        success: function (data) {
            $("#col" + a).append('<div class="row"><div class="col-md-12">' + data.html + '</div></div>');
        },
        error: function () {

        }
    });
}

Very simple rendered View

<div>Model.Id = @Model.Id</div>
<div>TextBoxFor(model => model.Id) = @Html.TextBoxFor(model => model.Id)</div>
<div>TextBoxFor(model => Model.Id) = @Html.TextBoxFor(model => Model.Id)</div>
<div></div>

<div>Model.Id2 = @Model.Id2</div>
<div>TextBoxFor(model => model.Id2) = @Html.TextBoxFor(model => model.Id2)</div>
<div>TextBoxFor(model => Model.Id2) = @Html.TextBoxFor(model => Model.Id2)</div>
<div></div>

<div>Model.Name = @Model.Name</div>
<div>TextBoxFor(model => model.Name) = @Html.TextBoxFor(model => model.Name)</div>
<div>TextBoxFor(model => Model.Name) = @Html.TextBoxFor(model => Model.Name)</div>
<div></div>
<div></div>

In result view in TextBoxFor(model => Model.Id) without ModelState.Clear()

show 4a01aadd-c61a-4524-95f6-e88a665c0745 (from param (Guid? id))

must be d2afb9a8-fb43-4237-9f34-abc7e5b59a41** (from public static Guid g2)

Main problem

Why value in TextBoxFor() from param id not from model.Id ?

P.S. This problem get in big project where model get from DataBase but in view model.Id is different from Database, model.Id = param of query.

Community
  • 1
  • 1
Alexandr Sulimov
  • 1,894
  • 2
  • 24
  • 48
  • Not clear what your asking. What do you mean by `must be x.Id = d2afb9a8-fb43-4237-9f34-abc7e5b59a41`? What are the values of the parameter `id` and `g2`. (and why are you returning json instead of a partial view?) –  Oct 24 '15 at 09:03
  • Why value in TextBoxFor() from param **id** not from **model.Id** ? @Model.Id in partial is valid!!! But in TextBoxFor(model => model.Id) - NOT valid. – Alexandr Sulimov Oct 24 '15 at 09:11
  • 1
    The duplicates in your previous question explained it. And your project name suggests you think this is a bug - its not - its by design (refer the last part of [this answer](http://stackoverflow.com/questions/26654862/textboxfor-displaying-initial-value-not-the-value-updated-from-code/26664111#26664111) for an explanation as to why) –  Oct 24 '15 at 09:12
  • 1
    Your parameter has a name `id`. Its value is added to `ModelState` when the method is called. Your model also has a property named `id` which you set to a different value. But the html helpers use the values from `ModelState` if they exist. You can easily solve this by changing the name of one or the other so they do not match (or use `ModelState.Clear()` as per your 2nd example) –  Oct 24 '15 at 09:17
  • Sorry i don`t understand your answer :(. 1 I not use redirect. 2 I not post in param model. I see a) action param name "id" matches with property name MyModel.Id and model !created in action with different value of "Id" but in partial Model.Id - correct but in helper model => model.Id - NOT correct. If change action param name "id" to "id2" - al work correct. – Alexandr Sulimov Oct 24 '15 at 09:23
  • 2
    Whether you redirect or not is irrelevant - its the last part of that answer which explains why the html helpers use the value from `ModelState` rather that the value of the model property. **Its by design!** If you don't want that behavior, then change the name of your method parameter or the name of the model property or use `ModelState.Clear()` –  Oct 24 '15 at 09:29
  • Thanks. "But the html helpers use the values from ModelState if they exist". I first time get this "bug". I think, very bad watch to match in param name and model property name. If after refactoring model property name, property name match with param name - helpers have work not valid. – Alexandr Sulimov Oct 24 '15 at 09:33
  • Sorry, I don't understand what you saying in your last comment. –  Oct 24 '15 at 09:37

0 Answers0