简体   繁体   中英

Multiple Models in one view

I am working on a site that has a Layout with multiple partial views on it. These Partial views have models with a database connected to them. I can display the partial views separately but not on the same layout. I used the code first approach so i have a entities model that has all of my models in it. The database was generated and connected. I have data in the tables and i am currently displaying these in the admin section for editing and deleting etc. I am not sure what i am doing wrong. Here is my layout partial call:

    <li>@Html.Partial("_SubCategory")</li>

My controller is a list - currently in the auditScheduleController:

    public ActionResult _SubCategory()
    {
        return View(_db.SubCategories.ToList());
    }

In my partial view i have put a number of senarios and non seem to work. If i put @model IEnumerable<QQAudit.Models.Subcategory> I can display the page by itself and get a list of the Subcategories. It faults out if i open it up through the layout. Saying something about

"The model item passed into the dictionary is of type 'System.Data.Entity.DynamicProxies.AuditSchedule_5218AAD34020C54DC3FF0CFD6169988BB5FED2C296ABF80AC3EFFF93441CE91B', but this dictionary requires a model item of type 'QQAForm.Models.SubCategory'.

I have looked on different forums and found some things but everything i have tried that is out there seems to fail in some fashion.

The funny thing is my background is Zend PHP and i can do what i am tring to accomplish there but cannot duplicate it in MVC3 C#.

August 01 2012

I am at the point were i want to add the ID of the page to the link I have already changed the global.asax to be:

            routes.MapRoute(
            "AuditSecion", // Route name
            "{controller}/{action}/{id}/{section}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional, section = UrlParameter.Optional }, // Parameter defaults
            new[] { "QQAForm.Controllers" }
        );

I want the link to populate the ID that the page is on. So the partial looks like this:

@{ Layout = null; }

@model IEnumerable<QQAForm.Models.SubCategory>

@foreach (var item in Model)
{

@Html.ActionLink(item.SubcategoryName, "audit", new { id = item.SubCategoryID }, null)

}  

The subcategoryID should be the section and the ID should be the ID of the page which looks like this.

http://localhost:52490/AuditSchedule/Audit/1192

in theory the link of the submenu should be

http://localhost:52490/AuditShedule/Audit/1192/1

thanks for the help!

The problem is the way you're invoking the partial view. Html.Partial requires a view name and a model; since you're not passing a model, it is crapping out. (It seems like you're expecting your code to invoke the action named _SubCategory , but it's actually trying to invoke the partial view named _SubCategory .) Try passing the model explicitly (if you have it available in your model):

@Html.Partial("_SubCategory", Model.EnumerableListOfSubCategories)

Alternatively, you can use the Html.Action method, which will render the result of an action in your current controller:

@Html.Action("_SubCategory")

I think the second approach is what you are attempting to do.

Try putting

@{ Layout = null; }

At the top of your _subcategory view

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM