14

So, we can return a partial view from a controller like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;

namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        public PartialViewResult Address()
        {
            Address a = new Address 
            { 
                Line1 = "111 First Ave N.", 
                Line2 = "APT 222", 
                City = "Miami", 
                State = "FL", 
                Zip = "33133" 
            };

            return PartialView(@"~/Views/Home/_Address.cshtml", a);
        }
    }
}

But, how am I supposed to use the returned partial view? I created _Address.cshtml under Views/Home, like this:

@model MvcApplication1.Models.Address

<p>
    This is a partial view of address.
</p>
<p>
  @Model.City
</p>

And, at the end of Views/Home/Contact.cshtml, I added this line:

@Html.Partial(@"~/Views/Home/_Address.cshtml")

And I expect to see the City of my address, but it doesn't show up. I am confused.

3
  • Thank you for formatting my code. I tried hard formatting it, but it refuses to format like I wanted. Commented Mar 27, 2014 at 2:11
  • It's actually pretty easy. Just select your code text, then hit the {} button in the editor OR make sure that all of it is indented 4 spaces. You can also surround inline code with backticks `code` Commented Mar 27, 2014 at 2:15
  • OK, thanks. Will try next time. Commented Mar 27, 2014 at 2:25

2 Answers 2

12

When the partial takes a different model than the method you're including it in you need to use the overload that takes a model parameter and supply the model for the view. By default it uses the same model as the including view. Typically you only need the path if it's in a different, non-shared folder. If it's in the same controller's folder, using just the name should do the trick.

@Html.Partial("_Address", Model.Address)

On the other hand, if you're asking how do I get the partial view from an action included in my page, then you want to use the Action method instead of the Partial method.

@Html.Action("Address")

EDIT

To make the partial work you need to pass a Contact model to the contact view.

public ActionResult Contact()
{
     var contact = new Contact
     {
        Address = new Address
                  { 
                       Line1 = "111 First Ave N.",
                       Line2 = "APT 222",
                       City = "Miami",
                       State = "FL",
                       Zip = "33133"
                  }
     }

     return View(contact);
}
Sign up to request clarification or add additional context in comments.

6 Comments

@Stack0verflow - that's because you're not passing a Contact model to the parent page.
But I am passing the @model MvcApplication1.Models.Address to my _Address.cshtml partial view.
But then you have taken my partial view action method out of the equation.
@Stack0verflow - that's where the second option, rendering an action in a view comes in. That's the option you want to use if you want to include the result (partial view) of a different action.
Ah, that's how we use a partial view returned from an action method. Thanks a lot. Yes, the 2nd option works: @Html.Action("Address")
|
7

demo for you:

    public ActionResult Update(Demo model)
{
    var item = db.Items.Where(item => item.Number == model.Number).First();
    if (item.Type=="EXPENSIVE")
    {
        return PartialView("name Partial", someViewModel);
    }
}

2 Comments

Thanks, suppose my partial view name is _Address.cshtml, how am I supposed to use this partial view in my Contact.cshtml view?
if you can call partial in view, you can using : @{ Html.RenderPartial("_Test"); }

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.