2011-07-13 42 views
2

我想讓用戶通過下拉列表或場館地圖選擇一個座席區域[現在讓我們說下拉列表] 用戶選擇休息區和點擊提交按鈕,我想加載與他們選擇了休息區的桌子另一種觀點。MVC3 - 剃刀 - 在視圖或下拉列表之間傳輸數據

下面是數據模型的.edmx爲休息區,並表[表]部分(只是向您展示的關係設置。)

EDMX Model

我試過MVC3期貨序列化 - 無法按照我想要的方式工作

在他們的網站上看到此示例後,我看着KnockoutJS:http://knockoutjs.com/examples/cartEditor.html以及當您選擇產品在其旁邊下拉列表中更改的類別時的方式。 (此方法現在可行),但無法從示例中瞭解數據如何拉入)

最終,我想擁有2個視圖(或一個只隱藏座位區域選擇並顯示錶選擇)

Razor視圖 - 用戶會點擊[分配表]去的第一個視圖(圖#1)

Click Assign Table

查看#1:選擇客廳角 [控制器代碼]

public ActionResult AddTableTo(int 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(); 
     } 

與下拉生成此Razor視圖:

View1 with drop-down

我將最終使這個地圖會場展示不同的水平和休息區。

他們選擇了休息區後...我想重定向到從之前不同的視圖保留數據(SeatingAreaID),或隱藏休息區選擇,並顯示在休息區的桌子。

[部分控制器代碼查看#2]

ViewBag.TableId = new SelectList(db.Tables.Where(y => y.SeatingAreaId == seatingAreaId), "TableId", "TableName"); 

請讓我知道什麼是韓德爾的最佳方式。我相信我會用這一次,而不僅僅是這一次。知道該怎麼做可能非常有價值。

這可能是一個很容易的事,但我不能就如何做到這一點,我想的方式隨時隨地發現任何東西。而SeatingAreaID是不是我會希望視圖之間傳遞的唯一變量,我需要保持VenueID & VipServiceID轉移也跟着,在控制器將創建一個多到很多表的選擇結束表和VipService之間的關係並重定向回VipService Details頁面。

謝謝你的時間和幫助。 添

回答

1

我說幹就幹,只是沒有我知道的唯一的方式,可能不是最好的方式,但它的工作。如果任何人有更好的方式來完成這個讓我知道。

我做了什麼:用戶選擇添加表按鈕

後。

[控制器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); 
      } 

     } 

最後,我要做出的下拉列表進入會場的視覺效果,使用戶只需觸摸(點擊),他們要分配的區域和表格。

再次,如果任何人有一個更好的解決方案,請讓我知道。

謝謝。

0

首先第一件事情我會強烈建議您停止使用ViewData字典屬性,以從控制器的觀點傳遞數據。任何時候你可以使用ViewData,你也可以使用ViewModel。使用ViewModel,您可以讓編譯器幫助您,而不僅僅是將密鑰傳遞到字典中。

很好看的可以在這裏找到:http://stephenwalther.com/blog/archive/2009/04/13/asp.net-mvc-tip-50-ndash-create-view-models.aspx

越來越願意使用視圖模型模式,你應該能夠輕鬆地將數據從視圖之間移動之後。

+0

我有一個視圖模型設置好的,我沒有看到文章如何有什麼做什麼我問。 –