What am I misunderstanding about how Html.TextBoxFor works

前端 未结 3 1502
旧时难觅i
旧时难觅i 2021-01-05 03:02

I\'m just trying to get back into .NET MVC with the new release and I can\'t get my head round the way may view is binding to the DataModel.

I have a model with a pr

相关标签:
3条回答
  • 2021-01-05 03:46

    Because HTML helpers read the value from the ModelState and not from the model. In order the change that behavior you'll need to work with the ModelState as well.
    (see: Changing model’s properties on postback)

    0 讨论(0)
  • 2021-01-05 03:46

    This should work for the first two:

    <%= Html.TextBox("first_name", x => x.first_name)%>
    <%= Html.TextBoxFor(model => model.first_name) %> 
    
    0 讨论(0)
  • 2021-01-05 04:07

    You need to clear your model state so your code would look something like:

    [HttpPost]
    public ActionResult Register(Registration model)
    {
        ModelState.Clear();
        model.first_name = "Steve";
        return View(model);
    }
    
    0 讨论(0)
提交回复
热议问题