Try this solution and change as per your requirement
View Model will be :
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()
{
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<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
@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
@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
<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>