Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将数据从ViewModel传递到控制器_C#_Asp.net_Asp.net Mvc_Viewmodel - Fatal编程技术网

C# 将数据从ViewModel传递到控制器

C# 将数据从ViewModel传递到控制器,c#,asp.net,asp.net-mvc,viewmodel,C#,Asp.net,Asp.net Mvc,Viewmodel,我试图将viewmodel的数据从表单发布到控制器上的操作方法,但它总是空的 我的看法是: @model HotelSystem.ViewModels.UpdateReservationVM @{ ViewBag.Title = "Update Reservation"; } <h2>Update Reservation</h2> @using (Html.BeginForm("Update", "Reservations", FormMethod.Post, ne

我试图将viewmodel的数据从表单发布到控制器上的操作方法,但它总是空的

我的看法是:

@model HotelSystem.ViewModels.UpdateReservationVM

@{
ViewBag.Title = "Update Reservation";
}

<h2>Update Reservation</h2>

@using (Html.BeginForm("Update", "Reservations", FormMethod.Post, new { @class = "form-horizontal" }))
{


<div class="form-horizontal">

    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(Model => Model.Reservation.Guest.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.HiddenFor(Model => Model.Reservation.ReservationID)
            @Html.EditorFor(model => model.Reservation.Guest.FirstName, new { htmlAttributes = new { @class = "form-control", @id = "ReservationID", @disabled = true } })
            @Html.ValidationMessageFor(model => model.Reservation.Guest.FirstName, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Reservation.Guest.MiddleName, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Reservation.Guest.MiddleName, new { htmlAttributes = new { @class = "form-control", @id = "Arrival", @disabled = true } })
            @Html.ValidationMessageFor(model => model.Reservation.Guest.MiddleName, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Reservation.Guest.LastName, htmlAttributes: new { @class = "control-label col-md-2", @id = "Departure" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Reservation.Guest.LastName, new { htmlAttributes = new { @class = "form-control", @disabled = true } })
            @Html.ValidationMessageFor(model => model.Reservation.Guest.LastName, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Reservation.Guest.PhoneNo, "Phone Number", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Reservation.Guest.PhoneNo, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Reservation.Guest.PhoneNo, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Reservation.Guest.MobileNo, "Mobile #", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Reservation.Guest.MobileNo, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Reservation.Guest.MobileNo, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Reservation.Guest.HomeAddress, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Reservation.Guest.HomeAddress, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Reservation.Guest.HomeAddress, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Reservation.Guest.Occupation, "Occupation", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Reservation.Guest.Occupation, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Reservation.Guest.Occupation, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Reservation.VehiclePlateNo, "Vehicle Plate #", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Reservation.VehiclePlateNo, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Reservation.VehiclePlateNo, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Reservation.Guest.TitleID, "TitleID", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("TitleID", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Reservation.Guest.TitleID, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Reservation.Guest.CountryID, "CountryID", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("CountryID", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Reservation.Guest.CountryID, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Reservation.Guest.NationalityID, "NationalityID", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("NationalityID", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Reservation.Guest.NationalityID, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Reservation.Guest.VIPID, "VIPID", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("VIPID", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Reservation.Guest.VIPID, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Reservation.AgencyID, "AgencyID", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("AgencyID", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Reservation.AgencyID, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Reservation.GroupProfileID, "GroupProfileID", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("AgencyID", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Reservation.GroupProfileID, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Reservation.Guest.CompanyID, "Commpany", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("CompanyID", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Reservation.Guest.CompanyID, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Reservation.GroupProfileID, "Group Profile", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("GroupProfileID", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Reservation.GroupProfileID, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Reservation.SourceID, "SourceID", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("SourceID", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Reservation.SourceID, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Reservation.Guest.EmailAdd, "Email Address", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Reservation.Guest.EmailAdd, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Reservation.Guest.EmailAdd, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        <label class="control-label col-md-2">Room Information</label>


    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.Reservation.Arrival, "Arrival", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Reservation.Arrival, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Reservation.Arrival, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.Reservation.Departure, "Departure", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Reservation.Departure, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Reservation.Departure, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        <label class="control-label col-md-2">Nigt(s)</label>
        <div class="col-md-1">
            <input class="form-control" id="nights" />
         </div>
        <label class="control-label col-md-2">No. Of Room</label>
        <div class="col-md-1">
            @Html.EditorFor(model => model.Reservation.NoOfRooms, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Reservation.NoOfRooms, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.Reservation.NoOfAdults, "No of Adults", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-1">
            @Html.EditorFor(model => model.Reservation.NoOfAdults, new { htmlAttributes = new { @class = "form-control", @id = "NoOfAdults" } })
            @Html.ValidationMessageFor(model => model.Reservation.NoOfAdults, "", new { @class = "text-danger" })
        </div>
        @Html.LabelFor(model => model.Reservation.NoOfChild, "Child", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-1">
            @Html.EditorFor(model => model.Reservation.NoOfChild, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Reservation.NoOfChild, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.Room.RoomTypeID, "RoomTypeID", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-2">
            @Html.DropDownList("RoomTypeID", null, htmlAttributes: new { @class = "form-control", @id = "RoomTypeID" })
            @Html.ValidationMessageFor(model => model.Room.RoomTypeID, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.Reservation.RateID, "Rate", htmlAttributes: new { @class = "control-label col-md-1" })
        <div class="col-md-2">
            @Html.DropDownList("RateID", null, htmlAttributes: new { @class = "form-control", @id = "RateID" })
            @Html.ValidationMessageFor(model => model.Reservation.RateID, "", new { @class = "text-danger" })
        </div>
        @Html.LabelFor(model => model.Room.RoomID, "RoomID", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-1">
            @Html.DropDownList("RoomID", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Room.RoomID, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.Reservation.Rate, "Rate", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Reservation.Rate, new { htmlAttributes = new { @class = "form-control", @id = "Rate" } })
            @Html.ValidationMessageFor(model => model.Reservation.Rate, "", new { @class = "text-danger" })
        </div>
    </div>



    <div class="form-group">
        @Html.LabelFor(model => model.Reservation.ReservationTypeID, "ReservationTypeID", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("ReservationTypeID", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Reservation.ReservationTypeID, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Reservation.GroupProfileID, "GroupProfile", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("GroupProfileID", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Reservation.GroupProfileID, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Reservation.Guest.TitleID, "TitleID", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("TitleID", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Reservation.Guest.TitleID, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Reservation.Guest.NationalityID, "NationalityID", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("NationalityID", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Reservation.Guest.NationalityID, "", new { @class = "text-danger" })
        </div>
    </div>



    <div class="form-group">
        @Html.LabelFor(model => model.Payment.PaymentTypeID, "PaymentTypeID", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("PaymentTypeID", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Payment.PaymentTypeID, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.Payment.CardID, "CardTypeID", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("CardTypeID", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Payment.CardID, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.Payment.BookBy, "BookBy", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("BookBy", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Payment.BookBy, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" onclick="return confirm('Are you sure you want to proceed transaction?');" />
            <input type="button" value="Options" class="btn btn-default" id="alert" />
            <a href="#" data-bb="alert_callback" class="btn btn-default" id="alert2">Options</a>

        </div>
    </div>
</div>
}
这是我的视图模型:

public class UpdateReservationVM
{
    public virtual Reservation Reservation { get; set; }

    public virtual Payment Payment { get; set; }

    public virtual FlightInfo Flight { get; set; }

    public virtual RoomReservation Room { get; set; }


    public virtual Transfer Transfer { get; set; }
}
这个代码有什么问题?试图调试它时,它总是转到控制器的action方法,但传递的值都是NULL

试试

[HttpPost]
public ActionResult Update(ViewModels.UpdateReservationVM reservation)
{
    if (ModelState.IsValid)
    {
        db.Reservation.Attach(UpdateReservationVM.reservation);
        db.Entry(UpdateReservationVM.reservation).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(reservation);
}
调试时,请检查保留(小写)是否不为null

另一方面,您可能需要更改

public ActionResult Update(ViewModels.UpdateReservationVM reservation)

并在控制器顶部添加using语句,以包括viewmodel命名空间

using YOURPROJECTNAME.NAMEOFVIEWMODELSFOLDER;
所以,在我的情况下

using salesWebsite.viewModel;

在您的表单中添加antiforgerytoken:

@using (Html.BeginForm("Update", "Reservations", FormMethod.Post, new { @class = "form-horizontal" }))
{
    @Html.AntiForgeryToken()
    @*
        rest of code
    *@
}

修改了我的问题,@DLeh well付款、航班、房间和转机在我提交form@LuigiVibal-如果执行此操作,还需要在控制器中[HttpPost]的正下方添加[ValidateAntiForgeryToken]。@tintyethan仍然不工作您是否按照我在上面的完整回复中所说的做了?添加antiforgerytoken是一种很好的做法,但我认为它与空表单值无关。
using salesWebsite.viewModel;
@using (Html.BeginForm("Update", "Reservations", FormMethod.Post, new { @class = "form-horizontal" }))
{
    @Html.AntiForgeryToken()
    @*
        rest of code
    *@
}