MVC3模型绑定pagedlist到视图模型定制EditorTemplate和管窥 [英] MVC3 Model binding pagedlist to ViewModel with custom EditorTemplate and Partial View

查看:88
本文介绍了MVC3模型绑定pagedlist到视图模型定制EditorTemplate和管窥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着去制作它包含在一个局部视图结果的分页表。它是动态刷新与Ajax调用。

Im trying to produce a paged table of results which is contained in a Partial view. It is dynamically refreshed with ajax calls.

Im在一些地方的作品完美重现页本,但在特定网页上我所需要的表行的值进行约束,并与视图模型回来了。

Im reproducing this on a number of pages where it works perfectly, however on a particular page i require the values of the table rows to be bound and returned with the ViewModel.

要实现这一点,我曾试图使用EditorTemplate使用的PagedList收集自定义对象的IM。问题在于它的地方在PartialView犯规名字出现正确的PagedList收集主视图模型的editortemplate。

To achieve this i have tried to use an EditorTemplate for the custom object im using for the PagedList collection. The problem lies where the editortemplate which appears on the PartialView doesnt name correctly for the PagedList collection on the main ViewModel.

它可能更容易,如果我用code解释。

Its probably easier if i explain with code.

主要观点正是如此显示:

The main view appears thusly:

@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是:

The EditorTemplate is:

@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是:

The CreateRequestModel is:

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; }
}

但是,当回发到控制器(code):

But when posted back to the controller (code):

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

属性IPagedList未绑定到视图模型(显然),因为EditorTemplate的命名标准是

The property IPagedList is not bound to the ViewModel (obviously) because the naming standards of the EditorTemplate are

例如:[0] .Whatever

eg: [0].Whatever

而不是:

PagedList [0] .Whatever

PagedList[0].Whatever

我既不能实例化一个IPagedList在视图模型接口的约束副本。让我怎么去获取信息,关闭主视图回模式?

I cant instantiate a bound copy of the interface for an IPagedList in the ViewModel either. So how do i go about getting the information off the main View back to the model?

我想自定义的模型绑定可能是解决办法,但我的经验是在这个领域微乎其微。这样做的最终目的是要确定在桌子上的复选框的值。用户将选择与否,我需要知道。他们已选择了什么!

I'm thinking a custom Model Binder might be the solution, but my experience is minimal in this area. The end purpose of this is to determine the value of the checkboxes on the table. The user will select or not and i need to know what theyve selected!

任何帮助非常感谢。

推荐答案

有一个看看这篇文章:<一href=\"http://weblogs.asp.net/nmarun/archive/2010/03/13/asp-net-mvc-2-model-binding-for-a-collection.aspx\" rel=\"nofollow\">http://weblogs.asp.net/nmarun/archive/2010/03/13/asp-net-mvc-2-model-binding-for-a-collection.aspx

秘诀是产生在控制作为集合名称与索引为

The secret is to generate the Name on the control as the collection name with an indexer as

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

这篇关于MVC3模型绑定pagedlist到视图模型定制EditorTemplate和管窥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆