I am using the following to render a partial view to a string...
protected string RenderPartialViewToString(string viewName, object model)
{
if (string.IsNullOrEmpty(viewName))
viewName = ControllerContext.RouteData.GetRequiredString("action");
ViewData.Model = model;
using (var sw = new StringWriter())
{
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
return sw.GetStringBuilder().ToString();
}
}
However it returns the html with strange tags like this below... (I have included a small section as its a big view)
<$A$><div</$A$><$B$> class="modal hide fade"</$B$><$C$> id="dialog"</$C$><$D$>
This happens throughout the HTML. This section should look like this...
<div class="modal hide fade" id="dialog" style="display: none;">
The following code has always worked for me. Though I can't see any major differences, and can't understand fully why you'd get the output you're getting.
public static String RenderRazorViewToString(ControllerContext controllerContext, String viewName, Object model)
{
controllerContext.Controller.ViewData.Model = model;
using (var sw = new StringWriter())
{
var ViewResult = ViewEngines.Engines.FindPartialView(controllerContext, viewName);
var ViewContext = new ViewContext(controllerContext, ViewResult.View, controllerContext.Controller.ViewData, controllerContext.Controller.TempData, sw);
ViewResult.View.Render(ViewContext, sw);
ViewResult.ViewEngine.ReleaseView(controllerContext, ViewResult.View);
return sw.GetStringBuilder().ToString();
}
}
Strange, after a Clean and Rebuild it fixed the issue, must be a VS gremlin.
Related
Hello is it possible to run PartialView with model inside View without model, or I have to create model which will contains models for partial views?
Example:
User have his site:
#{
ViewBag.Title = "MyPanel";
}
<div class="jumbotron">
<!--Right Menu-->
<div id="RightMenu" class="col-md-10" style="max-height:550px; overflow-y:scroll;">
#Html.Action("AllSongs","UserPanel")
</div>
</div>
</div>
I would like to Render PartialView AllSongs which contains model.
public PartialViewResult AllSongs() {
var userId = (int)Session["Login"];
var songs = context.Songs.Where(x => x.UserID.Equals(userId));
return PartialView(songs);
}
But when code go inside this method I got error like this:
Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'
You can do this, however that action is going to load different that what you are expecting. I think you would want that to load outside of what else is going on. If that is the case you could return the content of that partial and place it in an element --> $('#MyDiv').html('#Model.AllSongsHtmlContnet'); You can use a helper function in your controller to return something like:
protected string RenderPartialViewToString(string viewName, object model)
{
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);
return sw.GetStringBuilder().ToString();
}
}
I have a form to apply for a job.In which the user should be allowed to upload a resume,also the form have a dropdown that allow the user to select from the list of previously uploaded resume.Now to my question,I need the new resume to be uploaded without form submission and repopulate the dropdown with the newly uploaded resume.So the user now can apply the job by selecting the resumes listed in dropdown.Any help please?
You need to use Json in order to prevent full postback in your page. After that you must return to Partial View.
As instance;
HTML Code:
<input type="text" id="UserName" name="UserName"/>
<input type="button" onclick="ButonClick()" value="Enter"/>
Javascript Code:
function ButonClick() {
var data= {
UserName: $('#UserName').val(),
};
$.ajax({
url: "/Home/MyActionResult",
type: "POST",
dataType: "json",
contentType: 'application/json',
data: JSON.stringify(data)
});
Controller:
public ActionResult MyActionResult(string UserName , MyModel model)
{
var stringView = RenderRazorViewToString("_YourPartialView", model);
return Json(stringView, JsonRequestBehavior.AllowGet);
}
Note:
You need below code to render your partial view for json.
Add below to your controller too.
public string RenderRazorViewToString(string viewName, object model)
{
ViewData.Model = model;
using (var sw = new StringWriter())
{
var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);
return sw.GetStringBuilder().ToString();
}
}
using (Ajax.BeginForm("Customer", new AjaxOptions { HttpMethod = "POST" }))
{
{
.................
}
}
How can ı disable postback in new ajaxoptions , what to write ?
You need to use Json in order to prevent full postback in your page.
After that you must return to Partial View.
As instance;
HTML Code:
<input type="text" id="UserName" name="UserName"/>
<input type="button" onclick="ButonClick()" value="Enter"/>
Javascript Code:
function ButonClick() {
var data= {
UserName: $('#UserName').val(),
};
$.ajax({
url: "/Home/MyActionResult",
type: "POST",
dataType: "json",
contentType: 'application/json',
data: JSON.stringify(data),
Controller:
public ActionResult MyActionResult(string UserName)
{
var stringView = RenderRazorViewToString("_YourPartialView", model);
return Json(stringView, JsonRequestBehavior.AllowGet);
}
Note:
You need below code to render your partial view for json.
Add below to your controller too.
public string RenderRazorViewToString(string viewName, object model)
{
ViewData.Model = model;
using (var sw = new StringWriter())
{
var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);
return sw.GetStringBuilder().ToString();
}
}
I have TinyMCE editor in my custom site CMS and store HTML content in SQL DB.
Then I render it in cshtml RazorView:
#MvcHtmlString.Create(Model.Content)
How can I include PartialView inside html content by TinyMCE?
Something like:
...content html...
<div>[[ViewName]]</div>
...content html...
A partial view can be included in an HTML page by using the following
<% Html.RenderPartial("~/Views/Folder/ViewName.ascx");%>
You could execute the Partial View and set the resultant html as the content for TinyMCE.
private string ProduceViewResult(string viewName, object model)
{
ViewData.Model = model;
using (var sw = new StringWriter())
{
var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);
return sw.ToString();
}
}
Usage
var partialViewHtml = ProduceViewResult("PartialViewName", myModel);
I have some trouble play with MVC3.
I have a strongly typed partial view contains a form. it is embedded in a big page. I want to submit this partial view to controller and to update some fields. After the updating I expect the partial view embedded in the page being replaced with the html contains new values becuase the UpdateTargetId is declared, but it is not. Do not know if I can achieve that. Any help will be appreciated. The code as:
public ActionResult Employee(Employee em)
{
var em1 = new Employee
{
Id = 1,
Name = "xing yanguang",
Code = "131324e12"
};
return PartialView(em);
}
The code in the partial view:
Try this
modify your partial view
//In Partial View
#model MvcApplication1.Employee
<table>
<tr>
<td>
#Html.TextBoxFor(m => m.Id)
</td>
<td>
#Html.TextBoxFor(m => m.Name)
</td>
<td>
#Html.TextBoxFor(m => m.Code)
</td>
</tr>
</table>
Now in your parent view
// In View.cshtml
<div id="div_employee">
#Partail("partailView",Model)
</div>
#using (Ajax.BeginForm("Employee", "PO", new AjaxOptions { HttpMethod = "GET", UpdateTargetId = "div_employee", InsertionMode = InsertionMode.Replace }))
{
<input type="submit" value="save" />
}
In Controller
public string Employee(Employee em)
{
var em1 = new Employee
{
Id = 1,
Name = "xing yanguang",
Code = "131324e12"
};
return RenderPartialViewToString("partailView",em1);
}
protected string RenderPartialViewToString(string viewName, object model)
{
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);
return sw.GetStringBuilder().ToString();
}
}
In controller add one more method to get RenderHtml of partial view, by passing PartailViewName and model to RenderPartialViewToString method it will return Htmlstring of view with updated values.