ChrilleDahl
ChrilleDahl

Reputation: 13

How can I post my value from a selectform without using Dropdownlistfor in MVC4?

I don't know how much code is needed in order for you to understand my problem so I'm starting with explaining what I want to do. I have a form in my View where I create a new object. In this form I want to use a dropdownlist and I've tried a lot with @Html.Dropdownlistfor() but realized I don't want to use that since I don't know how to use som Jquery UI-code on this and I don't know how to send a different value than what is shown in the dropdownlist. Does any of you know how i just send the value selected in the dropdownlist to my controller?

Upvotes: 1

Views: 516

Answers (2)

vborutenko
vborutenko

Reputation: 4443

In view model:

puclic class ViewModel
{
   int SelectId{get;set;}
}

In view

<select name="SelectId" id="SelectId">
  <option value='1'>one</option>
  <option value='2'>Two</option>
</select>

After post form ViewModel.SelectId will contain value of selected item in dropdown

Upvotes: 0

Shyju
Shyju

Reputation: 218852

DropDownListFor helper method esssentially render a SELECT element. So if you do not want to use the helper method, You may write the RAW HTML code

@model CreateUserVM
@using(Html.BeginForm())
{
    <select name="SelectedCity" id="SelectedCity">
      <option value='1'>Ann Arbor</option>
      <option value='2'>Novi</option>
      <option value='3'>Detroit</option>
    </select>
}

Make sure your SELECT elements name property value is same as what you use in your Model, so that Model binding will work.

public class CreateUserVM
{
   public int SelectedCity { set;get;}
  //other properties
}

Upvotes: 1

Related Questions