Click here to Skip to main content
16,019,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please guy help me I am stuck from past 1 week in dropdownlist.
I have a page where is a dropdown. When an item is selected in drop down then data is selected from database table related to that item and displayed in a table on same page.
Please explain full with model, view and controler. I am very new in asp.net.

Thanks in Advance,
Posted

1 solution

Try this solution and change as per your requirement

View Model will be :
C#
public class User
 {
     public int Id { set; get; }
     public string Name { get; set; }
     public int CompanyId { get; set; }
 }


Controller Will be like:
public class HomeController : Controller
   {
       private const string companyIdHiddenValue = "companyIdHiddenValue";

       private List<User> Users;

       public HomeController()
       {
           // don't get all user list here. I just added for demonstration.
           this.Users = new List<User>()
                            {
                                new User(){Id = 1, Name = "User1", CompanyId = 1},
                                new User(){Id = 1, Name = "User2", CompanyId = 5},
                                new User(){Id = 1, Name = "User3", CompanyId = 4},
                                new User(){Id = 1, Name = "User4", CompanyId = 3},
                                new User(){Id = 1, Name = "User5", CompanyId = 2},
                                new User(){Id = 1, Name = "User2", CompanyId = 1},
                                new User(){Id = 1, Name = "User3", CompanyId = 5},
                                new User(){Id = 1, Name = "User4", CompanyId = 2},
                                new User(){Id = 1, Name = "User5", CompanyId = 2},
                                new User(){Id = 1, Name = "User2", CompanyId = 3},
                                new User(){Id = 1, Name = "User3", CompanyId = 4},
                                new User(){Id = 1, Name = "User4", CompanyId = 4},
                                new User(){Id = 1, Name = "User5", CompanyId = 2},
                                new User(){Id = 1, Name = "User2", CompanyId = 5},
                                new User(){Id = 1, Name = "User3", CompanyId = 1},
                                new User(){Id = 1, Name = "User4", CompanyId = 1},
                                new User(){Id = 1, Name = "User5", CompanyId = 1},
                            };
       }

       public ActionResult List()
       {
           this.GetCompany();
           return this.View();
       }

       [HttpPost]
       public ActionResult List(string companyId)
       {
           this.GetCompany(companyId);
           var cId = Convert.ToInt32(companyId, CultureInfo.InvariantCulture);
           return this.PartialView(this.Users.Where(x => x.CompanyId == cId).ToList());
       }

       private void GetCompany(string companyId = null)
       {
           var companyList = new List&lt;SelectListItem>
           {
               new SelectListItem
                   {
                       Selected = true,
                       Text = "Select",
                       Value = string.Empty,
                   }
           };

           companyList.AddRange(new[]
                                    {
                                        new SelectListItem { Text = "Comapny1", Value= "1"},
                                        new SelectListItem { Text = "Comapny2", Value = "2"},
                                        new SelectListItem { Text = "Comapny3", Value = "3"},
                                        new SelectListItem { Text = "Comapny4", Value = "4"},
                                        new SelectListItem { Text = "Comapny5", Value = "5"}
                                    });

           var firstOrDefault = companyList.FirstOrDefault(x => x.Value == companyId);

           if (firstOrDefault != null)
           {
               firstOrDefault.Selected = true;
           }

           ViewBag.CompanyDropDown = companyList;
       }
   }


Views are :

List.cshtml

C#
@using MvcApplication1.Models
@model List<mvcapplication1.models.user>

@{ Html.RenderPartial("UserFilter"); }

@Html.Hidden("companytIdHiddenValue")
<div id="divUserList">
    @{Html.RenderPartial("UserList", Model ?? new List<user>());}
</user></div>
</mvcapplication1.models.user>



UserList.cshtml
C#
@using MvcApplication1.Models
@model List<user>
<table>
    <tr>
        <th>Id</th>
        <td>Name</td>
        <td>Comapny Id</td>
    </tr>
    @foreach (var user in Model)
    {
        <tr>
            <td>@user.Id</td>
            <td>@user.Name</td>
            <td>@user.CompanyId</td>
        </tr>  
    }
</table>
</user>


UserFilter.cshtml

C#
<script src="~/Scripts/jquery-1.8.2.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $("#CompanySelection").change(function () {
            $("#companyId").val($(this).val());
        });
    });

    function CheckUserValidation() {
        if ($('#companyId').val() == undefined || $('#companyId').val() == '') {
            alert('please select a company');
        }
    }

    function successUserAjax() {
        alert('ajax completed');

    }

    function onAjaxFailure() {
        alert('please try again');
    }
</script>

@using (Ajax.BeginForm("List", "Home", new AjaxOptions { UpdateTargetId = "divUserList", HttpMethod = "POST", OnSuccess = "successUserAjax", OnFailure = "onAjaxFailure" }))
{
    <ul>
        <li>
            <label>Company:<label>
            @Html.DropDownList("CompanySelection", (IEnumerable<SelectListItem>)ViewBag.CompanyDropDown)
            @Html.Hidden("companyId")
        </label></label></li>
    </ul>
    <input type="submit" value="Get" name="filter" onclick="return CheckUserValidation();" />
}



</script>
 
Share this answer
 

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