2012-05-15 15 views
31

現在、2つの厳密に型指定されたビューで構成されるフォームを投稿しようとしています。この質問は似ていますが、それは答えを持っていません。 複数の部分ビューを含むフォームを投稿する

MVC 3 Razor Form Post w/ Multiple Strongly Typed Partial Views Not Binding

私はコントローラに送信されたモデルは、常にnullで形成提出

。私はこれを動作させるために数時間を費やしました。これは単純なはずです。私はここに何かを逃していますか私はコントローラに投稿して新しいページをレンダリングできるだけのAjaxを行う必要はありません。

おかげで

ここに私のビューのコードは次のとおりです。

<div> 
    @using (Html.BeginForm("TransactionReport", "Reports", FormMethod.Post, new {id="report_request"})) 
    { 
     ViewContext.FormContext.ValidationSummaryId = "valSumId"; 
     @Html.ValidationSummary(false, "Please fix these error(s) and try again.", new Dictionary<string, object> { { "id", "valSumId" } }); 
     @Html.Partial("_ReportOptions", Model.ReportOptions); 
     @Html.Partial("_TransactionSearchFields", new ViewDataDictionary(viewData) { Model = Model.SearchCriteria }); 
    } 

ここでは、コントローラのコードです:

[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult TransactionReport(TransactionReportRequest reportRequest) 
{ 
    var reportInfo = new List<TransactionReportItem>(); 

    if (ModelState.IsValid) 
    { 
     var reportData = _reportDataService.GetReportData(Search.MapToDomainSearchCriteria(reportRequest.SearchCriteria)); 
     if (reportData!=null) 
     { 
      reportInfo = reportData.ToList(); 
     } 
     return View(reportInfo); 
    } 
    return View(reportInfo); 
} 

部分図自体は、彼らがしているすべてのため、かなり無関係ですやっていることは、彼らのモデルを託して表示することです。

+0

あなたがボタンを提出するのですか?フォームタグ内にある必要があります。あなたがちょうどそれを含んでいないか、それがタグの外にあるかどうかわからない –

答えて

49

部分的にはここに行くことができません。あなたはEditorTemplatesを探しています。これはあなたが望むもののために作られています。この場合、プロパティはモデルに正しく結び付けられます(送信する)。

あなたの主なビューは、このフォームがあります(あなただけPartialの代わりにEditorForを使用する必要がノートを、この場合、あなたはおそらくそうViewBagでいるのViewDataパラメータを入れたりする必要があります):

@using (Html.BeginForm("TransactionReport", "Reports", FormMethod.Post, new {id="report_request"})) 
{ 
    ViewContext.FormContext.ValidationSummaryId = "valSumId"; 
    @Html.ValidationSummary(false, "Please fix these error(s) and try again.", new Dictionary<string, object> { { "id", "valSumId" } }); 
    @Html.EditorFor(model => model.ReportOptions); 
    @Html.EditorFor(model = Model.SearchCriteria }); 
} 

これで、部分ファイルをフォルダ~/Shared/EditorTemplates/にドラッグし、それらの名前をエディタテンプレートと同じモデル名に変更するだけです。

~/Shared/EditorTemplates/フォルダで、新しい「表示」を作成します(例:SearchCriteria.cshtml)。内部では、エディタテンプレートを作成するクラスのタイプを「モデル」として配置します。例(例えば、クラスはプロパティNameOtherCriteriaを持っている):

@model MyNamespace.SearchCriteria 
<ul> 
    <!-- Note that I also use EditorFor for the properties; this way you can "nest" editor templates or create custom editor templates for system types (like DateTime or String or ...). --> 
    <li>@Html.LabelFor(m => m.Name): @Html.EditorFor(m => m.Name)</li> 
    <li>@Html.LabelFor(m => OtherCriteria): @Html.EditorFor(m => m.OtherCriteria</li> 
</ul> 

それらについていくつかの良い読書:

+1

はい、これははるかに良い解決策です。 Grailsにはグーグルの言葉を使って私がグーグルで喋ったはずの似たものがあります。助けてくれてありがとう! – Buzzer

+4

特定のテクノロジで正しい用語がわからないときに、何かを見つけるのが難しい場合があります。 – Styxxy

+0

笑...それは本当です。再度、感謝します! – Buzzer

10

私は@Styxxyと@Tonyに同意しますが、Editor Templatesがより良い解決策です。しかし、あなたの問題は、部分的なビューにサブモデルを供給しているということです。したがって、パーシャルビューがレンダリングされるとき、パーツがより大きいモデルの一部であることを知らず、正しい名前属性を生成しません。

エディタテンプレートではなくPartialsを使用することを強くお勧めする場合は、モデルをパーシャルに渡し、各パーシャルにModel.Whatever.Fooを渡して、バインディングの正しい名前属性を生成することをお勧めします。

9

PartialViewのフィールドにプレフィックスを追加する必要があります。これにより、データを正しくバインドできます。だからではなく、

@Html.Partial("_ReportOptions", Model.ReportOptions); 

用途:

@Html.Partial("_ReportOptions", Model.ReportOptions, new ViewDataDictionary { TemplateInfo = new TemplateInfo { HtmlFieldPrefix = "ReportOptions" }}) 
1
@Html.Partial("_ReportOptions", Model.Contact, new ViewDataDictionary() 
      { 
       TemplateInfo = new TemplateInfo() 
        { 
         HtmlFieldPrefix = "Contact" 
        } 
      }) 

) 


@Html.Partial("_TransactionSearchFields", Model.SearchCriteria, new 
ViewDataDictionary() 
      { 
       TemplateInfo = new TemplateInfo() 
        { 
         HtmlFieldPrefix = "SearchCriteria" 
        } 
      }) 
関連する問題