我有一个包含3个窗体的页面根据前一窗体中单选按钮的选定值显示每个窗体(我使用viewbag控制可见性希望找到更好的东西),并且所有表单都使用相同的视图模型。可以将3个表单发布到同一个动作,并使ViewModel将所有选定的值保存到另一个页面,甚至存储在数据库中?我试图这样做后,每个帖子后,旧的选定值在ViewModel中成为空。 我也尝试过发布here的解决方案,但仍然无法将ViewModel中的所有选定值保存在一起,那么将我的选定值存储以便以后使用的最佳方法是什么?mvc在一个页面中使用多个表单使用单选按钮进行过滤按钮
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>
}
}
}
对不起,我是mvc的初学者,你能帮助我如何根据单选按钮的值渲染我的局部视图吗? – EACoder
这里有很多路径可供选择,我会试着展示一个更适合初学者的方法。我建议你在使用ajax寻找解决方案之后。 –
我按照你所说的做了,但是在PartialForm中,你希望我使用其他视图呈现其他视图吗?如果是这样,我必须做出另一个条件,所以我可以在这里默认返回什么? – EACoder