生成列表视图中,并把它传递到控制器 [英] Generate list in view and pass it to controller

查看:145
本文介绍了生成列表视图中,并把它传递到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写的酒店预订网站,与Asp.net和MVC 4。
它有一个类名为保留具有的联系人列表。在创建视图我想动态创建联系人。 (触点=成人+孩子数,它会在确定创建预订视图)
我怎么可能发布的联系人信息到控制器?

 公共部分类预订
{
    公众诠释标识{搞定;组; }
    公众诠释RoomType_Id {搞定;组; }
    公共System.DateTime的寄件者{搞定;组; }
    公众的System.DateTime TODATE {搞定;组; }
    公共字节成人{搞定;组; }
    公共字节孩子{搞定;组; }
    公共十进制价格{搞定;组; }
    公众诠释USER_ID {搞定;组; }
    公众诠释STATE_ID {搞定;组; }
    公共虚拟ReservationState ReservationState {搞定;组; }
    公共虚拟RoomType RoomType {搞定;组; }
    公共虚拟用户用户{搞定;组; }
    公共虚拟的ICollection<&交易GT;交易{搞定;组; }
    公共虚拟的ICollection<联系与GT;联系方式{搞定;组; }
}

我应该设置一个最大数量的联系人(例如5,然后写这样的事?

  [HttpPost]
    公众的ActionResult创建(预约预订,联系Adult1,联系Adult2,联系Adult3,联系Adult4,联系Adult5,联系Kid1,联系KID2,联系Kid3)
    {
        如果(reservation.Adults大于0)
            reservation.Contacts.Add(Adult1);
        如果(reservation.Adults→1)
            reservation.Contacts.Add(Adult2);
        ...
        如果(ModelState.IsValid)
        {
            _db.Reservations.Add(预订);
            _db.SaveChanges();
            返回RedirectToAction(「指数」);
        }
    }

这是非常脏的是有没有更好的办法?我可以通过联系人列表?


解决方案

  @for(VAR I = 0; I< Model.Contacts.Count();我++)
{
    @ Html.EditorFor(M = GT; m.Contacts [I])
}

您需要做的唯一的事情就是实例化的新的联系人列表。这就是为什么一个视图模型为preferable,你可以简单地做在你的视图模型基于一定的价值构造:

 公共类ReservationViewModel
{
    公共ReservationViewModel()
    {
        联系方式=新的List<联系与GT;();
        对于(VAR I = 0; I<成人+儿童;我++)
        {
            Contacts.Add(新联系());
        }
    }    ...
}

另外,以后你看到获取生成你就会明白了ModelBinder的期望如何接收数据后面的code。您的投入将是这样的:

 <输入ID =Contacts_0__NameNAME =联系人[0] .Name点/>

其中, 0 是接触的迭代次数。如果你手动模拟这种结构,ModelBinder的会拿起数据一样好。

I'm writing a hotel reservation web site with Asp.net and MVC 4. it have a class named reservation which have a list of contacts. in create view i want to dynamically create contacts. (number of contacts = adults + kids and it will be determined in create reservation view) how could i post contact information to controller?

public partial class Reservation
{
    public int Id { get; set; }
    public int RoomType_Id { get; set; }
    public System.DateTime FromDate { get; set; }
    public System.DateTime ToDate { get; set; }
    public byte Adults { get; set; }
    public byte Kids { get; set; }
    public decimal Price { get; set; }
    public int User_Id { get; set; }
    public int State_Id { get; set; }
    public virtual ReservationState ReservationState { get; set; }
    public virtual RoomType RoomType { get; set; }
    public virtual User User { get; set; }
    public virtual ICollection<Transaction> Transactions { get; set; }
    public virtual ICollection<Contact> Contacts { get; set; }
}

should i set a maximum number for contacts(for example 5 and then write something like this?

    [HttpPost]
    public ActionResult Create(Reservation reservation,Contact Adult1,Contact Adult2, Contact Adult3, Contact Adult4, Contact Adult5, Contact Kid1,Contact Kid2, Contact Kid3)
    {
        if(reservation.Adults>0)
            reservation.Contacts.Add(Adult1);
        if(reservation.Adults>1)
            reservation.Contacts.Add(Adult2);
        ...
        if (ModelState.IsValid)
        {
            _db.Reservations.Add(reservation);
            _db.SaveChanges();
            return RedirectToAction("Index");
        }
    }

it's very dirty is there a better way? can i pass list of contacts?

解决方案

@for (var i = 0; i < Model.Contacts.Count(); i++)
{
    @Html.EditorFor(m => m.Contacts[i])
}

The only thing you need to do is instantiate a list of new Contacts. This is why a view model is preferable as you could simply do this in the constructor based upon some value on your view model:

public class ReservationViewModel
{
    public ReservationViewModel()
    {
        Contacts = new List<Contact>();
        for (var i = 0; i < Adults + Kids; i++)
        {
            Contacts.Add(new Contact());
        }
    }

    ...
}

Alternatively, after you see the code that gets generated you'll understand how the modelbinder expects to receive the data back. Your inputs will look like:

<input id="Contacts_0__Name" name="Contacts[0].Name" />

Where 0 is the iteration count of contacts. If you simulate this structure manually, the modelbinder will pick up the data just as well.

这篇关于生成列表视图中,并把它传递到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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