I have a view with a model populated with data relating to booking a taxi.
我有一个模型的视图,其中包含有关预订出租车的数据。
In the model is a list of quotations with time, price, vehicle type in it which I display a list of using a foreach. Each time the foreah loops it builds a form and a submit button to take me to the "BookingStage1" action in the controller. I have also added a hidden field which is populated with the bookingrefernce for the particular quotation.
在模型中是一个带有时间,价格,车辆类型的报价列表,其中显示了使用foreach的列表。每次foreah循环时,它会构建一个表单和一个提交按钮,以便将我带到控制器中的“BookingStage1”操作。我还添加了一个隐藏字段,其中填充了特定报价的预订信息。
So, I was hoping that when it hit the action result in my controller that the model would be returned fully populated just like it was with the view. But it's null, no data in it whatsoever.
所以,我希望当它在我的控制器中击中动作结果时,模型将完全填充,就像它与视图一样。但它是空的,没有任何数据。
I was hoping to pass the populated model between a number of controllers as the user progresses through the various search, results and booking screens...
当用户通过各种搜索,结果和预订屏幕进展时,我希望能够在多个控制器之间传递填充模型...
Is it possible to pass the fully populated model back from the view into the next controller?
是否可以将完全填充的模型从视图传递回下一个控制器?
Thanks
谢谢
In my Search Results page I have the following form:
在我的搜索结果页面中,我有以下表格:
using (Html.BeginForm("BookingPage1", "SearchResults", FormMethod.Post))
I also have a hidden field in the form as below:
我在表单中也有一个隐藏字段如下:
<input type="hidden" id="BookingID" name="ChosenBookingID" value='@item.QuotationID' />
which posts to my controller which looks like this:
发布到我的控制器,看起来像这样:
[HttpPost]
public ActionResult BookingPage1(string ChosenBookingID, Route theRoute)
{
//this does noting yet.
return View();
}
But theRoute is always empty :(
但是路径总是空的:(
1 个解决方案
#1
32
I hope this complete example will help you.
我希望这个完整的例子可以帮到你。
This is the TaxiInfo class which holds information about a taxi ride:
这是TaxiInfo类,其中包含有关乘坐出租车的信息:
namespace Taxi.Models
{
public class TaxiInfo
{
public String Driver { get; set; }
public Double Fare { get; set; }
public Double Distance { get; set; }
public String StartLocation { get; set; }
public String EndLocation { get; set; }
}
}
We also have a convenience model which holds a List of TaxiInfo(s):
我们还有一个便利模型,其中包含TaxiInfo列表:
namespace Taxi.Models
{
public class TaxiInfoSet
{
public List<TaxiInfo> TaxiInfoList { get; set; }
public TaxiInfoSet(params TaxiInfo[] TaxiInfos)
{
TaxiInfoList = new List<TaxiInfo>();
foreach(var TaxiInfo in TaxiInfos)
{
TaxiInfoList.Add(TaxiInfo);
}
}
}
}
Now in the home controller we have the default Index action which for this example makes two taxi drivers and adds them to the list contained in a TaxiInfo:
现在在家庭控制器中我们有默认的索引操作,对于这个例子,它会生成两个出租车司机并将它们添加到TaxiInfo中包含的列表中:
public ActionResult Index()
{
var taxi1 = new TaxiInfo() { Fare = 20.2, Distance = 15, Driver = "Billy", StartLocation = "Perth", EndLocation = "Brisbane" };
var taxi2 = new TaxiInfo() { Fare = 2339.2, Distance = 1500, Driver = "Smith", StartLocation = "Perth", EndLocation = "America" };
return View(new TaxiInfoSet(taxi1,taxi2));
}
The code for the view is as follows:
该视图的代码如下:
@model Taxi.Models.TaxiInfoSet
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@foreach(var TaxiInfo in Model.TaxiInfoList){
<form>
<h1>Cost: $@TaxiInfo.Fare</h1>
<h2>Distance: @(TaxiInfo.Distance) km</h2>
<p>
Our diver, @TaxiInfo.Driver will take you from @TaxiInfo.StartLocation to @TaxiInfo.EndLocation
</p>
@Html.ActionLink("Home","Booking",TaxiInfo)
</form>
}
The ActionLink is responsible for the re-directing to the booking action of the Home controller (and passing in the appropriate TaxiInfo object) which is defiend as follows:
ActionLink负责重定向Home控制器的预订操作(并传入相应的TaxiInfo对象),该操作如下所示:
public ActionResult Booking(TaxiInfo Taxi)
{
return View(Taxi);
}
This returns a the following view:
这将返回以下视图:
@model Taxi.Models.TaxiInfo
@{
ViewBag.Title = "Booking";
}
<h2>Booking For</h2>
<h1>@Model.Driver, going from @Model.StartLocation to @Model.EndLocation (a total of @Model.Distance km) for $@Model.Fare</h1>
A visual tour:
视觉游览:
#1
32
I hope this complete example will help you.
我希望这个完整的例子可以帮到你。
This is the TaxiInfo class which holds information about a taxi ride:
这是TaxiInfo类,其中包含有关乘坐出租车的信息:
namespace Taxi.Models
{
public class TaxiInfo
{
public String Driver { get; set; }
public Double Fare { get; set; }
public Double Distance { get; set; }
public String StartLocation { get; set; }
public String EndLocation { get; set; }
}
}
We also have a convenience model which holds a List of TaxiInfo(s):
我们还有一个便利模型,其中包含TaxiInfo列表:
namespace Taxi.Models
{
public class TaxiInfoSet
{
public List<TaxiInfo> TaxiInfoList { get; set; }
public TaxiInfoSet(params TaxiInfo[] TaxiInfos)
{
TaxiInfoList = new List<TaxiInfo>();
foreach(var TaxiInfo in TaxiInfos)
{
TaxiInfoList.Add(TaxiInfo);
}
}
}
}
Now in the home controller we have the default Index action which for this example makes two taxi drivers and adds them to the list contained in a TaxiInfo:
现在在家庭控制器中我们有默认的索引操作,对于这个例子,它会生成两个出租车司机并将它们添加到TaxiInfo中包含的列表中:
public ActionResult Index()
{
var taxi1 = new TaxiInfo() { Fare = 20.2, Distance = 15, Driver = "Billy", StartLocation = "Perth", EndLocation = "Brisbane" };
var taxi2 = new TaxiInfo() { Fare = 2339.2, Distance = 1500, Driver = "Smith", StartLocation = "Perth", EndLocation = "America" };
return View(new TaxiInfoSet(taxi1,taxi2));
}
The code for the view is as follows:
该视图的代码如下:
@model Taxi.Models.TaxiInfoSet
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@foreach(var TaxiInfo in Model.TaxiInfoList){
<form>
<h1>Cost: $@TaxiInfo.Fare</h1>
<h2>Distance: @(TaxiInfo.Distance) km</h2>
<p>
Our diver, @TaxiInfo.Driver will take you from @TaxiInfo.StartLocation to @TaxiInfo.EndLocation
</p>
@Html.ActionLink("Home","Booking",TaxiInfo)
</form>
}
The ActionLink is responsible for the re-directing to the booking action of the Home controller (and passing in the appropriate TaxiInfo object) which is defiend as follows:
ActionLink负责重定向Home控制器的预订操作(并传入相应的TaxiInfo对象),该操作如下所示:
public ActionResult Booking(TaxiInfo Taxi)
{
return View(Taxi);
}
This returns a the following view:
这将返回以下视图:
@model Taxi.Models.TaxiInfo
@{
ViewBag.Title = "Booking";
}
<h2>Booking For</h2>
<h1>@Model.Driver, going from @Model.StartLocation to @Model.EndLocation (a total of @Model.Distance km) for $@Model.Fare</h1>
A visual tour:
视觉游览: