Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello i am very new to MVC
I want show multi partial views on MVC Page. How can i do? Thanks.

What I have tried:

My Models
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MyApp.ViewModels
{
    public class Test1ViewModel
    {
        public int id { get; set; }
        public string Name { get; set; }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MyApp.ViewModels
{
    public class Test2ViewModel
    {
        public int id { get; set; }
        public string Name { get; set; }
    }
}

My action
using MyApp.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MyApp.Controllers
{
    public class TestController : Controller
    {
        // GET: Test
        public ActionResult Index()
        { 
           Partial_1();
           Partial_2();
           return View();
        }

        public ActionResult Partial_1()
        {
            var model = new Test1ViewModel
            {
                id = 1,
                Name = "Name1"
            };
            return PartialView(model);
        }

        public ActionResult Partial_2()
        {
            var model = new Test2ViewModel
            {
                id = 1,
                Name = "Name2"
            };
            return PartialView(model);
        }


    }
}

My partial views

Partial_1.cshtml
@model MyApp.ViewModels.Test1ViewModel 

<div class="col-md-2">
    <h4>Area 1</h4>
    @Html.LabelFor(m => m.id)
    @Html.LabelFor(m => m.Name)
</div>

Partial_2.cshtml
@model MyApp.ViewModels.Test2ViewModel

<div class="col-md-2">
    <h4>Area 1</h4>
    @Html.LabelFor(m => m.id)
    @Html.LabelFor(m => m.Name)
</div>

My index view
@{
    ViewBag.Title = "index";
}

<div class="container">
    <div class="row">
        @Html.Partial("Partial_1")
        @Html.Partial("Partial_2")
    </div>
</div> 
Posted
Updated 29-Mar-21 3:28am
v4

Create a view model to combine the models for your partial views:
C#
public class TestIndexViewModel
{
    public Test1ViewModel Test1 { get; set; }
    public Test2ViewModel Test2 { get; set; }
}
Change your controller to build the combined view-model, rather than calling PartialView and throwing the results away:
C#
public class TestController : Controller
{
    public ActionResult Index()
    { 
        var model = new TestIndexViewModel
        {
            Test1 = CreatePartial1ViewModel(),
            Test2 = CreatePartial2ViewModel(),
        };
        
        return View(model);
    }
    
    private Test1ViewModel CreatePartial1ViewModel() => new Test1ViewModel
    {
        id = 1,
        Name = "Name1",
    };
    
    private Test2ViewModel CreatePartial2ViewModel() => new Test2ViewModel
    {
        id = 1,
        Name = "Name2",
    };
}
Change your index view to pass the correct viewmodel to the partial views:
Razor
@model MyApp.ViewModels.TestIndexViewModel

@{
    ViewBag.Title = "index";
}

<div class="container">
    <div class="row">
        @Html.Partial("Partial_1", Model.Test1)
        @Html.Partial("Partial_2", Model.Test2)
    </div>
</div> 
 
Share this answer
 
Comments
gacar 29-Mar-21 17:01pm    
Great!! Worked. Thank you very much.
The google machine showed me this:

Partial Views - ASP.NET MVC Demystified[^]

Maybe try using RenderPartial instead? (This is just a stab in the dark)
 
Share this answer
 
Comments
gacar 28-Mar-21 9:43am    
Thank you for answer but there is no multiple partial.
#realJSOP 28-Mar-21 12:13pm    
I have an MVC app that has 8 partial views in _Layout.cshtml, and all of them use @Html.Partial for rendering.

If yours aren't rendering, you have something fundamental wrong with your application.

Try checking the developer tools window in you browser and see if you're getting any errors when it tries to load the files.

gacar 28-Mar-21 13:17pm    
8 partial views in _Layout.cshtml?! Who are you, Mark Zuckerberg? :))
If so, tell me what is wrong in my codes? Why you sending me different pages?
#realJSOP 28-Mar-21 14:20pm    
I can't possibly tell you what's wrong with your code because I don't have it to run in visual studio. Try reducing the complexity so that you only have one partial view. If that works, and a 2nd one, and so on.

I can only suggest things to try, and I'm certainly no expert on mvc.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900