简体   繁体   English

服务器端和客户端下拉列表

[英]Serverside & Client side dropdown lists

I have a Server side(html helper) & Client side 2 dropdown menus.when 1st dropdown menu's onchange event fires,Client side dropdown menu fired. 我有一个服务器端(html helper)和客户端2下拉菜单。当第一个下拉菜单的onchange事件触发时,客户端下拉菜单被触发。 then i need to get both dropdown's selected items. 然后我需要同时获取两个下拉菜单的选定项。

My View 我的观点

 @using (Html.BeginForm("BasicManagement", "Admin", System.Web.Mvc.FormMethod.Post))
    {
      //Dropdown ist 1
      @Html.DropDownList("UserId", ViewBag.SupervisorList as System.Web.Mvc.SelectList, "-- Select Supervisor --", new { @class = "form-control supervisors", id = "drpsupervisors", @onchange = "getDistricts()" })

    //Dropdown list 2
    <select id="dist" class="form-control"></select>//<-- District list loaded here

    }

Model 模型

public class PersonRole
{
    //some properties here
    public Guid DistrictId { get; set; }
}

Controller 调节器

 [HttpPost]
    public ActionResult BasicManagement(PersonRole pr)//<-- I wanted to get DistrictId to pr object.It doesn't come.
    {
      //Some code here
    }

It does not matter , you can get values of both by same jquery code. 没关系,您可以通过相同的jquery代码获取两者的值。 THis is the code via javascript to get 这是通过javascript获取的代码

$('#dist option:selected').val();
$('#drpsupervisors option:selected').val();

if you are using the form Submit then just give them the same names as they are in Model . 如果您使用的是Submit表单,则只需给它们提供与Model中相同的名称。 Example: In model you have 示例:在模型中

public int  DistrictId { get; set; }

Your select should be like this 您的选择应该像这样

<select name="DistrictId"></select>

and in Razor syntax 并采用Razor语法

@html.DropDownlistFor(x=>x.DistrictId)

"I wanted to get DistrictId to pr object.It doesn't come." “我想让DistrictId成为pr对象。它没有来。”

function getDistricts()
{
  //get ajax call however you're gettting Dropdown 2 data  
  //return a value distID for hidden model
   $("#districtID").val(distID);
}

Pass dropown 2 value to a hidden model 将dropown 2值传递给隐藏模型

 @Html.HiddenFor(model => model.DistrictId, new { @ID = "districtID" })

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

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