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();
}
}
Related
My view is divided into partial views. Hence, at the time of submission my model isn't reconstructed correctly.
The page view displays employee data, where Employee.Contactinfo is the model of _contactInfo partial view, which again has a partial view _phoneInfo to render phone info having model Employee.ContactInfo.PhoneInfo.
Now the problem is with the name of properties. Employee.ContactInfo.PhoneInfo.Contact1 at the time of rendering has name "Contact1", hence at the time of submission the model isn't created appropriately, I get primitive data of Employee but complex type like ContactInfo is null.
I think the solution is to add the prefix at the time of rendering the partial view. How can I perform the following in MVC 6?
employee.cshtml
#model Employee
<% Html.RenderPartial("_conctactInfo", Model.ContactInfo, new ViewDataDictionary
{
TemplateInfo = new System.Web.Mvc.TemplateInfo { HtmlFieldPrefix = "ContactInfo" }
})
%>
_contactInfo.cshtml
#model ContactInfo
<% Html.RenderPartial("_phoneInfo", Model.PhoneInfo, new ViewDataDictionary
{
TemplateInfo = new System.Web.Mvc.TemplateInfo { HtmlFieldPrefix = "PhoneInfo" }
})
%>
_phoneInfo.cshtml
#model PhoneInfo
<input asp-for="#Model.Contact1" />
Here is the solution,
namespace Website1.Extensions
{
public static class HtmlHelper
{
public static IHtmlContent Partial(this IHtmlHelper htmlHelper, string partialViewName, object model, string prefix)
{
var viewData = new ViewDataDictionary(htmlHelper.ViewData);
var htmlPrefix = viewData.TemplateInfo.HtmlFieldPrefix;
viewData.TemplateInfo.HtmlFieldPrefix += !Equals(htmlPrefix, string.Empty) ? $".{prefix}" : prefix;
return htmlHelper.Partial(partialViewName, model, viewData);
}
public static Task<IHtmlContent> PartialAsync(this IHtmlHelper htmlHelper, string partialViewName, object model, string prefix)
{
var viewData = new ViewDataDictionary(htmlHelper.ViewData);
var htmlPrefix = viewData.TemplateInfo.HtmlFieldPrefix;
viewData.TemplateInfo.HtmlFieldPrefix += !Equals(htmlPrefix, string.Empty) ? $".{prefix}" : prefix;
return htmlHelper.PartialAsync(partialViewName, model, viewData);
}
}
}
employee.cshtml
#using Website1.Extensions;
#model Employee
#Html.Partial("_contactInfo", Model.ContactInfo, nameof(Model.ContactInfo))
_contactInfo.cshtml
#using Website1.Extensions;
#model ContactInfo
#Html.Partial("_phoneInfo", Model.PhoneInfo, nameof(Model.PhoneInfo))
_phoneInfo.cshtml
#model PhoneInfo
<input asp-for="#Model.Contact1" />
If you only need this once this would be the quick solution for the _contactInfo partial view
employee.cshtml
#{
var viewData = new ViewDataDictionary(ViewData);
viewData.TemplateInfo.HtmlFieldPrefix = "ContactInfo";
}
<partial name="_conctactInfo" model="Model.ContactInfo" view-data="#viewData"/>
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.
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();
}
}
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.