我目前正在使用以下模型开发ASP.Net MVC应用程序:
索引视图模型
class IndexViewModel
{
public List<Type> Types
{
get
{
...
}
}
public Person Person { get; private set; }
public List<State> States
{
get
{
...
}
set
{
...
}
}
public List<Client> Clients
{
get
{
...
}
}
}
Types:
class Type
{
public string Name { get; set; }
public string Description { get; set; }
public string AccountNo { get; set; }
}
States:
class State
{
public string Name { get; set; }
public string Country { get; set; }
public string Abbreviation { get; set; }
}
Clients:
class Client
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Type Type { get; set; }
}
我目前在视图中引用属性,如下所示:
@Html.HiddenFor(m => m.Types)
@Html.HiddenFor(m => m.States)
@Html.HiddenFor(m => m.Clients)
有没有不同的方法?
发布于 2018-08-15 14:49:16
列表可以通过像myList这样的索引来访问,名称将是模型属性名称和索引,如
<input type="hidden" name="Model.Clients[0]" value="" />
当将模型加载到视图中时,对3个列表执行如下操作:
for(int i = 0; i < Model.Types.Count; i++)
{
@HTML.HiddenFor(x => Model.Types[i])
}
for(int i = 0; i < Model.Clients.Count; i++)
{
@HTML.HiddenFor(x => Model.Clients[i])
}
for(int i = 0; i < Model.States.Count; i++)
{
@HTML.HiddenFor(x => Model.States[i])
}
https://stackoverflow.com/questions/-100002180
复制相似问题