2

I'm trying to figure out why my view is not restoring the data to its original state after my form has been submitted and I'm returning the relevant ViewModel and I'm setting the ModelState.AddModelError("", "error");

When my Edit page is requested, I call a method which builds my view model and passes it to my view and everything gets displayed as expected:

public ActionResult Edit(string id, string returnUrl)
{
    if (ReferenceEquals(id, null))
        return HttpNotFound();

    var vm = GetOrganzationEditViewModel(id, returnUrl);

    return View(vm);
}

When I make some changes to the fields available in my view and I submit it, I perform some server side validation and if it fails one of the specific condition, I requests the ViewModel as above by calling the same method i.e. GetOrganzationEditViewModel using the same parameter as when the page was requested as these are included in the ViewModel and I'm also returning the relevant error using the ModelState.AddModelError.

if ((organizationModelFromDb.MembershipType == MembershipType.Full)
{
    ModelState.AddModelError("", $"Test Error:");

    return View("Edit",GetOrganzationEditViewModel(organizationEditViewModel.
    Organization.OrganizationId, organizationEditViewModel.ReturnUrl));
}

It does display the error as expected in my ValidationSummary but I was under the impression that by returning the ViewModel, it would re-fill my values with the ViewModel's values returned, but it actually leaves the values as they were when I edited them.

My Razor page has fields that are binded to my ViewModel and it looks pretty standard:

<div class="form-group">
    @Html.LabelFor(model => model.Organization.Website, htmlAttributes: new { @class = 
    "control-label col-md-3"})
    <div class="col-md-5">
        @Html.EditorFor(model => model.Organization.Website, new 
        {htmlAttributes = new { @class = "form-control", id = "txtWebsite" } })
        @Html.ValidationMessageFor(model => model.Organization.Website, "", new { @class = 
        "text-danger" })
    </div>
</div>

Am I missing something or am I suppose to handle this differently?

Thanks

Thierry
  • 6,142
  • 13
  • 66
  • 117
  • What you seeing is the default behavior, although it can be overridden. But why would you want to overwrite the values the user has entered and make them re-edit it over again? –  May 26 '17 at 02:26
  • @StephenMuecke The reason is they selected or entered something invalid so I thought it would be better to restored it to the original value but I take your point. If I leave it as is, they won't be able to go any further anyway. How can I overwrite this btw? I'd I still want to give it a go and have a feel with both behaviour and see which one seems more natural. – Thierry May 26 '17 at 07:52
  • Have a look at [this answer](https://stackoverflow.com/questions/26654862/textboxfor-displaying-initial-value-not-the-value-updated-from-code/26664111#26664111) for an explanation of the behavior and how you can use `ModelState.Clear();` Personally I would be rather annoyed if I entered all the data and you made me reenter it all over again just because one thing is invalid –  May 26 '17 at 07:57
  • @StephenMuecke Thanks and true, the more I think about it, but may I'll reset only the invalid entries. Not sure yet. – Thierry May 26 '17 at 08:25
  • But then the user will not know what was invalid because `ModelState` clears the errors and they wont be shown. –  May 26 '17 at 08:26
  • @StephenMuecke I understand, but what if I don't clear the `ModelState` and display/highlight the "dodgy" fields. I suppose I could do this and leave whatever the user has entered. – Thierry May 26 '17 at 08:41
  • That is the default behavior :) –  May 26 '17 at 08:43

1 Answers1

0

I read your above problem and as per my understanding, you may got that restore issue ,because of the following reason.

The viewmodel variable , coming through the edit post method as a parameter may intialized again or the name of the variable would have changed before send it to the view.

since i couldnt see the edit post method fully ,am not able to think any other reason.

And more thing you can do is debug ,by putting break point in the controller action method, to find out where exactly the data is getting lost.

Hope above information may helpful to get right direction,kindly let me know your thoughts or feedbacks

Thanks

Karthik

Karthik Elumalai
  • 1,574
  • 1
  • 11
  • 12