change partial view based on select asp.net core
// partial view controller
public IViewResult LoadPartial() {
return PartialView("_login"); // name of the partial view
}
// view (with jquery)
$("#container").load("LoadPartial");
change partial view based on select asp.net core
// partial view controller
public IViewResult LoadPartial() {
return PartialView("_login"); // name of the partial view
}
// view (with jquery)
$("#container").load("LoadPartial");
c# mvc return partial view
// Create a container for your data
<div id="ViewHolder"><div>
//You can call your method using ajax:
$.ajax({
type: "POST",
url: '<Your path to your controller>/GetView',
contentType: "application/text; charset=utf-8",
dataType: "text",
async: false,
success: function (data) {
// Populate your container
$('ViewHolder').html(data);
}
})
// In your controller
public PartialViewResult GetView()
{
//Passing a model is optional
MyModel myModel = new MyMyodel();
return PartialView("<Your View Name>", myModel);
}
mvc asp.net partial view from js
//You can call a Partial View through AJAX
<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) {
//Use append to add it to the div and not overwrite it
//if you have other data in your container
$('#containerId').append(data);
},
error: function (err) {
console.log(err);
}
});
//In C#
/// <summary>
/// Renders a single view.
/// NOTE : PARTIAL VIEW CANNOT RENDER MULTIPLE VIEWS!
/// Instead loop through them.
/// </summary>
/// <param name="obj">JSON object containing input data</param>
/// <returns></returns>
[HttpGet]
public ActionResult GetView(string obj)
{
//Parse the object into a model
try
{
MyModel model = (new JavaScriptSerializer()).Deserialize<MyModel>(obj);
return View("<Your View name>", obj);
}
catch (Exception ex)
{
return Json(ex.Message, JsonRequestBehavior.AllowGet);
}
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us