mvc partial view code example
Example 1: asp net core mvc partial view
<partial name="_PartialName" />
Example 2: c# mvc return partial view
<div id="ViewHolder"><div>
$.ajax({
type: "POST",
url: '<Your path to your controller>/GetView',
contentType: "application/text; charset=utf-8",
dataType: "text",
async: false,
success: function (data) {
$('ViewHolder').html(data);
}
})
public PartialViewResult GetView()
{
MyModel myModel = new MyMyodel();
return PartialView("<Your View Name>", myModel);
}
Example 3: mvc asp.net partial view from js
<div id="containerId"></div>
$.ajax({
type: "Get",
url: '<Your url>/GetView',
data: mydata,
contentType: "application/text; charset=utf-8",
dataType: "text",
success: function (data, status) {
$('#containerId').append(data);
},
error: function (err) {
console.log(err);
}
});
[HttpGet]
public ActionResult GetView(string obj)
{
try
{
MyModel model = (new JavaScriptSerializer()).Deserialize<MyModel>(obj);
return View("<Your View name>", obj);
}
catch (Exception ex)
{
return Json(ex.Message, JsonRequestBehavior.AllowGet);
}
}
Example 4: partial MVC
<partial name="ThenName" model="object" />
Example 5: mvc view vs partial view
I think the biggest difference is about the use of the _Layout page:
PartialView(): the razor engine will get the view (e.g. index.cshtml) without any layout page (_layout.cshtml).
View(): the engine will get your view (e.g. index.cshtml) and then appends the content of this view inside the layout page (_layout.cshtml)