0

我有以下查看代码(ASP:NET MVC 3):

@using(Html.BeginForm()){
@Html.ValidationSummary(true)
<table>
    <tr>
        <th>
            City
        </th>
        <th>
            Hotel
        </th>
        <th>
            Room name
        </th>
        <th>
            Number of beds
        </th>
        <th>
            Price per night
        </th>

        <th></th>
    </tr>

    <tr>
        <td>
            @Html.DisplayFor(x=>Model.SelectedRoom.Hotel.City.Name)
        </td>
        <td>
            @Html.DisplayFor(x => Model.SelectedRoom.Hotel.Name)
        </td>
        <td>
            @Html.DisplayFor(x => Model.SelectedRoom.Name)
        </td>
        <td>
            @Html.DisplayFor(x => Model.SelectedRoom.NumberOfBeds)
        </td>
        <td>
            @Html.DisplayFor(x => Model.SelectedRoom.PricePerNight)
        </td>

    </tr>
</table>
            <input type="hidden" name="RoomID" value="@Model.SelectedRoom.RoomID" id="RoomID" />
            <br />
            <br />
            <p>
                From: @Html.TextBoxFor(m=>m.DateOne, new {@class="datepicker1", @readonly="readonly" }) 
                @Html.ValidationMessageFor(m=>m.DateOne)

                To: @Html.TextBoxFor(m => m.DateTwo, new { @class = "datepicker2", @readonly = "readonly" })
                @Html.ValidationMessageFor(m=>m.DateTwo)

            </p>
            <p>
                Total cost : @Html.TextBoxFor(m=>m.TotalCost, new {@class="TotalAmount", @readonly="readonly" })
            </p>
              <br />
           <input type="button" id="Reserve" value="Confirm" onclick = "window.location.href='/Home/MakeReservation'+@Model" />
    }

这是我对此视图的模型定义:

public class MakeReservationViewModel
    {
        BusinessLogic bl;

        public MakeReservationViewModel() 
        {
            bl = new BusinessLogic();
        }

        public Room SelectedRoom { get; set; }


        [Required(ErrorMessage="Please select a starting date")]
        public DateTime DateOne { get; set; }

        [Required(ErrorMessage = "Please select an ending date")]  
        public DateTime DateTwo { get; set; }

        public decimal TotalCost { get; set; }
    }

如何通过单击按钮并将其 onclick 属性正确设置为 MakeReservation 从上面的视图中调用 * MakeReservation控制器操作?* 这是我的控制器:

[HttpPost]
    public ViewResult MakeReservation(MakeReservationViewModel mrvm)
    {
        if (ModelState.IsValid)
        {

        }

        return View();
    }

这是我的错误:对象引用未设置为对象的实例。在这条线上: 如何让模型接受隐藏场?

解决了。我补充说:

@Html.TextBoxFor(m => m.SelectedRoom.RoomID, new {@class="RoomID", @hidden="hidden" })

而不是以前使用非强类型属性的方式。

4

0 回答 0