我說幹就幹,只是沒有我知道的唯一的方式,可能不是最好的方式,但它的工作。如果任何人有更好的方式來完成這個讓我知道。
我做了什麼:用戶選擇添加表按鈕
後。
[控制器1a]
public ActionResult AddTableTo(Int64 id)
{
var tableService = id;
var tableServiceName = db.VipServices.Single(x => x.VipServiceId == id);
var venueId = tableServiceName.Event.Venue.VenueId;
ViewData["TablerServiceNameFor"] = tableServiceName.Customer.FullName;
ViewData["TableServiceFor"] = tableService;
ViewBag.SeatingAreaId = new SelectList(db.SeatingAreas.Where(y => y.VenueId == venueId), "SeatingAreaId", "AreaName");
return View();
}
[查看#1]
@model ShadowVenue.ViewModels.VipSeatingAreaViewModel
@{
ViewBag.Title = "Select Seating Area";
}
<h2>Select Seating Area For:</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
<fieldset>
<legend>@ViewData.Eval("TablerServiceNameFor")'s Table(s)</legend>
<div class="editor-label">
Select Seating Area
</div>
<div class="editor-field">
@Html.DropDownList("SeatingAreaId", String.Empty)
</div>
@Html.Hidden("VipServiceId", ViewData.Eval("TableServiceFor"))
<p>
<input type="submit" name="nextButton" value="Next" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to Table Service Details", "Details", new { Id = ViewData.Eval("TableServiceFor") })
</div>
[控制器1B]
[HttpPost]
[Authorize(Roles = "Administrator, SuperUser")]
public ActionResult AddTableTo(VipSeatingAreaViewModel seatingArea, string nextButton)
{
if (nextButton != null)
return RedirectToAction("AddTableToTable", new { sid = seatingArea.SeatingAreaId, vid = seatingArea.VipServiceId });
return View(seatingArea);
}
控制器1B重定向到動作AddTableToTable [控制器2],併發送沿選定的SeatingAreaId和VipServiceId
[控制器2]
public ActionResult AddTableToTable(Int16 sid, Int64 vid)
{
var tableService = vid;
var tableServiceName = db.VipServices.Single(x => x.VipServiceId == vid);
var seatingAreaId = sid;
var seatingArea = db.SeatingAreas.Single(x => x.SeatingAreaId == sid);
ViewData["TablerServiceNameFor"] = tableServiceName.Customer.FullName;
ViewData["TableServiceFor"] = tableService;
ViewData["SeatingAreaName"] = seatingArea.AreaName;
ViewBag.TableId = new SelectList(db.Tables.Where(y => y.SeatingAreaId == seatingAreaId), "TableId", "TableName");
return View();
}
渲染視圖#2 [查看#2]
@model ShadowVenue.ViewModels.VipTableViewModel
@{
ViewBag.Title = "Select Table";
}
<h2>Select Table For:</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
<fieldset>
<legend>@ViewData.Eval("TablerServiceNameFor")'s VIP Service</legend>
<div class="editor-label">
Select Table in the <b>@ViewData.Eval("SeatingAreaName")</b> Seating Area
</div>
<div class="editor-field">
@Html.DropDownList("TableId", String.Empty)
</div>
@Html.Hidden("VipServiceId", ViewData.Eval("TableServiceFor"))
<p>
<input type="submit" name="backButton" value="Back" />
<input type="submit" name="nextButton" value="Next" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to Table Service Details", "Details", new { Id = ViewData.Eval("TableServiceFor") })
</div>
我給用戶返回到選擇不同的用餐區,或者選擇的選項添加到VIP服務的表格。在他們選擇查看帖子到[控制器2b]之後,它爲VipService和表添加多對多關係。
[控制器2B]
[HttpPost]
[Authorize(Roles = "Administrator, SuperUser")]
public ActionResult AddTableToTable(VipTableViewModel model, string backButton)
{
if (backButton != null)
{
return RedirectToAction("AddTableTo", new { id = model.VipServiceId });
}
if (ModelState.IsValid)
{
VipService v = db.VipServices.Single(x => x.VipServiceId == model.VipServiceId);
Table t = db.Tables.Single(x => x.TableId == model.TableId);
v.Tables.Add(t);
db.SaveChanges();
return RedirectToAction("Details", new { id = model.VipServiceId });
}
else
{
return View(model);
}
}
最後,我要做出的下拉列表進入會場的視覺效果,使用戶只需觸摸(點擊),他們要分配的區域和表格。
再次,如果任何人有一個更好的解決方案,請讓我知道。
謝謝。
我有一個視圖模型設置好的,我沒有看到文章如何有什麼做什麼我問。 –