C# 用于编辑模型中列表属性的显示模板在表单提交时返回null
我有一个模型,它包含一个类,比如C# 用于编辑模型中列表属性的显示模板在表单提交时返回null,c#,asp.net,asp.net-mvc,entity-framework,C#,Asp.net,Asp.net Mvc,Entity Framework,我有一个模型,它包含一个类,比如Days,其中Days是Day的集合 这是实体框架自动生成模型的外观: public MyModel() { this.ExceptionDays = new HashSet<ExceptionDay>(); this.RegularDays = new HashSet<RegularDay>(); } public int Id { get;
Days
,其中Days
是Day
的集合
这是实体框架自动生成模型的外观:
public MyModel()
{
this.ExceptionDays = new HashSet<ExceptionDay>();
this.RegularDays = new HashSet<RegularDay>();
}
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public virtual ICollection<ExceptionDay> ExceptionDays { get; set; }
public virtual ICollection<RegularDay> RegularDays { get; set; }
}
@model MyModel
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
<div class="form-group">
<div class="col-md-10">
**@Html.EditorFor(model => model.RegularDays, "ICollection_RegularDay_Edit")**
</div>
</div>
<div class="form-group">
<div class="col-md-10">
**@Html.EditorFor(model => model.ExceptionDays, "ICollection_ExceptionDay_Edit")**
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
我的编辑模板如下所示:
@model RegularDay
<div class="form-group">
@Html.LabelFor(model => model.dayOfWeek)
<div class="col-md-">
<div class="col-md-">
@Html.DropDownListFor(model => model.dayOfWeek, new SelectList(
new List<Object>
{
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
}
))
</div>
@Html.ValidationMessageFor(model => model.dayOfWeek, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.startTime)
<div class="col-md-">
@Html.EditorFor(model => model.startTime, new { htmlAttributes = new { @class = "form-control" } })
@*@Html.ValidationMessageFor(model => model.startTime, "", new { @class = "text-danger" })*@
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.NumberOfHours)
<div class="col-md-">
@Html.DropDownListFor(model => model.NumberOfHours, new SelectList(
new List<Object>
{
1,2,3,4,5,6,7,8
}
))
</div>
</div>
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,Name,Description, RegularDays, ExceptionDays")] MyModel myModel)
{
if (ModelState.IsValid)
{
db.LocationHoursModels.Add(locationHoursModel);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(myModel);
}
如何为该MVC的
Days
属性类型创建显示模板或创建视图?首先删除[Bind(Include=…)]
。是否要动态添加新的例外日
和/或常规日
或仅编辑现有项目?当它是一个编辑器模板时,为什么要使用@Html.DisplayFor
,为什么要传递模板名称?(应该将EditorTemplate
命名为RegularDay.cshtml
1.删除了绑定属性。2.我想创建一个新项目,但最终我也想动态编辑。3.@Html.DisplayFor
是一个错误。我修复了它。3.显式调用该模板是因为我的DisplayFor/EditorFor用于RegularDay,但我的模板用于RegularDay
模型/类。我可以为RegularDay
模型创建模板,但当顶部的模型为RegularDay
时,我不知道如何访问RegularDay
模型。要创建新项目,您必须使用javascript。如果您需要示例,请告诉我。您的模板应该是RegularDay.cshtml
和@model RegularDay
(与ExceptionDay
相同),并且位于视图/共享/编辑模板
(或视图/YourController/EditorTemplates
)文件夹中,称为@Html.EditorFor(m=>m.RegularDay)
等。通过传递模板名称,您将丢失控件的自动生成索引命名