C# 动态地将ICollection项添加到模型中,同时;创造;在MVC中
找到了一个解决方案,但这是一个非常丑陋的解决方案,请看底部,有人能改进该解决方案吗? 所以我有一个像这样的虚拟模型C# 动态地将ICollection项添加到模型中,同时;创造;在MVC中,c#,asp.net,asp.net-mvc,razor,C#,Asp.net,Asp.net Mvc,Razor,找到了一个解决方案,但这是一个非常丑陋的解决方案,请看底部,有人能改进该解决方案吗? 所以我有一个像这样的虚拟模型 public class TestModel { public int TestModelID { get; set; } public string Name { get; set; } } public class Collector { public int CollectorID { get; set; } public string Co
public class TestModel
{
public int TestModelID { get; set; }
public string Name { get; set; }
}
public class Collector
{
public int CollectorID { get; set; }
public string CollectorString { get; set; }
public ICollection<TestModel> MyList { get; set; }
}
还有一个像这样的
public class TestModel
{
public int TestModelID { get; set; }
public string Name { get; set; }
}
public class Collector
{
public int CollectorID { get; set; }
public string CollectorString { get; set; }
public ICollection<TestModel> MyList { get; set; }
}
这是生成的HTML代码(我希望只有相关部分)
虽然非常推荐在MVC中使用,但在这里生成
<input class="form-control text-box single-line" name="[0].Name" type="text" value="" />
<input class="form-control text-box single-line" name="MyList[0].Name" type="text" value="" />
控制器理解它,我没有得到
null
。如何使用razor解决此问题?尝试更改收集器类以包含初始化
public class Collector
{
public Collector()
{
set MyList = new Collection<TestModel>();
}
public int CollectorID { get; set; }
public string CollectorString { get; set; }
public ICollection<TestModel> MyList { get; set; }
}
公共类收集器
{
公共收藏家()
{
set MyList=new Collection();
}
公共int集合ID{get;set;}
公共字符串收集器字符串{get;set;}
公共ICollection MyList{get;set;}
}
ICollection
没有索引器<代码>IList执行
public class Collector {
public Collector() {
MyList = new List<TestModel>();
}
public int CollectorID { get; set; }
public string CollectorString { get; set; }
public IList<TestModel> MyList { get; set; }
}
在视图中生成所需的标记
<input class="form-control text-box single-line" name="MyList[0].Name" type="text" value="" />
您可以使用razor语法来实现这一点
<div class="form-group">
@for(int i = 0; i < Model.MyList.Count; i++) {
@Html.LabelFor(model => Model.MyList[i].Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.MyList[i].Name, new { htmlAttributes = new { @class = "form-control" } })
</div>
}
</div>
@对于(int i=0;imodel.MyList[i]。名称,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.MyList[i].Name,new{htmlAttributes=new{@class=“form control”})
}
您的收集器
类应该包含一个构造函数,并将MyList
初始化为MyList=newlist()
@RobertStettler您是否考虑过将ICollection更改为IList,以便使用索引器。ICollection没有索引器属性,而IList有。我已经提供了一个解释的答案。该代码无法生成,您的意思是删除集
?我发现找不到错误类型或命名空间集。即使这样,它也不起作用
@Html.EditorFor(model => model.MyList[0].Name, new { htmlAttributes = new { @class = "form-control" } })
<input class="form-control text-box single-line" name="MyList[0].Name" type="text" value="" />
<div class="form-group">
@for(int i = 0; i < Model.MyList.Count; i++) {
@Html.LabelFor(model => Model.MyList[i].Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.MyList.ToList()[i].Name, new { htmlAttributes = new { @class = "form-control" } })
</div>
}
</div>
[HttpGet]
public ActionResult Create() {
var model = new Collector();
model.MyList.Add(new TestModel());
return View(model);
}
<div class="form-group">
@for(int i = 0; i < Model.MyList.Count; i++) {
@Html.LabelFor(model => Model.MyList[i].Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.MyList[i].Name, new { htmlAttributes = new { @class = "form-control" } })
</div>
}
</div>