2012-01-06 64 views
3

我試圖產生一個分頁視圖中包含的分頁結果表。它通過ajax調用動態刷新。MVC3模型綁定pagedlist與ViewModel與自定義EditorTemplate和部分視圖

我在許多頁面上完美地再現了它,但是在特定的頁面上,我需要綁定表格行的值並將其與ViewModel一起返回。

爲了實現這一點,我試圖使用EditorTemplate作爲PagedList集合使用的自定義對象。問題在於出現在PartialView上的editortemplate沒有正確命名主ViewModel上的PagedList集合。

如果我用代碼解釋它可能更容易。

主視圖出現正是如此:

@model RequestCreateViewModel 
<Code> 

<div id="gridContainer"> 
    @Html.Partial("_PagedCreateRequest") 
    @Html.HiddenFor(x => x.PageSize) 
    @Html.HiddenFor(x => x.PageNumber) 
</div> 
<div id="loadingContainer"> 
</div> 

<More code> 

的部分是這樣的:

@model IPagedList<CreateRequestModel> 
<Code> 

<table class="tablesorter" style="width:750px;"> 
       <thead> 
        <tr> 
</tr> 
       </thead> 
       <tbody> 
        @Html.EditorFor(x => Model) 
       </tbody> 
      </table> 
     </div> 

<div style="float:left"> 
      @Ajax.ImageActionLink(
       "../../Content/images/first.gif", 
       "alt text", 
       "PageResults", 
       new 
       { 
        Page = 1, 
        area = "Admin", 
        SortBy = Model.SortBy, 
        SortDescending = Model.SortDescending, 
        PageSize = Model.PageSize 
       }, 
       new 
       { 
        style = "margin-top: 2px;" 
       }, 
       new 
       { 
        @readonly = "readonly" 
       }, 
       new AjaxOptions 
       { 
        UpdateTargetId = "gridContainer" 
       } 

        ) 
     </div> 

+ other pager Ajax pager icons and stuff 

的EditorTemplate是:

@model CreateRequestModel 

<tr> 
    <td> 
     @Html.CheckBoxFor(x => x.IsSelected) 
    </td> 
    <td> 
     @Html.DisplayFor(x => x.CompanyName) 
     @Html.HiddenFor(x => x.CompanyName) 
    </td> 
    <td> 
     @Html.DisplayFor(x => x.CompanyISIN) 
     @Html.HiddenFor(x => x.CompanyISIN) 
    </td> 
    <td> 
     @Html.DisplayFor(x => x.ShareDesc) 
     @Html.HiddenFor(x => x.ShareDesc) 
    </td> 
    <td> 
     @Html.DisplayFor(x => x.ShareClass) 
     @Html.HiddenFor(x => x.ShareClass) 
    </td> 
</tr> 

視圖模型是:

public class RequestCreateViewModel : ViewModelBase 
{ 
    public IPagedList<CreateRequestModel> PagedList { get; set; } 

的CreateRequestModel是:

public class CreateRequestModel 
{ 
    public int RequestId { get; set; } 

    public bool IsSelected { get; set; } 

    public string CompanyName { get; set; } 
    public string CompanyISIN { get; set; } 
    public string ShareDesc { get; set; } 
    public string ShareClass { get; set; } 
} 

但是當回發到控制器(代碼):

[Authorize(Roles = "Foo")] 
[HttpPost] 
public ActionResult RequestCreate(RequestCreateViewModel model) 

酒店IPagedList未綁定到視圖模型(顯然),因爲的命名標準EditorTemplate是

例如:[0] .Whatever

而非:

PagedList [0] .Whatever

我不能實例化所述接口的綁定副本的IPagedList在任一視圖模型。那麼我該如何去獲取關於主視圖的信息回模型?

我在想一個自定義的模型綁定器可能是解決方案,但我的經驗是在這方面的最小。其最終目的是確定表格上覆選框的值。用戶將選擇或不選,我需要知道他們選擇了什麼!

非常感謝您的幫助。

回答

0

看一看這個帖子:http://weblogs.asp.net/nmarun/archive/2010/03/13/asp-net-mvc-2-model-binding-for-a-collection.aspx

祕訣是產生在控制作爲集合名稱與索引作爲

<input id="Products_0__ID" name="Products[0].ID" type="text" value="1" /> 
+0

的名稱,如果我想經過和手動命名一切是的,我想這會起作用。儘管助手和模板的關鍵在於自動生成綁定的命名約定。我提到一個自定義模型聯編程序的原因之一是我可以綁定沒有前綴:在這種情況下,「產品」...必須有比手動輸入每個屬性的字段更好的方法! =/ – M05Pr1mty 2012-01-06 14:42:28

+0

你可能總是寫你自己的幫手,處理創建控件的正確名稱。 – KMan 2012-01-06 14:44:31

+0

這是一種可能性,你有任何例子或建議,我真的不知道從哪裏開始。我寫了大量的定製助手,但我不知道命名標準是如何生成的...... – M05Pr1mty 2012-01-06 14:52:03

相關問題