【发布时间】:2017-07-25 11:35:30
【问题描述】:
我刚刚开始使用 ASP.net mvc 5,遇到了一个我在下面解释过的问题:
我有User 和History 模型。并且用户可以拥有多个历史记录。
User模特:
public class User
{
public int ID { get; set; }
public string TicketType { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Gender { get; set; }
public virtual ICollection<History> Histories { get; set; }
}
History模特:
public class History
{
public int ID { get; set; }
public int Year { get; set; }
public int Month { get; set; }
public int Day { get; set; }
public virtual User User { get; set; }
}
UserController:
public class UsersController : Controller
{
private MyDbContext db = new MyDbContext();
public ActionResult Create()
{
var user = new User();
user.Histories = new List<History>();
user.Histories.Add(new History { });
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ID,TicketType,FirstName,LastName")] User user)
{
if (ModelState.IsValid)
{
db.Users.Add(user);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(user);
}
}
这是我的表格:
Create.cshtml
@model MyApp.Models.User
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.Partial("_Form")
<input type="submit" value="Create" class="btn btn-lg btn-success" />
}
_Form.cshtml:只是为了删除Create 和Edit 表单中的重复项
@model MyApp.Models.User
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.TicketType, htmlAttributes: new { @class = "control-label" })
@Html.DropDownListFor(model => model.TicketType, new List<SelectListItem>
{
new SelectListItem() {Text = "OPD", Value="opd"},
new SelectListItem() {Text = "Emergency", Value="emergency"},
}, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.TicketType, "", new { @class = "text-danger" })
</div>
<div class="form-group">
@Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
</div>
<div class="form-group">
@Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
</div>
<!-- TODO: fix these form elements which are
related to History not User. I am not sure how to do that.
Also NOTE that, user fields are saving properly and
update is also working. -->
<div class="form-group">
<label class="control-label">Year</label>
<input type="text" value="" class="form-control" />
</div>
<div class="form-group">
<label class="control-label">Month</label>
<input type="text" value="" class="form-control" />
</div>
<div class="form-group">
<label class="control-label">Day</label>
<input type="text" value="" class="form-control" />
</div>
【问题讨论】:
-
一些选项请参考this answer,使用
BeginCollectionItem()的详细示例请参考this one -
@StephenMuecke:是的,我已经研究过那个解决方案,对我来说,我不会有多个嵌套表单,所以,如果我可以在不使用
BeginCollectionItem助手的情况下解决它,那会很棒吗? -
你是什么意思多个嵌套表单(两个链接都没有 - 特别是因为它不起作用)。不知道为什么您不想在工作中使用正确的工具,但请查看第一个链接中的第二个选项
-
@StephenMuecke:感谢您的帮助,试试
BeginCollectionItem()helper。 :) 。阅读第一个链接答案后,您还提供了手动解决方案,也感谢您。 -
@StephenMuecke:我使用
BeginCollectionItem()helper 后的最后一个问题是;如何在Create操作中允许Year、Month、Day,即:[Bind(Include = "ID,TicketType,FirstName,LastName")]?请帮忙
标签: c# asp.net asp.net-mvc asp.net-mvc-5 entity-framework-6