简体   繁体   中英

Need to get value from DropDownListFor asp.net mvc

I have combobox in view and I need to get select value in combobox.

Code View:

@Html.DropDownListFor(model => model.servicesModel, (SelectList)ViewBag.serviceNames, new { @id = "listNames" })

Code action that added values in combobox(I know, I missed the symbol in database when created table):

var servicesDetails = dbModel.SERVICES.ToList();

        ViewBag.serviceNames = new SelectList(servicesDetails, "iIdSevices", "vName");

I have form, press button and after that thanks to @using (Html.BeginForm("SendApplication", "Home", FormMethod.Post)) I send data to My actionResult.

My ActionResult:

 [HttpPost]
    public ActionResult SendApplication(App_AppTemp_Serv_PersInfModel appFile, int iIdServices)
    {           
        int iIdService = 0;            

        var serviceDetails = dbModel.SERVICES.Where(x => x.iIdSevices == iIdServices).FirstOrDefault();
        iIdService = serviceDetails.iIdSevices;

        if (bFile == true)
        {
            appFile.appModel.vFile = "Приклепленные файлы имеются";
        }
        else
        {
            appFile.appModel.vFile = "Приклепленных файлов нет";
        }            

        appFile.appModel.vDate = Convert.ToString(DateTime.Now);

        try
        {
            dbModel.APPLICATIONS.Add(appFile.appModel);
            dbModel.SaveChanges();
            ModelState.Clear();
        }
        catch (Exception exc)
        {
            ViewBag.sErrorMessage = exc.Message;               
        }

        return View("~/Views/Home/ProblemForm.cshtml", appFile);
    }

But I can't to get the data from combobox, I don't know how?! Maybe use class and get class value, anyone have a idea for this?

Hi you can get the data from dropdownlist using the name of that control. It seems that in your example model.servicesModel will be the name of dropdownlist. so in your controller get data using that property .

for ex: Assuming App_AppTemp_Serv_PersInfModel is your model, if no, change it accordingly

[HttpPost]
    public ActionResult SendApplication(App_AppTemp_Serv_PersInfModel appFile, int iIdServices)
    {  

            //dropdownlist selected value

            string selectedvalue =   appFile.servicesModel

           //other code
}

Hope it was helpful

Thanks

Karthik

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM