【问题标题】:Send Dropdown Selected Value from View to Controller using "DropDownListFor" in MVC使用 MVC 中的“DropDownListFor”将下拉选择值从视图发送到控制器
【发布时间】:2019-04-01 15:36:27
【问题描述】:

无法将选定的值从视图发送到控制器。我正在使用 DropdownlistFor 发送带有 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 = "这个值无效", @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 必须具有模型中的 id 和 name 属性。因此,将“SelectedStatus”作为 id 和 name 属性添加到您的下拉列表中。
  • 我的模型有:- public string SelectedStatus { get;放; } public List FilterStatusList { get;放; },当我在视图中添加 SelectedStatus 时,出现错误,内容不存在

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


【解决方案1】:

DropdownList 必须具有模型中的 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 " })

【讨论】:

  • 我再次遇到同样的问题:- List 不包含 SelectedStatus 的定义
  • 你在哪里得到错误,无论是在控制器中还是在视图中?我需要澄清您是提交 List 还是仅提交 ReportModel 的一个实例?
  • 请在action public ActionResult ReportList(ReportModel reportmodel)中检查reportModel对象是否有空值
  • 我在查看页面出现错误。我提交 List 不是报告模型
  • 但是您在控制器中的操作不适用于 List
猜你喜欢
  • 2018-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-03
  • 2021-04-09
  • 1970-01-01
  • 2014-08-22
  • 1970-01-01
相关资源
最近更新 更多