Skip to main content

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.

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.

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.

added 177 characters in body
Source Link
tvanfosson
  • 534.2k
  • 102
  • 703
  • 801

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.";

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>

This is a partial view of address.

@Model.City

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

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

@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.

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

This is a partial view of address.

@Model.City

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.

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.

Source Link
Stack0verflow
  • 1.2k
  • 5
  • 21
  • 44

Return a partial view from a controller?

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

This is a partial view of address.

@Model.City

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.