1

この質問はあちこちで繰り返し見られるかもしれませんが、答えを見つけることができませんでした。

私のプロジェクトはホテルの予約に関するものです。クラス Reservation witch には、ChoosenRooms の Icollection と、予約を行うユーザーを表すクラス、および日付やその他のものがあります。

プロセスは次のとおりです。

最初のビューでは、選択した部屋、日付などを取得し、それを 2 番目のビューに渡し、ユーザー情報を取得します。次に、収集したすべての情報を表示する 3 番目のビューがあるので、ユーザーは最終的にボタンをクリックしてデータを保存できます。

私の問題は、これらすべてのビューで予約オブジェクト クラスを渡す必要があることです。私のテストでは、プリミティブ型が問題なく通過することがわかりましたが、ビューから次のコントローラー アクションにポスト バックすると、ChoosenRooms の iColletion が失われます。

ビューからコントローラーへのポストバック、別のクラスの予約内のChoosenRoomsのような複雑なタイプがプロセスで失われない方法の例を誰かが投稿できますか?

または、この情報が失われた理由を説明してください。

コード:

 public class Reserva
{
    public virtual int ID { get; set; }

    [NotMapped]
    public string[] q { get; set; }

    [Display(Name = "Cliente")]
    public virtual Utilizador utilizador { get; set; }

    [Display(Name = "Quarto")]
    public virtual ICollection<Quartos> ChoosenRooms{ get; set; }

    [Display(Name = "Serviços Adicionais")]
    public virtual ICollection<ReservasItens> itens { get; set; }

景色

@model SolarDeOura.Models.Reserva
    @{
        ViewBag.Title = "AddReservaUser";
        var _reserva = TempData["reserva"] as Reserva;
     }

    <h2>AddReservaUser</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Reserva</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.dtEntrada)
        </div>
        <div class="editor-field">
            @Html.DisplayFor(model => model.dtEntrada)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.dtSaida)
        </div>
        <div class="editor-field">
            @Html.DisplayFor(model => model.dtSaida)
        </div>
        <div class="editor-label">
            @Model.q.Count() Choosen Rooms 
        </div>

        @foreach (var q in Model.ChoosenRooms)
        { 
            <ul>
                <li>
                    @Html.DisplayFor(modelitem => q.descricao)
                </li>
            </ul>
        }

ここからの投稿が問題です。このビューでは、「 @foreach (Model.ChoosenRooms の var q)」にデータがありますが、データのポスト バックは失われます。

4

1 に答える 1

1

The concept of model binder at this point was not very clear to me and some knowledge about this topic is all it takes to solve the question.

In resume:

The view gets a model which is a complex type: class [reserva] has a collection of [ChoosenRooms] which is also a complex type.

The line @Html.DisplayFor(modelitem => q.descricao) renders the necessary html elements to display the data, but not the necessary html to be posted back to the controller (input element or hidden field ) so the model binder fails.

Also the controller (post) action argument didn't had the property name that matches the field, in this case it needed to be a String[] type since its a collection of values.

I would also recommend reading about Display Templates and Editor Templates.

于 2015-08-24T16:36:27.630 に答える