TextBoxFor Helper retains previous value even when model value is empty

前端 未结 3 934
北恋
北恋 2021-01-11 16:54

I have an MVC form for adding a simple entity. I am using TextBoxFor(model => model.FieldName) to create the input fields. I have a Save button and a Save and New button.

3条回答
  •  悲哀的现实
    2021-01-11 17:41

    The issue here is that your ViewData.ModelState is still populated with the values from the original post, even if the Model is null and you don't explicitly pass any values into your view.

    I actually don't think redirecting to the original action is that ugly of a solution, but if you don't want to do that then clearing out the ViewData should work for you:

    [HttpPost]
    public ActionResult Save(TestModel model)
    {            
        ViewData = null;
        return View();
    }
    

提交回复
热议问题