Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am trying to get the selected value of dropdownlist in mvc,
In my view I have textbox and dropdwnlist I am getting textxboxes value in controller but not getting dropdownlist selected value.

Please help me to know what I am missing.

I have below code.

In Model
C#
public class ValveCertificateModel
    {
        public string Leakage { get; set; }
        public int Job { get; set; }
    }

In ViewModel
C#
public class ValveCertificateViewModel
    {
        public ValveCertificateModel ValveCertificateData { get; set; }
        public IEnumerable<TableSignalModel> SignalModel { get; set; }
    }

C#
public class TableSignalModel
   {
       public string SignalName { get; set; }
   }


In Controller
C#
public ActionResult Index()
       {
           var valveFields = CertificateRepository.GetValveFields();
           return View(valveFields);
       }

       [HttpPost]
       public ActionResult Display(ValveCertificateViewModel list)
       {
           //TODO
           return View(paraList);
       }


What I have tried:

In View

HTML
@using (Html.BeginForm("Display", "Certificate", FormMethod.Post))
{
@Html.LabelFor(m => m.ValveCertificateData.Job, new { @class = "labelColor" }) :
                                                @Html.TextBoxFor(m => m.ValveCertificateData.Job, new { @class = "form-control" })

@Html.DropDownListFor(m => m.ValveCertificateData.Leakage, new List(Model.SignalModel, "", "SignalName", Model.ValveCertificateData.Leakage),"Select one", new { @class = "form-control" });
}




When I run this I am getting list of SignalName in dropdwnlist, and on select the itemand on submit button click, In controller In list I am getting null for leakage property.
but I am getting textbox value in controller.
Posted
Updated 9-Jul-17 23:48pm
Comments
F-ES Sitecore 4-Jul-17 4:48am    
It worked ok for me, although in the view I had to change

new List(Model.SignalModel...

to

new SelectList(Model.SignalModel...

1 solution

As suggested by 'F-ES-SiteCore', change the new List() to new SelectListItem(). This should work.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900