multiple ways to call a partial view in asp.net mvc 5
there are scenarios where you want to minimize redundancy and fasten up productivity, hence partial views play role over here.
1st Approach
suppose you are creating a user mgt application where you'll first add user with four input fields, then will show them in index page, if operator want to edit any user record, that record will get displayed in edit mode (same four fields) then upon update record gets saved in db.
in this scenario either you have to create two different views create and update containing same fields OR you just make a template / partial view and call it in both views resulting in mitigating redundancy, increasing productivity, also if you want to make some changes, you dont need to update both views i.e (create / update) you'll only need to update one partial view.
create.cshtml
@model Model.User
Html.BeginForm()
{
@RenderPartial()
}
update.cshtml
partialuser.cshtml
2nd Approach
1st Approach
suppose you are creating a user mgt application where you'll first add user with four input fields, then will show them in index page, if operator want to edit any user record, that record will get displayed in edit mode (same four fields) then upon update record gets saved in db.
in this scenario either you have to create two different views create and update containing same fields OR you just make a template / partial view and call it in both views resulting in mitigating redundancy, increasing productivity, also if you want to make some changes, you dont need to update both views i.e (create / update) you'll only need to update one partial view.
create.cshtml
@model Model.User
Html.BeginForm()
{
@RenderPartial()
}
update.cshtml
partialuser.cshtml
2nd Approach
what I see in your controller is a mix of methods. I would create a partial view method
public PartialViewResult GetChart(int id)
{
Model model = (build your model);
return PartialView("_PartialViewName", Model);
}
in your view<div class="Content"></div>
then in your javascript $('.btnSubmit').on('click', function(){
$.ajax({
url: "@(Url.Action("GetChart", "Controller"))",
type: "POST",
cache: false,
async: true,
data: { data: 'fieldName' },
success: function (result) {
$(".Content").html(result);
}
});
});
you can send whatever data you need to through the data just data: 'data', data1: 'data1', etc. Hopefully this helps.
Comments
Post a Comment