【问题标题】:MVC 5 Complex View Model binding is not workingMVC 5 复杂视图模型绑定不起作用
【发布时间】:2015-06-26 07:12:22
【问题描述】:
public class CreateProjeModel
{
    public Proje Proje { get; set; }
    public List<GeometryModel> GeometryList { get; set; }
    public CreateProjeModel()
    {
        Proje = new Proje();
        GeometryList = new List<GeometryModel>();
    }
}

public class GeometryModel
{
    public List<PointModel> PointList { get; set; }

    public GeometryModel()
    {
        PointList = new List<PointModel>();
    }
}

public class PointModel
{
    public int X { get; set; }
    public int Y { get; set; }
}

public class Proje : EntityBase
{
    public int FirmaId { get; set; }
    public int IlId { get; set; }
    public int? IlceId { get; set; }
    public int PlanTurId { get; set; }
    public int EtudTurId { get; set; }
    public int EtudAmacId { get; set; }
    public int DilimId { get; set; }
    public string Aciklama { get; set; }

    public virtual Firma Firma { get; set; }
    public virtual IL Il { get; set; }
    public virtual ILCE Ilce { get; set; }
    public virtual PlanTur PlanTur { get; set; }
    public virtual EtudTur EtudTur { get; set; }
    public virtual EtudAmac EtudAmac { get; set; }
    public virtual Dilim Dilim { get; set; }

}

我有一个名为 CreateProjeModel 的复杂模型。我正在使用“for”来循环集合属性和绑定,如下所示:

@Html.TextBoxFor(m => m.GeometryList[i].PointList[j].X)

动作如下:

[HttpPost]
public async Task<ActionResult> Create(CreateProjeModel proje)
{
    //ToDo
    return View(proje);
}

发布的数据如下:

说到动作,GeometryList 是空的,Proje 的属性没有设置为 post 值。我哪里做错了?

【问题讨论】:

    标签: asp.net asp.net-mvc asp.net-mvc-5 model-binding


    【解决方案1】:

    您的问题是您的CreateProjeModel 模型有一个名为Proje 的属性,但您的Create() 方法的参数也命名为proje。您需要将方法签名更改为(比如说)

    public async Task<ActionResult> Create(CreateProjeModel model)
    

    其中参数名称与您的属性之一的名称不同

    【讨论】:

    • 我从来没有想过它。谢谢。我正要开始编写我的自定义活页夹。是否有任何默认模型绑定器的文档?
    猜你喜欢
    • 2018-06-18
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 2016-03-07
    • 1970-01-01
    • 1970-01-01
    • 2014-02-12
    • 1970-01-01
    相关资源
    最近更新 更多