I have a solution with two projects and I'm trying to send a partial view from one project to the other.
So in project AI have a controller like this:
public PartialViewResult Index()
{
return PartialView("_Forms");
}
And in project BI have a controller like this:
public ActionResult Index()
{
var form = pa.Index(); // <-- This is the controller from controller A
ViewBag.CMSForm = form;
return View();
}
... so far so good, but now I need to render the partial view from ViewBag.CMSForm and I can't figure out how.
If you are only trying to render the partial view...
I was just messing with something similar to this, where I needed to render different partial views on one Index view based on where the user was routing from.
What I did was something like this...
public ActionResult Index()
{
ViewBag.CMSForm = "_Forms";
return View();
}
Then on your view
@{
string form= ViewBag.CMSForm;
}
@section CustomForm{
@Html.Partial(form)
}
I have adapted the solution from http://craftycodeblog.com/2010/05/15/asp-net-mvc-render-partial-view-to-string/ to your case.
Change your code from project B to this:
public ActionResult Index()
{
var form = pa.Index(); // <-- This is the controller from controller A
using (var sw = new StringWriter())
{
// Find the actual partial view.
var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, form.ViewName);
// Build a view context.
var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
// Render the view.
viewResult.View.Render(viewContext, sw);
// Get the string rendered.
ViewBag.CMSForm = sw.GetStringBuilder().ToString();
}
return View();
}
Why not use rendering of ControllerA.Index-action in view of ControllerB?
<p>
@Html.Action("Index", "ControllerA")
</p>
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.