0

I have a webgrid contains a dropdown which contains different items for each user(Items are grouped). I want to get the selected values to the controller . How can I do that. Heres my ;

Model :

public SelectList AvailableDevices { get; set; }

View :

...

 var grid = new WebGrid(Model. ...
..
..

 grid.Column(header: "AvailableDevices", format: @item => Html.DropDownList("value", (IEnumerable<SelectListItem>)item.AvailableDevices)),

And I have a Submit Button

@using (Html.BeginForm("AssignUserDevices", "Device"))
{
    <input type="submit" value="setUserDevice" onchange="CheckSelectedDevices()" />
}

I want to set users device according to his user type. I know what his choices and send dropdown items according to his type. So each item in webgrid differs from each other.

And Also I dont know how to give indices to each item in webgrid.( I think we will need it.)

Im new at MVC so hope you will understand.

Thanks;

albatross
  • 455
  • 2
  • 8
  • 27

1 Answers1

0

What I got from our requirement that you want the selected item and have that item value in form field before posting if yes then you can follow as given.

  @Html.DropDownList("value", (IEnumerable<selectlistitem>)item.AvailableDevices), new      {@class="deviceclass"} )

 @using (Html.BeginForm("AssignUserDevices", "Device"))
 {
    <input type="hidden" value="" name="deviceId" id="deviceId" />
    <input type="hidden" value="" name="userId" />
    <input type="submit" value="setUserDevice" />
 }

  <script>
     $(".deviceclass").on('change', function () {
        var dropdownvalue = $(this).val();
        $('#deviceId').val(dropdownvalue);
    })
</script>

and you can define an action function in controller

public ActionResult Details(string deviceId, string userId)
    {
        // do as you need. 
        return View();
    }
Sandeep
  • 1
  • 1
  • Assume that I have 5 users and 3 devices. Now I selected for the 1st user to 2nd device and 2nd user to 3rd device and 4th user to 1st device. In this method I can only send 1 device (whichever is the last one) How can I detect which device is attended to which user. Thanks – albatross Jul 22 '14 at 11:10