在mvc中自动发回的下拉列表 [英] dropdownlist with auto-post back in mvc
本文介绍了在mvc中自动发回的下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请伙计帮助我,我在下拉列表中被困在过去1周。
我有一个页面,其中有一个下拉列表。当在下拉列表中选择一个项目时,将从与该项目相关的数据库表中选择数据并显示在同一页面上的表格中。
请完整解释模型,视图和控制器。我是asp.net的新手。
先谢谢,
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,
推荐答案
试试这个解决方案根据您的要求更改
查看模型将是:
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()
{
// 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<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;
}
}
浏览次数:
List.cshtml
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
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
UserFilter.cshtml
<script src="~/Scripts/jquery-1.8.2.js"></script>
<script type="text/javascript">
(document).ready(function(){
(document).ready(function () {
( #CompanySelection)。change(function(){
("#CompanySelection").change(function () {
这篇关于在mvc中自动发回的下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文