MVC4从视图中传递模式控制器 [英] MVC4 Passing model from view to controller

查看:112
本文介绍了MVC4从视图中传递模式控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有填充与预约出租车数据模型的视图。

I have a view with a model populated with data relating to booking a taxi.

在该模型是在它的时间,价格,车辆类型报价我显示使用foreach的列表的列表。每个foreah循环时间就建立了一个表格和一个提交按钮带我到控制器中的BookingStage1行动。我还添加了填充了bookingrefernce为特定的报价隐藏字段。

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.

所以,我希望,当它击中在我的控制器的操作结果,该模型将全额返还填充就像它的观点。但它空,没有数据在里面任何责任。

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?

感谢

在我的搜索结果页面上,我有以下形式:

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();
    }

但theRoute总是空:(

But theRoute is always empty :(

推荐答案

我希望这个完整的例子会帮助你。

I hope this complete example will help you.

这是在 TaxiInfo 类持有约乘坐出租车信息:

This is the TaxiInfo class which holds information about a taxi ride:

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; }
    }
}

我们还必须持有 TaxiInfo的列表(S)的一个便捷模型

We also have a convenience model which holds a List of 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);
            }
        }
    }
}

现在在家居控制器,我们有默认的首页的行动在这个示例中提出了两个出租车司机,并将它们添加到包含在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:

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));
}

在code的观点如下:

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>
}

ActionLink的负责重新定向到首页控制器的预订行为(和传递适当的TaxiInfo对象),这是defiend如下:

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:

    public ActionResult Booking(TaxiInfo Taxi)
    {
        return View(Taxi);
    }

这将返回以下观点:

@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>

一个视觉之旅:

这篇关于MVC4从视图中传递模式控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆