mvc在一个页面中使用多个表单,通过选择单选按钮进行过滤

时间:2016-02-29 21:15:01

标签: c# asp.net-mvc radiobuttonlist

我有一个包含3个表单的页面,每个表单都会显示,具体取决于上一个表单中单选按钮的选定值(我使用viewbag控制可见性希望找到更好的东西),所有表单都使用相同的视图是否可以将3个表单发布到同一个操作并使ViewModel保存所有选定的值以发送到另一个页面甚至存储在数据库中?我试图这样做,在每个帖子之后,旧的选定值在ViewModel中变为null。 我也尝试过发布here的解决方案,但仍然无法在ViewModel中保存所有选定的值,那么将所选值存储到以后使用的最佳方法是什么?

RouteList.cs我的ViewModel

    public class RoutesList
{
    public int ID { get; set; }
    public List<Route> Routes
    {
        get
        {
            Queries.BookingHandler handler = new Queries.BookingHandler();
            return handler.GetAllRoutes();
        }
        set { }
    }
    public List<Route> OppositeRoutes
    {
        get
        {
                Queries.BookingHandler handler = new Queries.BookingHandler();
                return handler.GetRoutDirections(long.Parse(SelectedRouteID));

        }
        set { }
    }
    public List<RouteStation> RouteStations
    {
        get
        {
                Queries.BookingHandler handler = new Queries.BookingHandler();
                return handler.GetRouteStations(long.Parse(SelectedOppositeRouteID));
        }
        set { }
    }
        public string SelectedRouteID { get; set; }
    public string SelectedOppositeRouteID { get; set; }
    public string SelectedFromStationID { get; set; }
    public string SelectedToStationID { get; set; }
    public int Count { get; set; }
}

我的Controller有Get和post

的索引操作
 public ActionResult Index()
    {
        return View(new RoutesList());
    }


[HttpPost]
      public ActionResult Index(RoutesList route, FormCollection frm)
    {

        if (!string.IsNullOrEmpty(route.SelectedRouteID))
            ViewBag.isDirection = true;
        if (!string.IsNullOrEmpty(route.SelectedOppositeRouteID))
            ViewBag.isStations = true;
        if(!string.IsNullOrEmpty(route.SelectedFromStationID)&&!string.IsNullOrEmpty(route.SelectedToStationID))
            return RedirectToAction("Index", "Time", new { id = route.SelectedRouteID });

        return View(route);

    }

和我的View Index.cshtml

@model BusStarBackend.Models.RoutesList
    @using (Html.BeginForm())
    {
        <table class="table table-hover">
            <tr>
                <th>Trips</th>
                <th>Price</th>
            </tr>
            @foreach (var item in Model.Routes)
            {
                <tr>
                    <td>
                        @Html.RadioButtonFor(m => m.SelectedRouteID, item.RouteID)
                        @Html.DisplayFor(model => item.RouteName)
                    </td>
                    <td>@Html.DisplayFor(m => item.TicketPrice)</td>
                    <td> </td>
                </tr>
            }
        </table>
        <input type="submit" value="next" class="btn btn-default" />
    }
@{
    using (Html.BeginForm())
    {
        if (ViewBag.isDirection != null && ViewBag.isDirection)
        {
            <table class="table">
                <tr>
                    <th>
                        Please selected your Direction
                    </th>
                    <th></th>
                </tr>

                @foreach (var item in Model.OppositeRoutes)
                {
                    <tr>
                        <td>
                            @Html.RadioButtonFor(m => Model.SelectedOppositeRouteID, item.RouteID)
                            @Html.DisplayFor(modelItem => item.RouteName)
                        </td>
                    </tr>
                }
            </table>
            <input type="submit" value="next" class="btn btn-default" />
        }
    }
}
@{
    if (ViewBag.isStations != null && ViewBag.isStations)

    {
        using (Html.BeginForm())
        {
            <div id="stations">

                <table class="table">
                    <tr>
                        <th>
                            From Station
                        </th>
                        <th>
                            To Station
                        </th>
                        <th></th>
                    </tr>

                    @foreach (var item in Model.RouteStations)
                    {
                        <tr>
                            <td>
                                @Html.RadioButtonFor(model => model.SelectedFromStationID, item.StationID)
                                @Html.DisplayFor(modelItem => item.Station.Name)
                            </td>
                            <td>
                                @Html.RadioButtonFor(model => model.SelectedToStationID, item.StationID)
                                @Html.DisplayFor(modelItem => item.Station.Name)
                            </td>
                        </tr>
                    }
                </table>
                <input type="submit" value="next" class="btn btn-default" />
            </div>
        }
    }
}

1 个答案:

答案 0 :(得分:1)

对于最简单的&#34;修复,你应该使用你的&#34; if&#39; s&#34;在开始形式之前,这样的事情:

@model BusStar.Models.RoutesList
@if (ViewBag.isDirection != null && ViewBag.isDirection)
   {...
    @using (Html.BeginForm())
    {.....

为了获得更好的解决方案,您应该使用&#39; Html.Action&#39;方法,构建一个包含每个表单的局部视图,并根据单选按钮的值仅渲染所需的表单。

这样的事情:

每个表单的2个局部视图 - 这是针对方向形式:(称为_directionForm.cshtml)

@model BusStar.Models.RoutesList
<table class="table">
<tr>
    <th>
       Please selected your Direction
    </th>
    <th></th>
    </tr>
    @foreach (var item in Model.OppositeRoutes)
    {
    <tr>
        <td>
            @Html.RadioButtonFor(m => Model.SelectedOppositeRouteID, item.RouteID)
            @Html.DisplayFor(modelItem => item.RouteName)
        </td>
    </tr>
    }
</table>
<input type="submit" value="next" class="btn btn-default" />

你的控制器:

public ActionResult Index()
    {
        return View(new RoutesList());
    }

public ActionResult PartialForm(RoutesList route)
{
   if (!string.IsNullOrEmpty(route.SelectedRouteID))
   return view("__directionForm", route);

   return view("...", route); //your other view
}

[HttpPost]
      public ActionResult Index(RoutesList route, FormCollection frm)
    {

        //if (!string.IsNullOrEmpty(route.SelectedRouteID)) ViewBag.isDirection = true;
        //if (!string.IsNullOrEmpty(route.SelectedOppositeRouteID)) ViewBag.isStations = true;
        if(!string.IsNullOrEmpty(route.SelectedFromStationID)&&!string.IsNullOrEmpty(route.SelectedToStationID))
            return RedirectToAction("Index", "Time", new { id = route.SelectedRouteID });

        return View(route);

    }

在您的旧视图中,将以下两种形式替换为:

Html.Action("PartialForm", Model)