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

后端 未结 2 1683
伪装坚强ぢ
伪装坚强ぢ 2021-01-28 12:56

I have a class

public class Citizen
{
    public long ID { get; set; }
    public string Name { get; set; }
    public long CityID { get; set; }
    public stri         


        
相关标签:
2条回答
  • 2021-01-28 13:03

    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
    }}
    
    0 讨论(0)
  • 2021-01-28 13:22

    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)

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