使用MVC中的“ DropDownListFor”从视图向控制器发送下拉选择值

时间:2019-03-29 12:09:55

标签: c# asp.net-mvc entity-framework

无法将选定的值从视图发送到控制器。我正在使用DropdownlistFor通过From Tag发送数据。

@using(可变格式= Html.Bootstrap()。Begin(new Form(“ ReportList”,“ Report”)。Type(FormType.Vertical).FormMethod(FormMethod.Post).HtmlAttributes(新{@id = “ reportForm”,@ data_bv_message =“此值无效”,@ data_bv_feedbackicons_valid =“ fa fa-check”,@ data_bv_feedbackicons_invalid =“ fa fa-times”,@ data_bv_feedbackicons_validating =“ fa fa-refresh”,区域=“管理员” })))                     {                         

                        <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);
        }

1 个答案:

答案 0 :(得分:1)

DropdownList必须具有Model中的id属性。因此,将“状态”更改为“已选择状态”,如下所示:

@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 " })