开发者

mvc3 razor index page with two partial pages with diff. page model

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 li开发者_运维百科st 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.


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)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜