0

I want to to pass an array of int from Razor view to controller

I tried this it gave me one line "1","2" not two items

    public List<int> SelectedIDs { get; set; }

     @Html.HiddenFor(model => model.SelectedIDs, new { id = "hid" })

     $('#document').submit(function () {

        $("#hid").val($('#dropdownOne').val());

      });
1
  • 1
    Can you share controller code? Commented May 6, 2016 at 12:45

1 Answer 1

1

HiddenFor doesn't work with arrays and lists. You should create list of hidden inputs:

for(int i = 0; i < Model.SelectedIDs.Count(); i++)
{
    @Html.HiddenFor(model => model.SelectedIDs)
}

Note that i use for loop instead of foreach becouse foreach will break binding.

And then if you wrap your hidden filds with a form tag it will bind automatically to your model in controller on form POST.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you teo van kot for your help. but how i will assign value from dropdownlist to hide field like this $("#hid").val($('#dropdownOne').val());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.