Updating ModelState with model object

前端 未结 3 1783
猫巷女王i
猫巷女王i 2020-12-30 05:52

The problem: How to update ModelState in posting+validation scenario.

I\'ve got a simple form:

<%= Html.ValidationSummary() %>
<% using(Html         


        
3条回答
  •  梦毁少年i
    2020-12-30 06:20

    I know this post is fairly old but it's a problem I've had before and I just thought of a simple solution that I like - just clear the ModelState after you've got the posted values.

    UpdateModel(viewModel);
    ModelState.Clear();
    
    viewModel.SomeProperty = "a new value";
    return View(viewModel);
    

    and the view has to use the (possibly modified) view model object rather than the ModelState.

    Maybe this is really obvious. It seems so in hindsight!

提交回复
热议问题