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.
{}button in the editor OR make sure that all of it is indented 4 spaces. You can also surround inline code with backticks`code`