MVC 5 复杂视图模型绑定不起作用
MVC 5 Complex View Model binding is not working
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 值。我哪里做错了?
您的问题是您的 CreateProjeModel
模型有一个名为 Proje
的 属性,但是您的 Create()
方法的参数也被命名为 proje
。您需要将方法签名更改为(例如)
public async Task<ActionResult> Create(CreateProjeModel model)
其中参数名称与您的某个属性的名称不同
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 值。我哪里做错了?
您的问题是您的 CreateProjeModel
模型有一个名为 Proje
的 属性,但是您的 Create()
方法的参数也被命名为 proje
。您需要将方法签名更改为(例如)
public async Task<ActionResult> Create(CreateProjeModel model)
其中参数名称与您的某个属性的名称不同