You may not get this easily with the default model binding. You should have a little workaround like this.
1) Add a new property to the model / view mode to save the selected text
public class DocumentModel { public int TypeID { get; set; } public List<SelectListItem> DocumentTypes { get; set; } public string SelctedType { set;get;} }
2) Use the Html.HiddenFor
Helper to create a hidden variable in the form for this property
@Html.HiddenFor(x => x.SelctedType)
3) Use a little javascript to override sending! i.e; When the user submits the form, pull the selected text from the drop-down list and set this value as the value of the hidden field.
$(function () { $("form").submit(function(){ var selTypeText= $("#TypeID option:selected").text(); $("#SelctedType").val(selTypeText); }); });
Now in your HTTPPost
action method, this will be available in the SelectedType
property.
[HttpPost] public void UploadDocument(DocumentModel model) { if(ModelState.IsValid) { string thatValue=model.SelectedType; } }
Shyju source share