简体   繁体   中英

Send Dropdown Selected Value from View to Controller using "DropDownListFor" in MVC

Unable to send selected Value from view to controller.I am using DropdownlistFor to send data with From Tag.

@using (var form = Html.Bootstrap().Begin(new Form("ReportList", "Report").Type(FormType.Vertical).FormMethod(FormMethod.Post).HtmlAttributes(new { @id = "reportForm", @data_bv_message = "This value is not valid", @data_bv_feedbackicons_valid = "fa fa-check", @data_bv_feedbackicons_invalid = "fa fa-times", @data_bv_feedbackicons_validating = "fa fa-refresh", area = "Admin" }))) {

                        <div class="col-md-2">@Html.Label("Status:", new { @class = "" })  </div>
                        <div class="col-md-4">@Html.DropDownListFor(a => a.Status, new List<SelectListItem>
                                                                  { new SelectListItem(){ Text="NEW", Value = "NEW" },
                                                                    new SelectListItem(){ Text="PAID", Value = "PAID" },
                                                                    new SelectListItem(){ Text="PENDING", Value = "PENDING" },
                                                                    new SelectListItem(){ Text="FAULT", Value = "FAULT" },
                                                                    new SelectListItem(){ Text="PROCESS", Value = "PROCESS" }
                                                                  }, "Select Status", new { @class = "form-control " })</div>

                        <div class="col-md-2"><button type="submit" class="btn btn-success">Submit</button> </div>

                        </div>

                        }

 public ActionResult ReportList()
        {
            List<ReportModel> reportList = new List<ReportModel>();

            string FilterStatusList = reportmodel.SelectedStatus;

            List<InsuranceDetails> insuranceList = _insuranceDetailsService.GetAllInsurances().ToList();

            //Stroed Procedure calling
            // List<InsuranceDetails> filterdata = _insuranceDetailsService.GetDatatableFilterbyStatus().Where(a => a.InsuranceStatus.Equals("DropdownValue")).ToList();
            try
            {
                foreach (InsuranceDetails currentitem in insuranceList)
                {
                    ReportModel currentItemmodel = new ReportModel();

                    currentItemmodel.InsuranceId = currentitem.InsuranceId;
                    currentItemmodel.EncodedInsuranceId = Encode(currentitem.InsuranceId.ToString());
                    currentItemmodel.PlateNumber = currentitem.PlateNumber;
                    currentItemmodel.ReceiptNumber = currentitem.ReceiptNumber;
                    currentItemmodel.VehicleType = currentitem.VehicleType;
                    currentItemmodel.CustomerName = currentitem.CustomerName;
                    currentItemmodel.InsurancePreiod = string.Concat(currentitem.PeriodStartDate, "-", currentitem.PeriodEndDate);
                    currentItemmodel.UserName = currentitem.UserName;
                    currentItemmodel.BranchName = currentitem.BranchName;
                    currentItemmodel.TotalPremiumPrice = currentitem.TotalPremiumPrice;
                    currentItemmodel.InvoiceNumber = currentitem.InvoiceNumber;
                    currentItemmodel.InsuranceStatus = currentitem.InsuranceStatus;
                    currentItemmodel.UploadedImagePath = string.IsNullOrEmpty(currentitem.InvoiceImage) ? "" : ImageBaseUrl + "ImageUploads/" + "OriginalSize/" + currentitem.InvoiceImage;      
                    reportList.Add(currentItemmodel);
                }

            }
            catch (Exception e)
            {
                string message = AppLogger.CreateMessage(e);
                AppLogger.LogFileWrite(message);
            }
            TempData["ReportList"] = reportList;
            return View(reportList);
        }

DropdownList must have id property as in your Model. So change 'Status' to 'SelectedStatus' as below

@Html.DropDownListFor(model => model.SelectedStatus, new List<SelectListItem>
                                                                  { new SelectListItem(){ Text="NEW", Value = "NEW" },
                                                                    new SelectListItem(){ Text="PAID", Value = "PAID" },
                                                                    new SelectListItem(){ Text="PENDING", Value = "PENDING" },
                                                                    new SelectListItem(){ Text="FAULT", Value = "FAULT" },
                                                                    new SelectListItem(){ Text="PROCESS", Value = "PROCESS" }
                                                                  }, "Select Status", new { @class = "form-control " })

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