質問

これについては少し研究をしていますが、MVC 3でモデル化が必要な場合はご理解がございました。

public class InvitationRequestViewModel
{
    public InvitationRequest InvitationRequest { get; private set; }

    public IEnumerable<SelectListItem> EventsList { get; private set; }

    public string EventId { get; set; }

    public InvitationRequestViewModel(InvitationRequest request)
    {
        InvitationRequest = request;
        EventsList = new SelectList(MyRepositoryAndFactory.Instance.FindAllEvents()
                .Select(events => new SelectListItem 
                { 
                    Value = events.ID.ToString(),
                    Text = String.Format("{0} - {1} - {2}", events.Name, events.Location, events.StartDate.ToShortDateString())
                }
            ), "Value", "Text");
    }
}
.

My InvitationRequestコントローラには、次のアクションメソッドがあります。

public ActionResult Create()
    {
        InvitationRequest request = new InvitationRequest(User.Identity.Name);

        return View(new InvitationRequestViewModel(request));
    } 

    [HttpPost]
    public ActionResult Create(InvitationRequestViewModel newInvitationRequest)
    {
        try
        {
            if (!ModelState.IsValid) return View(newInvitationRequest);

            newInvitationRequest.InvitationRequest.Save();
            MyRepositoryAndFactory.Instance.CommitTransaction();
            return RedirectToAction("Index");
        }
        catch
        {
            ModelState.AddModelError("","Invitation Request could not be created");
        }

        return View(newInvitationRequest);
    }
.

私は問題なしで登録ビューに到達することができ、DDLに利用可能なイベントのリストが入力されます。私の問題は私がinvitationRequestViewModelをhttppost createメソッドにマッピングすることを期待していたことです。代わりに、「Webサイトはページを表示できない」と言われているというエラーが発生します。 署名を使うとき:

public ActionResult Create(FormCollection collection){ }
.

その後投稿された値を見ることができます。コントローラ内の自分のマッピングコードをやらなければならないことを望んでいました。
答えのカスタムモデルバインダーですか?

編集 InvitationRequestViewModel型の強く型付けビューを使用しています。これはDDLコードです。

<div class="editor-label">
        @Html.LabelFor(model => model.InvitationRequest.Event)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(x => x.EventId, Model.EventsList)
    </div>
.

役に立ちましたか?

解決

InvitationRequestViewModelのパラメータレスコンストラクタを指定して、デフォルトのモデルバインダーがインスタンス化できます。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top