user2962142
user2962142

Reputation: 2066

How to get text from DropDownList in .net-MVC?

Alright so i was using a text box like this :

@Html.TextBoxFor(u=>u.Time)

now i wanna replace the text box with a drop down list, i did this :

@Html.DropDownList("FromDD",new SelectList (new[] {"first","second","third"}))

how can i get the text from the dropDownList and save it in the model (Model.Time) ?

Upvotes: 1

Views: 5240

Answers (4)

Murali Murugesan
Murali Murugesan

Reputation: 22619

@Html.HiddenFor(m=>m.Time new{@id='hdnTime'})

Set this in client side from dropdown before you submit

$('#hdnTime').val($('#fromDD option:selected').text())

Upvotes: 4

user1047100
user1047100

Reputation:

Given your example, the value you are looking for will be posted as a string called FromDD. The string you use as first parameter will be the name of the property the data binder is going to bind your value to.
If you want you can use the strongly typed overload

@Html.DropDownListFor(p => p.Time, new SelectList (new[] {"first","second","third"}))

this will post back the data as a string named Time, exactly like your textbox did.

Upvotes: 1

Rudis
Rudis

Reputation: 1217

You can use

@Html.DropDownListFor(u => u.Time,new SelectList (new[] {"first","second","third"}))

Upvotes: 0

Dmytro Rudenko
Dmytro Rudenko

Reputation: 2524

you can pass it as a parameter in your action method

[HttpPost]
public ActionResult ActionName(string FromDD){

}

your form have to have action attribute set to ActionName:

<form action="ActionName">
...
@Html.DropDownList("FromDD",new SelectList (new[] {"first","second","third"}))

Upvotes: 0

Related Questions