2011-07-13 33 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

我有一个视图模型设置好的,我没有看到文章如何有什么做什么我问。 –