且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MVC5 Asp.net有没有办法从EditorFor得到正确的值的子类

更新时间:2023-02-24 12:38:07

您发的原因的前pression该名称:


  

我得到的是NAME =资源[0] .CurrentAmount然后没有映射
  正确的CitizenResource类。


块引用>
  @ Html.EditorFor(型号=> model.Resources [I] .CurrentAmount,新{htmlAttributes = {新@class =表格控}})

相反,必须编辑CitizenResource的局部视图

  @model CitizenResource@using(Html.BeginForm(EditResource,公民)){
            {
//完整的元素
@ Html.EditorFor(型号=> model.CurrentAmount,新{htmlAttributes = {新@class =表格控}})
//元件的其余部分
}}

I have a class

public class Citizen
{
    public long ID { get; set; }
    public string Name { get; set; }
    public long CityID { get; set; }
    public string City { get; set; }
    public List<CitizenResource> Resources { get; set; }
}

I want to edit individual items in the Resources but the EditorFor doesn't set up the input correctly.

@for (int i = 0; i < Model.Resources.Count; i++ )
{ 
    @Html.BeginForm("EditResource", "Citizen")
    {
        @Html.AntiForgeryToken()
        <div class="form-horizontal">
            <div class="form-group">
                <p class="form-control-static">
                    @Html.DisplayFor(model => model.Resources[i].Name)
                </p>
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Resources[i].CurrentAmount, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Resources[i].CurrentAmount, "", new { @class = "text-danger" })
                    <input type="hidden" name="ResourceID" value="@Model.Resources[i].ResourceID"/>
                    <input type="submit" value="Update" class="btn btn-default" />
                </div>
            </div>
        </div>
     }
}

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

Can anyone help point me in the right direction?

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
}}