I am afraid that I do not understand what you are trying to do. If you explain it in more details
what you are trying to achieve and show us more relavant code me and others could be more helpfull.
I am also not sure what could be different in MVC4 but you can always use a reflector to inspect and compare the source code or you can enable
source code debugging in Visual Studio.
Please "Mark As Answer" if the post helped you.
mitja.gti | www.mitjagti.com
I understand, but still can't figgure out why would you use it and the most important thing how are you using it. I would like to replicate your issue and am unable to do so.
However I have build you a few examples of how to work with partial views and Ajax in MVC (including your
toString). If you'll use browsers developer tools and put a breakpoint in success function you will see that the result is returned as string and to render it to the page you need to replace contents (html) of the wrapper.
[Pure Html] First try pure HTML submit form to see that everything is working.
[Ajax 1] Comment out Html.BeginForm and uncomment Ajax.BeginFormin the partialView (_TestPartial). Test it.
[Ajax 2] Uncomment Html.BeginForm and comment out Ajax.BeginForm in the partialView (_TestPartial). In view (Index) uncomment script tag and leave those lines inside success funtion commented out. Test it.
[Ajax 3] In Controller (ViewStringContoller) comment out the line (return PartialView("_TestPartial", model);) and uncomment next line. In view (Index) comment out the first line in success function and uncomment next 4 lines. Test it.
Controller (ViewStringController.cs)
public class MyViewModel
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
}
public ActionResult Index()
{
return View(new MyViewModel {Id = 1, Name = "ASP.NET" });
}
[HttpPost]
public ActionResult PostModel(MyViewModel model)
{
if (ModelState.IsValid)
{
ModelState.Remove("Name");
model.Name = "Hello " + DateTime.Now.ToString("MM:ss:fff");
}
if (Request.IsAjaxRequest())
{
return PartialView("_TestPartial", model);
//return Json(new { error = true, message = RenderViewToString("_TestPartial", model) });
}
else
return View("Index", model);
}
believe we cant render the partial views as strings using ajax. It really sucks...
yeah this is really bad, considerign the rise in JS RIA's. I just want the controler to render the html and then call a JS function with the PV HTML as a argument in the JS funciton:
Public ActionResult GetSomething(FromCollection collection)
{
if(exists == null)
{
//OnSuccess
return JavaScript("OnSuccess(rendered PV stuff in here);");
}
Laurent Jord...
Member
88 Points
64 Posts
RenderPartialViewToString fails in MVC 4 (just HTML controls)
Jul 25, 2012 11:59 AM|LINK
Hello,
I Was working a lot with this method to render partial views to keep the MVC Spirit and behavior of the first rendered view :
protected string RenderPartialViewToString(string viewName, object model) { string toReturn; if (string.IsNullOrEmpty(viewName)) viewName = ControllerContext.RouteData.GetRequiredString("action"); ViewData.Model = model; using (StringWriter sw = new StringWriter()) { ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName); ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw); viewResult.View.Render(viewContext, sw); toReturn = sw.GetStringBuilder().ToString(); return toReturn; } }It was working perfectely with MVC 3 but it fails with MVC 4.
I verified the model, data are well updated. In step by step mode during view rendering. View data corresponds to the updated data.
After step by step in the view, the update arrives on the site not updated.
I hope I'm clear...
Awaiting your answer or a new way to generate a view as string.
Best regards,
Laurent Jordi
http://framework.innovacall.net
http://innovacallframework.codeplex.com
Laurent Jord...
Member
88 Points
64 Posts
Re: RenderPartialViewToString fails in MVC 4 (just HTML controls)
Jul 25, 2012 12:00 PM|LINK
In fact it work well for a first model level :
MyModel.MyObject (well rendered)
but
MyModel.MyObject.MySubObject[I] (is not rendered).
I'm sure it was working in WMC 3.
Awaiting your help
Best regards,
Laurent
Laurent Jord...
Member
88 Points
64 Posts
Re: RenderPartialViewToString fails in MVC 4 (just HTML controls)
Jul 27, 2012 01:59 AM|LINK
Hello,
I changed the title of the thread because I have seen if I user @Model.myfield in a @Html.Raw(Model.myfield) it works.
If i use @Html.TextBoxFor(... it fails.
I tried multiple render to string, same problem.
I don't understand.
Could you help me ?
Best regards,
Laurent
Laurent Jord...
Member
88 Points
64 Posts
Re: RenderPartialViewToString fails in MVC 4 (just HTML controls)
Aug 19, 2012 11:40 AM|LINK
Hello,
No one answer me ... :(
Awaiting...
Best regards
Laurent Jordi
mitja.GTI
Star
11157 Points
2094 Posts
Re: RenderPartialViewToString fails in MVC 4 (just HTML controls)
Aug 19, 2012 02:59 PM|LINK
Dear Laurent Jordi
I am afraid that I do not understand what you are trying to do. If you explain it in more details what you are trying to achieve and show us more relavant code me and others could be more helpfull.
You can check this article that explains how to render partial views on the page.
I am also not sure what could be different in MVC4 but you can always use a reflector to inspect and compare the source code or you can enable source code debugging in Visual Studio.
mitja.gti | www.mitjagti.com
Laurent Jord...
Member
88 Points
64 Posts
Re: RenderPartialViewToString fails in MVC 4 (just HTML controls)
Aug 20, 2012 06:35 AM|LINK
Hi,
Thanks for your answer.
The render partial view to string is usefull for ajax posts and callbacks.
See this thread and tell me if you think its more understandable...
http://aspnetwebstack.codeplex.com/workitem/297
Thanks
Laurent
mitja.GTI
Star
11157 Points
2094 Posts
Re: RenderPartialViewToString fails in MVC 4 (just HTML controls)
Aug 20, 2012 03:13 PM|LINK
I understand, but still can't figgure out why would you use it and the most important thing how are you using it. I would like to replicate your issue and am unable to do so.
However I have build you a few examples of how to work with partial views and Ajax in MVC (including your toString). If you'll use browsers developer tools and put a breakpoint in success function you will see that the result is returned as string and to render it to the page you need to replace contents (html) of the wrapper.
Controller (ViewStringController.cs)
public class MyViewModel { public int Id { get; set; } [Required] public string Name { get; set; } } public ActionResult Index() { return View(new MyViewModel {Id = 1, Name = "ASP.NET" }); } [HttpPost] public ActionResult PostModel(MyViewModel model) { if (ModelState.IsValid) { ModelState.Remove("Name"); model.Name = "Hello " + DateTime.Now.ToString("MM:ss:fff"); } if (Request.IsAjaxRequest()) { return PartialView("_TestPartial", model); //return Json(new { error = true, message = RenderViewToString("_TestPartial", model) }); } else return View("Index", model); }View (Index.cshtml)
@model MvcASPNET.Controllers.ViewStringController.MyViewModel @{ ViewBag.Title = "Index"; } <h2>Index</h2> <div id="TestPartial"> @Html.Partial("_TestPartial") </div> @section Scripts { @Scripts.Render("~/bundles/jqueryval") @* <script type="text/javascript"> $("#Submit1").live("click", function () { var d = $("Form").serialize(); $.ajax({ type: "POST", url: "/ViewString/PostModel", data: d, success: function (result) { $("#TestPartial").html(result); //$("#TestPartial").html(result.message); //if (result.error) { // $("Form").append("<p>An error occured!</p>"); //} }, error: function (req, status, error) { alert(error); } }); return false; }); </script>*@ }Partial View (_TestPartial.cshtml)
@model MvcASPNET.Controllers.ViewStringController.MyViewModel @using(Html.BeginForm("PostModel","ViewString", FormMethod.Post)) { <h3>Partial View (Html)</h3> @Html.HiddenFor(m => m.Id) @Html.NameFor(m=> m.Name)@: : @Html.EditorFor(m=>m.Name) <input id="Submit1" type="submit" value="submit" /> @Html.ValidationSummary() } @*@using (Ajax.BeginForm("PostModel", "ViewString", new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId="TestPartial" })) { <h3>Partial View (Html)</h3> @Html.HiddenFor(m => m.Id) @Html.NameFor(m=> m.Name)@: : @Html.EditorFor(m=>m.Name) <input id="Submit1" type="submit" value="submit" /> @Html.ValidationSummary() }*@mitja.gti | www.mitjagti.com
GorillaMann
Member
117 Points
332 Posts
Re: RenderPartialViewToString fails in MVC 4 (just HTML controls)
Aug 24, 2012 02:33 PM|LINK
I would also like to know the best way to render PV's as strings, I want to pass this string into a JavascriptResult method in my action
syed.iddi
Participant
1688 Points
363 Posts
Re: RenderPartialViewToString fails in MVC 4 (just HTML controls)
Aug 24, 2012 02:41 PM|LINK
I believe we cant render the partial views as strings using ajax. It really sucks...
The action method expects an json serialized object
Try using HttpResponseMessage
GorillaMann
Member
117 Points
332 Posts
Re: RenderPartialViewToString fails in MVC 4 (just HTML controls)
Aug 24, 2012 03:11 PM|LINK
Sorry can you explain more Syed?
believe we cant render the partial views as strings using ajax. It really sucks...
yeah this is really bad, considerign the rise in JS RIA's. I just want the controler to render the html and then call a JS function with the PV HTML as a argument in the JS funciton:
Public ActionResult GetSomething(FromCollection collection)
{
if(exists == null)
{
//OnSuccess
return JavaScript("OnSuccess(rendered PV stuff in here);");
}