简体   繁体   English

选择DropDownList值Razor后显示列表

[英]Show list after select DropDownList value Razor

I've been working for a while on this, I know how to resolve it using JQuery, but I need to solve this just using server side code, I'm in a Razor View The thing is: 我已经为此工作了一段时间,我知道如何使用JQuery来解决它,但是我只需要使用服务器端代码来解决它,我就在Razor View

I have an @Html.DropDownlist that is showing some States from USA, and once clicked one of the States from the DropDownList then I want to show some cities that belong to the State selected using other DropDownList, I'm not sure how to get the value from the selected field just using Razor syntax and then show the cities that belong to the State in other DropDownList when one State is selected, I'm using a SelectList and I have an StateID to bind the cities... I'm showing all the States inside a DropDownList that is working. 我有一个@Html.DropDownlist ,它显示了来自美国的某些州,并且一旦单击了DropDownList一个州,然后我想显示属于使用其他DropDownList选择的州的一些城市,我不确定如何获取仅使用Razor syntax从选定字段中获取的值,然后在选择一个州时在另一个DropDownList中显示属于该州的城市,我使用的是SelectList ,我有一个StateID来绑定城市...显示正在运行的DropDownList内的所有状态。 Here is my code: 这是我的代码:

These are just two classes that I'm using to fill the SelectList with some properties: 这些只是我用来用某些属性填充SelectList的两个类:

 public States(int id, string name, List<string> list)
        {
            StateID = id;
            Name = name;
            Cities = list;
        }
        public int StateID { get; set; }
        public string Name { get; set; }
        public List<string> Cities { get; set; }
    }


    public static class Fill
    {
        public static List<States> GiveMeStates()
        {
            List<States> li = new List<States>() {
             new States(1, "Alabama",new List<string> {"Adamsville", "Addison", "Anderson","Anniston", "Arab" }),
             new States(2,"Alaska", new List<string> {"Anchorage","Juneau","Fairbanks","Sitka"}),
             new States(3,"Arizona", new List<string> { "Avondale", "Benson", "Besbee"})
            };
            return li;
        }
    }

And now this is my Razor View: 现在这是我的剃刀视图:

@using RazorMVC.Models;

@{ 
    List<States> aux = Fill.GiveMeStates();   
    SelectList states = new SelectList(aux, "StateID", "Name");
}
<form>
    @Html.DropDownList("ddlStates", states);

</form>

If you absolutely do not want to use javascript/jQuery, you may submit the form (with the selected state id) and get the states based on the posted state id and show that. 如果您绝对不想使用javascript / jQuery,则可以提交表单(具有选定的状态ID),并根据发布的状态ID获取状态并进行显示。

Assuming you want to show the cities for the selected state in the same view. 假设要在同一视图中显示所选州的城市。

@{

  var stateId = Request.QueryString["ddlStates"] as string;
  List<States> aux = Fill.GiveMeStates();
  SelectList states = new SelectList(aux, "StateID", "Name");
  List<SelectListItem> cities = new List<SelectListItem>();
  if (!String.IsNullOrEmpty(stateId))
  {       
    var state = aux.FirstOrDefault(f => f.StateID == Convert.ToInt32(stateId));
    if (state != null)
    {
        cities = state.Cities
                      .Select(x => new SelectListItem {Value = x, Text = x}).ToList();
    }
  }
}
<label>Select a state and hit submit </label>
@using (Html.BeginForm("Index", "Home", FormMethod.Get)) 
{
  @Html.DropDownList("ddlStates", states)
  <label>Cities < /label>
  @Html.DropDownList("City", cities)
  <input type="submit" />
}

I personally prefer to not put a lot of C# code in the razor views. 我个人不希望在剃刀视图中放置很多C#代码。 I usually create a view model and use that to pass the values needed in the view. 我通常创建一个视图模型,并使用该模型来传递视图中所需的值。 So most of the above code you see in the view goes in my action method. 因此,您在视图中看到的大多数上述代码都放在我的action方法中。

If you prefer to use jQuery/javascript (Why not ?), You may listen to the change event of the the first dropdown ,get the selected option value and send that to server via an ajax call. 如果您更喜欢使用jQuery / javascript(为什么?),则可以侦听第一个下拉列表的change事件,获取选定的选项值,然后通过ajax调用将其发送到服务器。 Let your server action method returns the states in json format and your ajax metod's call back can parse the json data and update the cities dropdown. 让您的服务器操作方法以json格式返回状态,而您的ajax metod的回调可以解析json数据并更新城市下拉列表。 Here is a sample to start with 这是一个示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM