MVC4 将模型从视图传递到控制器 [英] MVC4 Passing model from view to controller

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

问题描述

我有一个模型视图,其中填充了与预订出租车相关的数据.

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

在模型中是一个包含时间、价格、车辆类型的报价列表,其中我显示了一个使用 foreach 的列表.每次 foreah 循环时,它都会构建一个表单和一个提交按钮,将我带到控制器中的BookingStage1"操作.我还添加了一个隐藏字段,其中填充了特定报价的预订参考.

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)List:

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

现在在 home 控制器中,我们有默认的 Index 操作,在本例中,它创建了两个出租车司机并将他们添加到 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));
}

视图的代码如下:

@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 负责重定向到 Home 控制器的预订操作(并传入适当的 TaxiInfo 对象),其定义如下:

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天全站免登陆