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.

8
  • 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?) Commented Oct 24, 2015 at 9: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. Commented Oct 24, 2015 at 9: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 for an explanation as to why) Commented Oct 24, 2015 at 9: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) Commented Oct 24, 2015 at 9:17
  • 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() Commented Oct 24, 2015 at 9:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.