层叠的DropDownList和列表框在ASP.Net MVC [英] Cascading DropDownList and ListBox in ASP.Net MVC
问题描述
我想出Asp.Net MVC级联dropdowlist和列表框的样本。我看到单独级联dropdownlists的例子,但是,使用JSON来填充第二DropDownList的。所以,请不要标记这个问题为重复的。
我为显示区/县一个DropDownList和显示的客户在查看网页,其中使用的模型填充pre列表框。当我在DropDownList选择了一个县/区,我需要得到客户的过滤列表在列表框中,通过查询,我preFER LINQ。
我的问题是,我会写在控制器中的查询?
型号:
公共类ReportParameterCustomerCard
{
[必需(的ErrorMessage =请选择一个区)]
[显示(NAME =区)]
公众诠释区{搞定;组; }
公共System.Web.Mvc.SelectList languanges {搞定;组; }
公共字符串[] {selectedCustomer获得;组; }
公共IEnumerable的< SelectListItem> allCustomer {搞定;组; }
公共字符串的客户code {搞定;组; }
}
控制器:
公众的ActionResult CustomerbyDistrictReport()
{
ViewBag.ShowIFrame = FALSE;
ReportParameterCustomerCard CUS =新ReportParameterCustomerCard();
清单< SelectListItem>名单=新名单,LT; SelectListItem>();
Helpers.DataLayer DL =新Helpers.DataLayer();
数据集DS = dl.ReturnDataset([销售] [县]。,*);
的for(int i = 0; I< ds.Tables [0] .Rows.Count;我++)
{
list.Add(新SelectListItem {文本= ds.Tables [0] .Rows [I] [COUNTY_NAME]。的ToString(),值= ds.Tables [0] .Rows [I] [ROWNUM]。的ToString ()});
}
cus.languanges =新的SelectList(列表中,价值,文字);
cus.selectedCustomer = NULL;
cus.allCustomer = GetallCustomer();
返回查看(CUS);
}
查看:
<表ALIGN =中心的风格=宽度:37%;>
&所述; TR>
< TD类=STYLE1>
< H3>
< ASP:标签ID =Label4=服务器文本=区>< / ASP:标签>
< / H3 GT&;
< / TD>
&所述; TD>
<%= Html.DropDownListFor(A => a.District,Model.languanges - 选择One--,新{ID =ddlDistrict,风格=WIDTH:255px;高度:25px的;} )%GT;
< / TD>
< / TR>
&所述; TR>
< TD类=STYLE1>
< H3>
< ASP:标签ID =Label5=服务器文本=客户>< / ASP:标签>
< / H3 GT&;
< / TD>
&所述; TD>
<%= Html.ListBoxFor(A => a.selectedCustomer,Model.allCustomer,新{@class =选择,多个=多,风格=宽度:250像素;的tabindex =4} )%GT;
< / TD>
< / TR>
&所述; TR>
< TD类=STYLE1>
&安培; NBSP;
< / TD>
&所述; TD>
<输入ID =btnsearch级=按钮类型=提交值=显示/>
< / TD>
< / TR>
< /表>
例如我在这里放一些编码。请不要跟着喜欢它。
$('#firstDropDown')。在('变化',函数(){
VAR的getValue = $(本).VAL();
$阿贾克斯({
网址:'@ url.Action(secondDrodownFetchValue),新{myvalue的:的getValue}
输入:GET,
成功:函数(结果){
$。每个(结果,函数(指数值){
$('#secondDorpDown')追加。($('<选项>')文本(值).attr('值',值)。);
});
}
});
})
我的控制器的操作方法
[HTTPGET]
公众的ActionResult secondDrodownFetchValue(字符串myvalue的)
{
//你的code
返回列表<串GT;(); //这里我通过空值,你要通过客户的名单。
}
I am trying out a sample of cascading dropdowlist and listbox in Asp.Net MVC. I have seen examples of cascading dropdownlists alone, but that uses JSON to fill the second dropdownlist. So, please don't mark this question as duplicate.
I have a dropdownlist for displaying the districts/counties and a listbox that displays the customers in the View page which are pre populated using the model. When I chose an county/district in the dropdownlist, I need to get a filtered list of customers in the listbox, by using queries, I prefer linq.
My question is where would I write the query in the controller?
Model:
public class ReportParameterCustomerCard
{
[Required(ErrorMessage = "Please select a district")]
[Display(Name = "District")]
public int District { get; set; }
public System.Web.Mvc.SelectList languanges { get; set; }
public string[] selectedCustomer { get; set; }
public IEnumerable<SelectListItem> allCustomer { get; set; }
public string CustomerCode { get; set; }
}
Controller:
public ActionResult CustomerbyDistrictReport()
{
ViewBag.ShowIFrame = false;
ReportParameterCustomerCard cus = new ReportParameterCustomerCard();
List<SelectListItem> list = new List<SelectListItem>();
Helpers.DataLayer dl = new Helpers.DataLayer();
DataSet ds = dl.ReturnDataset("[Sales].[County]", "*");
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
list.Add(new SelectListItem { Text = ds.Tables[0].Rows[i]["County_Name"].ToString(), Value = ds.Tables[0].Rows[i]["RowNum"].ToString() });
}
cus.languanges = new SelectList(list, "Value", "Text");
cus.selectedCustomer = null;
cus.allCustomer = GetallCustomer();
return View(cus);
}
View:
<table align="center" style="width: 37%;">
<tr>
<td class="style1">
<h3>
<asp:Label ID="Label4" runat="server" Text="District"></asp:Label>
</h3>
</td>
<td>
<%=Html.DropDownListFor(a=>a.District,Model.languanges," --Select One-- ",new{id="ddlDistrict", style = "width: 255px;height:25px;" })%>
</td>
</tr>
<tr>
<td class="style1">
<h3>
<asp:Label ID="Label5" runat="server" Text="Customer"></asp:Label>
</h3>
</td>
<td>
<%=Html.ListBoxFor(a=>a.selectedCustomer,Model.allCustomer, new { @class = "chosen", multiple = "multiple", style = "width: 250px;", tabindex = "4" })%>
</td>
</tr>
<tr>
<td class="style1">
</td>
<td>
<input id="btnsearch" class="button" type="submit" value="Show" />
</td>
</tr>
</table>
For Example I am put some coding here. Please do follow like it.
$('#firstDropDown').on('change', function() {
var getValue = $(this).val();
$.ajax({
url: '@url.Action("secondDrodownFetchValue"), new { myValue : getValue }',
type: 'GET',
success: function (result) {
$.each(result, function (index, value) {
$('#secondDorpDown').append($('<option>').text(value).attr('value', value));
});
}
});
})
My controller action method
[HttpGet]
public ActionResult secondDrodownFetchValue(string myValue)
{
// your code
return List<string>(); // Here i pass the empty value You have to pass the list of customer .
}
这篇关于层叠的DropDownList和列表框在ASP.Net MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!