MVC5 Asp.net有没有办法从EditorFor得到正确的值的子类 [英] MVC5 Asp.net Is there a way to get the correct value from EditorFor for a subclass

查看:100
本文介绍了MVC5 Asp.net有没有办法从EditorFor得到正确的值的子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类

 公共类公民
{
    众长ID {搞定;组; }
    公共字符串名称{;组; }
    众长CityID {搞定;组; }
    公共字符串城{搞定;组; }
    公开名单< CitizenResource>资源{搞定;组; }
}

我想编辑在资源的个别项目,但EditorFor没有建立正确输入。

  @for(INT I = 0; I< Model.Resources.Count;我++)
{
    @ Html.BeginForm(EditResource,公民)
    {
        @ Html.AntiForgeryToken()
        < D​​IV CLASS =形横>
            < D​​IV CLASS =表单组>
                < p =班的形式控制静电>
                    @ Html.DisplayFor(型号=> model.Resources [I] .Name点)
                &所述; / P>
                < D​​IV CLASS =COL-MD-10>
                    @ Html.EditorFor(型号=> model.Resources [I] .CurrentAmount,新{htmlAttributes = {新@class =表格控}})
                    @ Html.ValidationMessageFor(型号=> model.Resources [I] .CurrentAmount,新{@class =TEXT-危险})
                    <输入类型=隐藏的名字=资源ID值=@ Model.Resources [I] .ResourceID/>
                    <输入类型=提交值=更新级=BTN BTN-默认的/>
                < / DIV>
            < / DIV>
        < / DIV>
     }
}

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

谁能帮我指出了正确的方向?


解决方案

您发的原因的前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
}}

这篇关于MVC5 Asp.net有没有办法从EditorFor得到正确的值的子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆