【问题标题】:Cascading DropDownList and ListBox in ASP.Net MVCASP.Net MVC 中的级联 DropDownList 和 ListBox
【发布时间】:2015-01-13 05:39:50
【问题描述】:

我正在尝试 Asp.Net MVC 中的级联 dropdowlist 和列表框示例。我已经看到了单独的级联下拉列表的示例,但是它使用 JSON 来填充第二个下拉列表。所以,请不要将此问题标记为重复。

我有一个用于显示地区/县的下拉列表和一个列表框,用于在使用模型预先填充的视图页面中显示客户。当我在下拉列表中选择县/区时,我需要在列表框中获取过滤的客户列表,通过使用查询,我更喜欢 linq。

我的问题是我应该在控制器的哪里编写查询?

型号:

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; }
}

控制器:

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);
}

查看:

<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">
      &nbsp;
    </td>
    <td>
      <input id="btnsearch" class="button" type="submit" value="Show" />
    </td>
  </tr>
</table>

【问题讨论】:

  • 您不想使用javascript/jquery(在这种情况下,您每次选择一个地区时都需要提交表单)?而且您的视图不包括该地区的下拉列表?
  • 请查看问题中的编辑。不幸的是,我复制粘贴了一个不同的视图。
  • 所有教程都描述了使用 Json 使用 javascript 处理下拉列表。
  • 为什么不想使用 javascript/jquery 来填充列表框?

标签: c# asp.net-mvc asp.net-mvc-4 listbox


【解决方案1】:

例如,我在这里放了一些编码。请按赞。

$('#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));
                });
        }
    });
})

我的控制器动作方法

[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 .
}

【讨论】:

  • 请试试上面的代码,我现在已经更新了。请尝试这样,让我知道。
猜你喜欢
  • 2021-07-04
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多