I have two applications written in an aspx web form, and the other in MVC5 razor cshtml.
An iframe is embedded in the web form, and I want to load the cztml razor file inside the iframe when the user clicks the button.
I searched and found useful posts for mixing webforms and MVC pages so that I can display the MVC page on an aspx webforms page.
How to use ASP.Net MVC View inside WebForms.aspx page?
How to enable partial view inside web form
Based on the above post, I created the MvcUtility class in my MVC application in a new folder called Helpers.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using System.Web.Mvc; using System.Web.Routing; using Desk.Web.Client.Controllers; using Desk.Web.Client.Models; namespace Desk.Web.Client.Helpers { public class RazorPartialBridge { private static void RenderPartial(string partialViewName, object model) { HttpContextBase httpContextBase = new HttpContextWrapper(HttpContext.Current); RouteData routeData = new RouteData(); routeData.Values.Add("controller", "Dummy"); ControllerContext controllerContext = new ControllerContext(new RequestContext(httpContextBase, routeData), new DummyController()); IView view = FindPartialView(controllerContext, partialViewName); ViewContext viewContext = new ViewContext(controllerContext, view, new ViewDataDictionary { Model = model }, new TempDataDictionary(), httpContextBase.Response.Output); view.Render(viewContext, httpContextBase.Response.Output); }
as indicated in the message. Then I created a class (RendeActionViewModel) inside the models folder, and the code
namespace Desk.Web.Client.Models { public class RenderActionViewModel { public string ControllerName { get; set; } public string ActionName { get; set; } public object RouteValues { get; set; } } }
I created a dummy controller under the controllers folder
public class DummyController : Controller { public ActionResult PartialRender() { return PartialView(); } }
Then I created a view called PartialRender.cshtml in the views folder.
@model RenderActionViewModel <h1>Hello World</h1> <p> I was rendered from a <strong>@Model.Source</strong>! </p>
In the webform of my asp.net web application, I created a new aspx page and added the code below.
<%@ Page Title="Demo" Language="C#" AutoEventWireup="true" Inherits="Demo" Codebehind="Demo.aspx.cs" %> <html> <head></head> <body> <% RazorPartialBridge.RenderPartial("_Partial", new RenderActionViewModel() { Source = "ASPX Page" }) %> </body> </html>
However, when I launch the application, I get the error below
Partial view of PartialRender not found. Search for places: ~ / Views / Dummy / PartialRender.aspx ~ / Views / Dummy / PartialRender.aspx ~ / Views / Shared / PartialRender.aspx ~ / Views / Shared / PartialRender.ascx ~ / Views / Dummy / PartialRender.cshtml ~ / Views / Dummy / PartialRender.vbhtml ~ / Views / Shared / PartialRender.cshtml ~ / Views / Shared / PartialRender.vbhtml
and the view name is returned as null in the code below when I tried to debug the application
ViewEngineResult result = ViewEngines.Engines.FindPartialView(controllerContext, partialViewName);
Can someone help me and tell me where I am going wrong?