C# MVC3-Razor-在视图或下拉列表之间传输数据

C# MVC3-Razor-在视图或下拉列表之间传输数据,c#,asp.net-mvc-3,drop-down-menu,razor,knockout.js,C#,Asp.net Mvc 3,Drop Down Menu,Razor,Knockout.js,我想让用户通过下拉列表或场馆地图选择座位区[现在就说下拉列表] 在用户选择座位区并点击提交按钮后,我想加载另一个视图,其中包含他们选择的座位区的表格 这是数据模型的一部分。EDMX用于座位区和桌子[桌子],只是为了向您展示已设置的关系 我尝试了MVC3期货序列化-无法按我想要的方式工作 在他们的网站上看到这个例子后,我研究了KnockoutJS:以及当你选择一个类别时,它旁边的下拉列表中的产品是如何变化的。这个方法目前还可以工作,但无法从示例中看出它是如何将数据拉入的 最后,我希望有两个视图或一

我想让用户通过下拉列表或场馆地图选择座位区[现在就说下拉列表] 在用户选择座位区并点击提交按钮后,我想加载另一个视图,其中包含他们选择的座位区的表格

这是数据模型的一部分。EDMX用于座位区和桌子[桌子],只是为了向您展示已设置的关系

我尝试了MVC3期货序列化-无法按我想要的方式工作

在他们的网站上看到这个例子后,我研究了KnockoutJS:以及当你选择一个类别时,它旁边的下拉列表中的产品是如何变化的。这个方法目前还可以工作,但无法从示例中看出它是如何将数据拉入的

最后,我希望有两个视图或一个视图,仅隐藏座位区选择并显示桌子选择

Razor视图-用户将单击[分配表]进入第一个视图1

视图1:选择座位区 [控制器代码]

使用下拉列表生成此razor视图的:

我最终会把这张地图做成一张场地地图,显示不同的楼层和座位区

在他们选择座位区之后。。。我想重定向到另一个视图,保留上一个视图中的数据SeatingAreaID,或者隐藏座位区选择并显示该座位区中的表格

[视图2的部分控制器代码]

请让我知道什么是最好的方式来处理这个。我相信我会不止一次使用这个。知道怎么做可能很有价值

这可能是一件很容易做到的事情,但我在任何地方都找不到任何我想要的方法。SeatingAreaID并不是我想要在视图之间传输的唯一变量,我还需要保持VenueID和VipServiceID的传输,在选择表的最后,控制器将在表和VipService之间创建多对多关系,并重定向回VipService详细信息页面

谢谢你的时间和帮助。
Tim

首先,我强烈建议您停止使用ViewData dictionary属性将数据从控制器传递到视图。任何时候都可以使用ViewData,也可以使用ViewModel。使用ViewModel,编译器可以帮助您,而不仅仅是将键传递到字典中

可以在这里找到一本好的读物:


在熟悉使用ViewModel模式后,您应该能够轻松地将数据从一个视图移动到另一个视图。

我继续了,只是用我所知道的唯一方法,可能不是最好的方法,但它起了作用。如果有人有更好的方法来实现这一点,请告诉我

我所做的:

用户选择“添加表格”按钮后

[控制器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重定向到Action 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]

最后,我将把下拉列表变成场馆的视觉效果,这样用户只需点击他们想要分配的区域和桌子

再一次,如果有人有更好的解决方案,请让我知道


谢谢。

我已经有了ViewModel设置,我不知道那篇文章与我的要求有什么关系。
@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>
[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);
}
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();
        }
@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>
[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);
            }

        }