MVC5 Asp.net Is there a way to get the correct value from EditorFor for a subclass

ε祈祈猫儿з 提交于 2019-12-02 08:38:29

The expression you sent causes that name:

What I get is name="Resources[0].CurrentAmount" which then doesn't map correctly to the CitizenResource class.

@Html.EditorFor(model => model.Resources[i].CurrentAmount, new { htmlAttributes = new { @class = "form-control" } })

Instead have a partial view to edit an CitizenResource

@model CitizenResource

@using(Html.BeginForm("EditResource", "Citizen")){
            {
//The complete elements
@Html.EditorFor(model => model.CurrentAmount, new { htmlAttributes = new { @class = "form-control" } })
//The rest of the elements
}}
masterlopau

Your code is creating multiple forms which is not good. The best way to handle this kind of scenario is the use of EditorTemplates. Checkout this link on how to do it. EditorFor() for a List of Complex Type (MVC)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!