2012-01-06 82 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

相关问题