1

I am following the tutorial in which the instructor create a class for converting razor syntax to string which is right bellow .

 public static string RazorToString(Controller controller , string viewName , object model = null)
        {
            controller.ViewData.Model = model;
            using(var sw = new StringWriter())
            {
                ViewEngineResult viewResult;
                viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);
                var viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
                viewResult.View.Render(viewContext, sw);
                viewResult.ViewEngine.ReleaseView(controller.ControllerContext, viewResult.View);
                return sw.GetStringBuilder().ToString();
            }
        }

In the controller action he return the jSON like this.

return Json(new { success = true, html = RazorViewToString.RazorToString(this,"GetAll" ,), message = "submitted succesfully" }, JsonRequestBehavior.AllowGet);

The GetALL is the action method which he passed , which is returning the list of records. which is right below.

 public ActionResult GetAll()
        {
            using (DBModel db = new DBModel())
            {
                var list = db.Employees.ToList();
                return View(list);
            }
        }

This above method is the difference I mean in that tutorial the instructor has separately created a method which is getting the list and in this action method he called that method.but in my implementation i returing the list directly as you can seen above.so in the third parameter of

RazorViewToString.RazorToString(this,"GetAll" ,)

He passed that method which i not created , what i supposed to pass here ? and also if someone can describe the functionality above i will be thankful to him. #Peace

0

1 Answer 1

1

The second parameter is not the method name,but the view name.The third parameter is the view model/model needed for that view.

Looks like your GetAll.cshtml view is strongly typed to a list of Employees. So you should be passing that as the model parameter value.

var employeeList = db.Employees.ToList();
return Json(new { success = true,
                  html = RazorToString(this, "GetAll",employeeList ), 
                  message = "submitted succesfully" },JsonRequestBehavior.AllowGet);

This will call the RazorToString method and inside the method we are manually executing the razor view engine and it will return a string which is the HTML markup produced by the view. We are then creating an anonymous object with 3 properties success, html and message and passing that to the Json method, which will convert it to corresponding json format.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.