TextBoxFor value not updating after post

后端 未结 5 792
暗喜
暗喜 2021-02-05 11:00

I have a simple strongly typed view, but I cant seem to update a textbox on my form after a post.

Here is my model:

public class Repair
  {
    public          


        
5条回答
  •  长发绾君心
    2021-02-05 11:48

    If you find ModelState.Clear() to be too destructive, you can target only the item you are changing, while preserving the rest with ModelState.Remove()

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Index(Repair r)
    {
        r.Number = "New Value";
        ModelState.Remove("Number");
        return View(r);
    }
    

    Also, it appears that it doesn't matter with either Remove() or Clear() whether you call that method before you update your model or after

提交回复
热议问题