Heba Heba - 7 days ago 5
C# Question

I tried to render partial view inside my view with jquery , but the the part of partial view dosen't appear in run time

these are the actions in controller:

public ActionResult AdminRoles(int? selectedValue)
{
if (!LogedUser.InRole(Functions.User, Roles.View)) return RedirectToAction("Login");

return View(db.Users.Where(u => u.Id != 1));
}
[HttpGet]
public ActionResult GetAdminRoles(int Id)
{
var secRole = db.SecurityRoles.Where(s => s.AdminId == Id);
var func = db.SystemFunctions.ToList();

if (func.Count() > secRole.Count())
{
foreach (var item in func)
{
if (secRole.Where(s => s.SystemFunctionId == item.Id).Count() <= 0)
{
SecurityRoles sec = new SecurityRoles();
sec.AdminId = Id; sec.SystemFunctionId = item.Id;
sec.CanView = false; sec.CanAdd = false; sec.CanEdit = false; sec.CanDelete = false;
db.SecurityRoles.Add(sec);
db.SaveChanges();
}
}
}

return PartialView("GetAdminRoles",db.SecurityRoles.Where(s => s.AdminId == Id));
}
[HttpPost]
public ActionResult GetAdminRoles(int hdnAdminIDs, int[] CanView, int[] CanAdd, int[] CanEdit, int[] CanDelete)
{
var list = db.SecurityRoles.Where(o => o.AdminId == hdnAdminIDs).ToList();

foreach (var item in list)
{
if (CanView != null && CanView.Contains(item.Id))
item.CanView = true;
else
item.CanView = false;


if (CanAdd != null && CanAdd.Contains(item.Id))
item.CanAdd = true;
else
item.CanAdd = false;


if (CanEdit != null && CanEdit.Contains(item.Id))
item.CanEdit = true;
else
item.CanEdit = false;


if (CanDelete != null && CanDelete.Contains(item.Id))
item.CanDelete = true;
else
item.CanDelete = false;
}
db.SaveChanges();

return RedirectToAction("AdminRoles");
}


the following is the AdminRoles() action which include the JQuery scripts that would bring the partial view GetAdminRoles
NOTE : I tried several scripts ( between the comment marks)

@model IEnumerable<Arabawy.Models.User>
@{
ViewBag.Title = "ContactusMessages";
Layout = "~/Views/Shared/_LayoutAdmin.cshtml";
ViewBag.CurrentURI = "/IWS/adminroles".ToLower();
}

<script>
$(function () {

@*$.get('@Url.Content("~/IWS/GetAdminRoles/")' + "?Id=" + $('#AdminIDs').val(), function (data) {

/* data is the pure html returned from action method, load it to your page */
$('#partialPlaceHolder').html(data);
/* little fade in effect */
$('#partialPlaceHolder').fadeIn('fast');
});*@


$('#AdminIDs').change(function () {
debugger
/* Get the selected value of dropdownlist */
var selectedID = $(this).val();

/* Request the partial view with .get request. */
$.get('@Url.Content("~/IWS/GetAdminRoles/")' + "?Id=" + selectedID, function (data) {

/* data is the pure html returned from action method, load it to your page */
$('#partialPlaceHolder').html(data);
/* little fade in effect */
$('#partialPlaceHolder').fadeIn('fast');
});

});
});
</script>

<div class="pages_inner_content">
<div class="title_page_start">
<div class="right_title">
<h2>صلاحيات المشرفين</h2>
<h2><span>تحديث صلاحيات المشرفين</span></h2>
</div>
<div class="left_lisks">
<ul class="links_buttons">
@{if (Arabawy.LogedUser.IsLoged() &&
Arabawy.LogedUser.InRole(Arabawy.Controllers.IWSController.Functions.User, Arabawy.Roles.Edit))
{
<li class="add_fo">
<a href="javascript:;" onclick="submitForm()" data-hover="حفظ"><span> حفظ </span></a>
</li>
}}
<li class="exit">
<a href="@Url.Action("Index")">خروج</a>
</li>
</ul>
</div>
</div><!--title_page_start-->

<div class="bread_cramb bread_cramb_akh_ed">
<h3> أدوات الأداره &nbsp; <span> صلاحيات المشرفين </span> </h3>
</div> <!--bread_cramb-->


<div class="block_in_editor">
<h3>صلاحيات المشرفين</h3>

<div style="clear:both"></div>

<div class="add_khaber_form">
@Html.ValidationSummary(false)

<hr />
<div class="blog_form_input">
<label>اختر : </label>
<select class="select" id="AdminIDs" name="AdminIDs">
<option value="" selected>&lt; اختر المشرف &gt;</option>
@{foreach (var item in Model)
{
<option value="@item.Id">@item.DisplayName</option>
}}
</select>
</div>
<br />

<div id="partialPlaceHolder" style="display:none;"> </div>


</div><!--add_khaber_form-->

</div><!--block_in_editor-->
</div><!--pages_inner_content-->


this is the view of GetAdminRoles

@model IEnumerable<Arabawy.Models.SecurityRoles>
<script>
function submitForm() {
$("#hdnAdminIDs").val($("#AdminIDs").val());
$('#formID').attr('target', '_self');
$("#formID").submit();
}
</script>
<form action="@Url.Action("GetAdminRoles")" target="_self" id="formID" method="post" enctype="multipart/form-data">
@Html.Hidden("hdnAdminIDs")
<div class="block_in_adv">
<div style="clear:both"></div>
<div style="clear:both"></div>
<div class="table_adv_show">
<div class="row_first color_row">
<div class="cols_mos_2">اسم الصفحة</div>
<div class="cols_mos_3">امكانية رؤية الصفحة</div>
<div class="cols_mos_3">امكانية الأضافة </div>
<div class="cols_mos_3">امكانية التعديل </div>
<div class="cols_mos_3">امكانية المسح</div>
</div><!--main_row_opi-->

<ul class="list_pages_web sortable list" id="itemContainer">
@Html.Hidden("DeletedID")
@foreach (var item in Model)
{
<li class="zoomInUp">
<div class="block_row_table">
<div class="main_row_opi_res ">
<div class="cols_mos_2">اسم الصفحة</div>
<div class="cols_mos_3">امكانية رؤية الصفحة</div>
<div class="cols_mos_3">امكانية الأضافة </div>
<div class="cols_mos_3">امكانية التعديل </div>
<div class="cols_mos_3">امكانية المسح</div>
</div><!--main_row_opi-->

<div class="main_row_opi">
<div class="cols_mos_2">@item.SystemFunction.PageName</div>
@{
string canView = item.CanView ? "checked" : "";
string CanEdit = item.CanEdit ? "checked" : "";
string CanAdd = item.CanAdd ? "checked" : "";
string CanDelete = item.CanDelete ? "checked" : "";
}

<div class="cols_mos_3">
<input type="checkbox" name="CanView" id=@string.Format("CanView" + item.Id) class="css-checkbox9 all_check" value="@item.Id" @canView /><label for=@string.Format("CanView" + item.Id) class="css-label9" checked="checked">&nbsp; </label>
</div>
<div class="cols_mos_3">
<input type="checkbox" name="CanAdd" id=@string.Format("CanAdd" + item.Id) class="css-checkbox9 all_check" value="@item.Id" @CanAdd /><label for=@string.Format("CanAdd" + item.Id) class="css-label9" checked="checked">&nbsp; </label>
</div>
<div class="cols_mos_3">
<input type="checkbox" name="CanEdit" id=@string.Format("CanEdit" + item.Id) class="css-checkbox9 all_check" value="@item.Id" @CanEdit /><label for=@string.Format("CanEdit" + item.Id) class="css-label9" checked="checked">&nbsp; </label>
</div>
<div class="cols_mos_3">
<input type="checkbox" name="CanDelete" id=@string.Format("CanDelete" + item.Id) class="css-checkbox9 all_check" value="@item.Id" @CanDelete /><label for=@string.Format("CanDelete" + item.Id) class="css-label9" checked="checked">&nbsp; </label>
</div>

</div><!--main_row_opi-->
</div><!--/*block_row_table*/-->
</li>
}

</ul>

<div class="holder"></div>

</div><!--table_adv_show-->



</div><!--block_in_adv-->




so , why my partial view does not appear in run time ???!!!!
Is there any thing wrong ???!!
please help me !!!!!!!!!!!!!!!!!!!!!

Answer

Navigate to that partial view using your browser to ascertain it is returning what you expect. Once you have made sure that is returning the expected HTML, then your code should work.

Comments