Why is my textBoxFor using my route data?

后端 未结 1 990
礼貌的吻别
礼貌的吻别 2021-01-06 01:11

So I have these three lines:

@Html.TextBoxFor(m => m.Id)
1条回答
  •  囚心锁ツ
    2021-01-06 01:38

    You could remove the problematic value from the ModelState (which is where the Html helpers are taking it from) in the controller action that is rendering the view:

    public ActionResult SomeAction(int id)
    {
        ModelState.Remove("Id");
        MyViewModel model = ...
        return View(model);
    }
    

    Now it's the Id property of your view model that's gonna get used by the TextBox and not the one coming from the route.

    Obviously that's only an ugly horrible workaround. The correct way is to of course properly define your view models so that you do not have such naming collisions.

    0 讨论(0)
提交回复
热议问题