我的数据库中有大约20个城市。我需要开发一个页面,我可以为每个城市分配一些价值。我可以更新页面中的分配。
我的数据库如下所示
City table - > City(CityId, Name)
我有一张表来存储城市的分配
Table - > CityAllocation(CityId, Allocation)
我必须在所有城市名称附近显示一个文本框。
CityAllocationViewModel.cs
public class CityAllocationViewModel
{
public List<City> Cities{get;set;}
public List<CityAllocation> CityAllocations{get;set;}
}
CityAllocation.cshtml
步骤1:要显示所有城市,我将遍历城市列表并显示它。
步骤2:需要在每个城市附近显示文本框 - &gt;如果存在任何内容,它将从CityAllocations加载现有值。否则只需要显示空
但CityAllocations列表第一次为null。有人可以解释我如何构造具有正确绑定名称的文本框,以便在我保存时,将值正确绑定到操作方法参数。我正在使用MVC 4。
我的行动方法如下所示
CityController.cs
public ActionResult SaveCityAllocation(CityAllocationViewModel cityAllocationViewModel)
{
}
答案 0 :(得分:1)
我建议使用EditorTemplateFor
。为CityAllocation
创建部分视图,并在主视图CityAllocationViewModel
中调用@html.EditorFor(m=>m.CityAllocations)