Generate list in view and pass it to controller

2020-07-26 09:00发布

问题:

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?

回答1:

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