L A

L A

  • NA
  • 170
  • 171.2k

Partial View replaces Parent view

Jul 6 2018 3:23 AM
Hi all,
 
I'm working on web app developed in ASP.Net MVC, having a partial view which should be rendered inside its parent view.
  • Parent view has a HTML Dropdown, on-change event should bind respective data to partial view. But on selection change, the complete parent view is replaced with partial view (child view).
Parent View (Index.cshtml)
  1. <h3>Please Select Group</h3>    
  2. @using (Html.BeginForm("EmployeeDeptHistory""Home", FormMethod.Post))    
  3. {    
  4.     @Html.AntiForgeryToken()    
  5.     if (ViewBag.DepartmentList != null)    
  6.     {    
  7.         @Html.DropDownList("DepartmentName", ViewBag.DepartmentList as SelectList, "-- Select --"new { Class = "form-control", onchange = "this.form.submit();" })    
  8.     }    
  9. }    
  10. <div>    
  11.     @{Html.RenderPartial("_EmployeeDeptHistory");}    
  12. </div>    
Partial View (_EmployeeDeptHistory.cshtml)
  1. @model IEnumerable<PartialViewApplSol.Models.EmployeeDepartmentHistory>    
  2. @if (Model != null)    
  3. {    
  4.     <h3>Employees Department History : @Model.Count()</h3>    
  5.     
  6.     foreach (var item in Model)    
  7.     {    
  8.         <div style="border:solid 1px #808080; margin-bottom:2%;">    
  9.             <div class="row">    
  10.                 <div class="col-md-2">    
  11.                     <strong>Name</strong>    
  12.                 </div>    
  13.                 <div class="col-md-5">    
  14.                     <span>@item.Name</span>    
  15.                 </div>    
  16.             </div>    
  17.             <div class="row">    
  18.                 <div class="col-md-2">    
  19.                     <strong>Shift</strong>    
  20.                 </div>    
  21.                 <div class="col-md-5">    
  22.                     <span>@item.Shift</span>    
  23.                 </div>    
  24.             </div>    
  25.             <div class="row">    
  26.                 <div class="col-md-2">    
  27.                     <strong>Department</strong>    
  28.                 </div>    
  29.                 <div class="col-md-5">    
  30.                     <span>@item.Department</span>    
  31.                 </div>    
  32.             </div>    
  33.             <div class="row">    
  34.                 <div class="col-md-2">    
  35.                     <strong>Group Name</strong>    
  36.                 </div>    
  37.                 <div class="col-md-5">    
  38.                     <span>@item.GroupName</span>    
  39.                 </div>    
  40.             </div>    
  41.             <div class="row">    
  42.                 <div class="col-md-2">    
  43.                     <strong>Start Date</strong>    
  44.                 </div>    
  45.                 <div class="col-md-5">    
  46.                     <span>@item.StartDate</span>    
  47.                 </div>    
  48.             </div>    
  49.             <div class="row">    
  50.                 <div class="col-md-2">    
  51.                     <strong>End Date</strong>    
  52.                 </div>    
  53.                 <div class="col-md-5">    
  54.                     <span>@item.EndDate</span>    
  55.                 </div>    
  56.             </div>    
  57.         </div>    
  58.     }    
  59. }    
I think the possible mistake is returning partial-view on drop down selection changed.
  1. [HttpPost]    
  2.         [ValidateAntiForgeryToken]    
  3.         public ActionResult EmployeeDeptHistory(FormCollection form)    
  4.         {    
  5.             IEnumerable<EmployeeDepartmentHistory> empHistList;    
  6.             using (IDbConnection con = new SqlConnection(connectionString))    
  7.             {    
  8.                 empHistList = con.Query<EmployeeDepartmentHistory>("sp_StoredProc"new { DeptId = form["DepartmentName"] }, commandType: CommandType.StoredProcedure);    
  9.             }    
  10.             return View("_EmployeeDeptHistory", empHistList);    
  11.         }   
Please help me to figure out my mistake.

Answers (3)