asp.net-mvc MVC4 将模型从视图传递到控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16339398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MVC4 Passing model from view to controller
提问by Trevor Daniel
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 :(
但是 theRoute 总是空的 :(
回答by HennyH
I hope this complete example will help you.
我希望这个完整的例子会对你有所帮助。
This is the TaxiInfoclass 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 Listof TaxiInfo(s):
我们也有拥有一个方便的模型列表的TaxiInfo(S) :
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 Indexaction which for this example makes two taxi drivers and adds them to the list contained in a TaxiInfo:
现在在 home 控制器中,我们有默认的Index动作,在这个例子中,它创建了两个出租车司机,并将他们添加到 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: [email protected]</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 ActionLinkis responsible for the re-directing to the booking action of the Homecontroller (and passing in the appropriate TaxiInfo object) which is defiend as follows:
的ActionLink的负责重新定向到的预约动作主页控制器(和传递适当TaxiInfo对象),它是defiend如下:
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 [email protected]</h1>
A visual tour:
视觉之旅:





