Alex
Alex

Reputation: 233

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

Upvotes: 0

Views: 1972

Answers (2)

Rohit Arora
Rohit Arora

Reputation: 2252

It does not matter , you can get values of both by same jquery code. THis is the code via javascript to get

$('#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 . Example: In model you have

public int  DistrictId { get; set; }

Your select should be like this

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

and in Razor syntax

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

Upvotes: 1

jfbouz23
jfbouz23

Reputation: 126

"I wanted to get DistrictId to pr object.It doesn't come."

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

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

Upvotes: 0

Related Questions