【发布时间】:2018-09-21 20:19:27
【问题描述】:
我对 ASP.Net 很陌生,我正在尝试预订指定的分支。当我点击一个分支的预订链接时,它会出现一个下拉列表,让我从数据库中已经存在的分支中选择一个分支。我想要的是已经根据我点击的那个分支的预订链接设置了这个分支。在数据库中,预订实体具有引用分支实体中的 branchId 的属性。我查看了一些相关问题,例如MVC5 - How to set “selectedValue” in DropDownListFor Html helper,但都没有正确解决我的问题。我暂时还没有这个概念。
这是我的预订控制器中的创建操作:
// GET: Reservations/Create
public ActionResult Create()
{
ViewBag.branchId = new SelectList(db.Branches, "branchId", "name");
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "reservationId,branchId,customerId,date")] Reservation reservation)
{
reservation.customerId = User.Identity.GetUserId();
ModelState.Clear();
TryValidateModel(reservation);
if (ModelState.IsValid)
{
db.Reservations.Add(reservation);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.branchId = new SelectList(db.Branches, "branchId", "name", reservation.branchId);
return View(reservation);
}
这是我的预订创建视图:
@model Mel_Medicare_Location_Reservation_System.Models.Reservation
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Reservation</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group ">
@Html.LabelFor(model => model.branchId, "branchId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("branchId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.branchId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.customerId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.customerId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.customerId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.date, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.date, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.date, "", new { @class = "text-danger" })
</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>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
【问题讨论】:
标签: c# asp.net-mvc razor