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

mvc use more than one form in one page filtered with the selection of radio Button

我有一个包含 3 个表单的页面,每个表单的显示取决于前一个表单中单选按钮的选定值(我使用 viewbag 来控制可见性希望找到更好的东西)并且所有表单都使用相同的视图模型。是否有可能 post 3 个表单执行相同的操作并使 ViewModel 保存所有选定的值以发送到另一个页面甚至存储在数据库中?我尝试这样做,在每个 post 之后,旧的选定值在 ViewModel 中变为 null。 我还尝试了 posted 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; }
}

并且我的控制器对 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);

    }

和我的观点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>
        }
    }
}

对于 "easiest" 修复,您应该在开始表单之前使用 "if's",如下所示:

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

为了更好的解决方案,您应该使用 'Html.Action' 方法,构建一个包含您的每个表单的局部视图,并根据单选按钮的值仅呈现您需要的那个。

像这样:

每种形式的 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)