Click here to Skip to main content
16,020,568 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I do not know how to insert the controller I want to use. I followed this video to make a modal with validation:
jQuery Ajax CRUD in ASP.NET Core MVC using Popup Dialog - YouTube[^]
When the validation is good, I need it to bring the user to the Album controller. Currently, it is set to use the Song controller by using the this keyword:
return Json(new { isValid = true, html = Helper.RenderRazorViewToString(this, "Index", context.Songs.ToList()) });

public IActionResult AddOrEdit(Song song)
        {
            if (ModelState.IsValid)
            {
                <-- removed for brevity -->
                    
                }
                context.SaveChanges();
                return Json(new { isValid = true, html = Helper.RenderRazorViewToString(AlbumController, "Index", context.Songs.ToList()) });
            }

The Helper class
public class Helper
    {
        public static string RenderRazorViewToString(Controller controller, string viewName, object model = null)
        {
            controller.ViewData.Model = model;
            using (var sw = new StringWriter())
            {
                IViewEngine viewEngine = controller.HttpContext.RequestServices.GetService(typeof(ICompositeViewEngine)) as ICompositeViewEngine;
                ViewEngineResult viewResult = viewEngine.FindView(controller.ControllerContext, viewName, false);

                ViewContext viewContext = new ViewContext(
                    controller.ControllerContext,
                    viewResult.View,
                    controller.ViewData,
                    controller.TempData,
                    sw,
                    new HtmlHelperOptions()
                );
                viewResult.View.RenderAsync(viewContext);
                return sw.GetStringBuilder().ToString();
            }
        }
    }


What I have tried:

Passing in the controller as a string:
return Json(new { isValid = true, html = Helper.RenderRazorViewToString("Album", "Index", context.Songs.ToList()) });
Posted
Updated 11-Oct-22 0:39am

1 solution

You can't convert a string to a Controller instance.

Depending on your setup, you might be able to create an instance of the controller to pass in:
C#
AlbumController controller = new() { ControllerContext = ControllerContext };
string html = Helper.RenderRazorViewToString(controller, "Index", context.Songs.ToList());
Or perhaps you could resolve the controller instance from your DI container:
C#
AlbumController controller = HttpContext.RequestServices.GetRequiredService<AlbumController>();
string html = Helper.RenderRazorViewToString(controller, "Index", context.Songs.ToList());
Otherwise, you may need to specify the full path of the view:
C#
string html = Helper.RenderRazorViewToString(this, "~/Views/Album/Index.cshtml", context.Songs.ToList());
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900