ASP.NET Core: asp-* attributes use request payload over model?

后端 未结 2 1460
感动是毒
感动是毒 2021-01-20 00:56

It seems that in ASP.NET Core, the value in asp-* attributes (e.g. asp-for) is taken from the request payload before the model. Example:

P

2条回答
  •  北海茫月
    2021-01-20 01:24

    I can confirm your observation. What's really going to blow your mind is that this:

    [HttpPost]
    public IActionResult Foo (MyModel m)
    {
        m.MyProperty = "changed";
        var result = new MyModel { MyProperty = "changed" };
        return View(result);
    }
    

    ...gives you the same result.

    I think you should log a bug: https://github.com/aspnet/mvc/issues


    Edit: I now remember this issue from previous encounters myself and concede that it isn't necessarily a bug, but rather an unintended consequence. The reasons for the result of executing this code is not obvious. There likely isn't a non-trivial way to surface a warning about this, but PRG is a good pattern to follow.

提交回复
热议问题