Click here to Skip to main content
15,886,787 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

In my application I am having a dropdown selection When the dropdown values is changed I am binding the records in the Grid in a partial view based on the dropdownselection value. Now I want to show the Partial view page when the main view page is loaded so I used Html.Action("PartialViewName"), its working fine. In the main page I am having the Edit button to Edit the values in a grid when I click the Edit button the values in the dropdown changed to default value as "select", the last selected value is lost. How to solve this issue is there any mistake I have did in my code.

MainView.cshtml
@model DM_Tool_MVC_Site.ViewModels.GroupHeadViewModels
@{
    ViewBag.Title = "Group Heads";
}
<script type="text/javascript">
    $(document).ready(function () {
        //$('#GroupList').hide();
        $('#SelectedDivision').change(function () {
            var DivId = $('#SelectedDivision option:selected').val();           
            $.get('@Url.Action("GroupHeadIndexPartial")',
                { DivisionID: DivId }, function (data) {
                    //$('#GroupList').show();
                    $("#GroupList").html(data);
                });
            
        });
    });
</script>

<h2>User Management</h2>
@using (Html.BeginForm("Index", "GroupHead", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
    @Html.AntiForgeryToken()
    <h4>@ViewBag.Title</h4>
    <hr />   
    <div class="form-group">
        @Html.LabelFor(m => m.SelectedDivision, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.DropDownListFor(m => m.SelectedDivision, Model.GetDivision(), "--Choose Division--", new { @class = "form-control" })
        </div>
    </div>
     <div class="form-group">
         <input type="submit" name="Edit" value="Edit" class="btn btn-default" />
     </div>
    <div class="form-group" id="GroupList">            
        @Html.Action("GroupHeadIndexPartial")       
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" class="btn btn-default" value="Update" />
        </div>
    </div>
}

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}


GroupIndexPartial.cshtml
@model IEnumerable<DM_Tool_MVC_Site.ViewModels.GroupHeadList>

<table class="table table-striped">
    <tr>
        <th>
            Group
        </th>
        <th>
            Group Head
        </th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.GroupName)
            </td>
            <td>
                @Html.HiddenFor(modelItem => item.DivID)
                @Html.HiddenFor(modelItem => item.GroupId)
                @Html.DropDownListFor(modelItem => item.GroupHeadUserName, item.UsersSelectList(item.DivID,item.GroupId).Select(u => new SelectListItem
{
Text = u.Text,
Value = u.Value,
Selected = u.Text == item.GroupHeadUserName
}), "--Select Group Head--", item.EditMode ? (object)new { @class = "form-control" } : new { @readonly = "readonly", @class = "form-control" })
               
                @*@Html.DisplayFor(modelItem => item.GroupHeadUserName)*@
            </td>
        </tr>
    }

</table>


GroupHeadController
public class GroupHeadController : Controller
    {
        private DMContext db = new DMContext();       
        // GET: /GroupHead/
        public ActionResult Index()
        {
            var model = new GroupHeadViewModels();            
            return View(model);           
        }
        //Get: /GroupHeadPartial  Page Filling
        public PartialViewResult GroupHeadIndexPartial(string DivisionID)
        {            
            int Div=0;
            if (DivisionID != string.Empty)
            {
                Div = Convert.ToInt32(DivisionID);
            }
            var GroupHeads = (from gr in db.Groups
                              join gh in db.GroupHeads on new { DivisionId = Div, GroupId = gr.ID } equals new { DivisionId = gh.DivisionID, GroupId = gh.GroupID } into x1
                               from x in x1.DefaultIfEmpty()
                              join user in db.Users on new { DivisionId = x.DivisionID, UserId = x.UserID } equals new { DivisionId = user.DivisionID, UserId = user.Id } into y1
                               from y in y1.DefaultIfEmpty()
                              where gr.IsActive == true
                              select new GroupHeadList
                              {
                                  GroupName = gr.Value,
                                  GroupHeadUserName = y.EmployeeName,
                                  DivID=Div,
                                  GroupId=gr.ID,
                                  UserId=y.Id
                              });            
            return PartialView(GroupHeads);
        }       
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900