mvc3 razor 索引页面,包含两个带有差异的部分页面。页面模型

发布于 2024-11-02 02:17:00 字数 317 浏览 0 评论 0原文

我的索引看起来像这样,页面模型接受对象列表

@model List<MyProject.Domain.Object>

@Html.Partial("PickDatePartial") 
@Html.Partial("ObjectPartial", Model)

我的问题出在第一个部分页面内,因为该部分页面接受的不是列表,而是单个 @model MyProject.Domain.Object

我怎样才能实现这个,在当前情况下,索引页无法呈现,因为它期望接收对象列表。

先感谢您。

my index looks like this with page model which accepts list of objects

@model List<MyProject.Domain.Object>

@Html.Partial("PickDatePartial") 
@Html.Partial("ObjectPartial", Model)

My problem is within this first partial page, cause this partial page accept not list but single @model MyProject.Domain.Object

How can I achive this, in this current situation index page cannot be rendered cause it expect to receive list of objects.

Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枫林﹌晚霞¤ 2024-11-09 02:17:00

好吧,您可以做的一件快速而肮脏的事情是将第一个对象传递给 PickDatePartial

@Html.Partial("PickDatePartial", Model.First())

但这不是很干净。我建议创建一个具有两个属性的视图模型,1)整个列表,2)由 PickDatePartial 绑定的属性:

public class IndexViewModel
{
    public List<MyProject.Domain.Object> MyList { get; set; }
    public MyProject.Domain.Object ObjectToBind { get; set; }
}

然后在控制器上的 Index 操作中:

public ActionResult Index()
{
    var myList = // instantiate list here
    var viewModel = new IndexViewModel
    {
        MyList = myList,
        ObjectToBind = myList.First() // or whichever you need out of that list
    }

    return View(viewModel);
}

当然,您需要在 Index 视图上更改模型的类型。现在 Index 视图如下所示:

@model IndexViewModel

@Html.Partial("PickDatePartial", Model.ObjectToBind) 
@Html.Partial("ObjectPartial", Model.MyList)

Well, one quick and dirty thing you could to is pass the first object to PickDatePartial

@Html.Partial("PickDatePartial", Model.First())

This is however not very clean. I would recommend creating a view model with two properties, 1) the whole list, 2) the one to be bound by PickDatePartial:

public class IndexViewModel
{
    public List<MyProject.Domain.Object> MyList { get; set; }
    public MyProject.Domain.Object ObjectToBind { get; set; }
}

Then in your Index action on your controller:

public ActionResult Index()
{
    var myList = // instantiate list here
    var viewModel = new IndexViewModel
    {
        MyList = myList,
        ObjectToBind = myList.First() // or whichever you need out of that list
    }

    return View(viewModel);
}

You'll need to change the type of the model on the Index view, of course. Now the Index view looks like this:

@model IndexViewModel

@Html.Partial("PickDatePartial", Model.ObjectToBind) 
@Html.Partial("ObjectPartial", Model.MyList)
末が日狂欢 2024-11-09 02:17:00

如果“PickDatePartial”需要一个对象,并且您给它一个对象列表,那么它应该使用哪个对象?

由您决定要做什么。

您可以告诉它使用第一个对象:

@if (Model.Count > 0) {
   @Html.Partial("PickDatePartial", Model[0])
}

或者,您可以为每个对象调用它:

@foreach(var item in Model) {
   @Html.Partial("PickDatePartial", item)

}

可能性是无限的。

If "PickDatePartial" expects one object, and you give it a list of objects, what object should it use?

It is up to you to decide what to do.

You could tell it to use the first object:

@if (Model.Count > 0) {
   @Html.Partial("PickDatePartial", Model[0])
}

or, you could call it for every object:

@foreach(var item in Model) {
   @Html.Partial("PickDatePartial", item)

}

The possibilities are endless.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文