从下拉列表中选择的值

本文关键字:选择 下拉列表 | 更新日期: 2023-09-27 18:25:02

我正试图从数据库中填充的下拉列表中获取所选值。列表填充良好,允许我选择一个值,但当我单击提交按钮时,我会收到一个错误,即列表"未设置为对象的实例"

这是我的观点:

@model LoanRequestRowViewModel
<h2>CreateTest</h2>
<div>
    @using (@Html.BeginForm())
    {
        <div>
            <span class="inline-block">
                @Html.LabelFor(model => model.LoanPurpose)
                @Html.DropDownListFor(model => model.LoanPurposeId, Model.LoanPurpose)
            </span>
            <span class="inline-block">
                @Html.LabelFor(model => model.AmountRequested)
                @Html.TextBoxFor(model => model.AmountRequested)
            </span>
            <span class="inline-block">
                @Html.LabelFor(model => model.RequestedRate, new { @class = "control-label" })
                @Html.TextBoxFor(model => model.RequestedRate, new { @class = "inputROW" })
            </span>
        </div>
        <div>
            <input type="submit" value="Create" />
        </div>
    }
</div>

这是我的控制器:

 [HttpGet]
        [Route("CreateTest")]
        public async Task<ActionResult> CreateTest()
        {
            var loanPurposes = await GetLoanPurposes();

            var model = new LoanRequestRowViewModel();
            model.LoanPurpose.Add(new SelectListItem { Text = "-Please Select-", Value = "-1" });
            foreach (var purpose in loanPurposes)
            {
                model.LoanPurpose.Add(new SelectListItem()
                {
                    Value = purpose.Value.ToString(),
                    Text = purpose.Text.ToString()
                });
            }
            return View(model);
        }
        [HttpPost]
        [Route("CreateTest")]
        public async Task<ActionResult> CreateTest(LoanRequestRowViewModel model)
        {
            var newLoan = new LoanRequest()
            {
                AmountRequested = model.AmountRequested,
                LoanPurposeId = model.LoanPurposeId
            };
            return View();
        }

这是型号:

public class LoanRequestRowViewModel
    {
        public LoanRequestRowViewModel()
        {
            LoanClasses = new List<SelectListItem>();
            LoanTypes = new List<SelectListItem>();
            LoanPurpose = new List<SelectListItem>();
            RateIndex = new List<SelectListItem>();
            PaymentPeriod = new List<SelectListItem>();
            PaymentType = new List<SelectListItem>();
        }
        public Guid Id { get; set; }
        public Guid LoanApplicationId { get; set; }
        public Guid LoanPurposeId { get; set; }
        public Guid LoanClassId { get; set; }
        [Display(Name = "Loan Class")]
        public IList<SelectListItem> LoanClasses { get; set; }
        public string SelectedLoanClass { get; set; }

        [Display(Name = "Loan Purpose")]
        public IList<SelectListItem> LoanPurpose { get; set; }

        public string SelectedLoanPurpose { get; set; }
        [Display(Name = "Amount Requested")]
        public decimal? AmountRequested { get; set; }
        [Display(Name = "Requested Rate")]
        public decimal? RequestedRate { get; set; }
        [Display(Name = "Loan Type")]
        public IList<SelectListItem> LoanTypes { get; set; }

        public string SelectedLoanType { get; set; }
        [Display(Name = "Rate Index")]
        public IList<SelectListItem> RateIndex { get; set; }
        public Guid SelectedRateIndex { get; set; }
        [Display(Name = "Origination Fee")]
        public decimal? OriginationFeePercentage { get; set; }
        [Display(Name = "Variable Rate Margin")]
        public decimal? VariableRateMargin { get; set; }
        [Display(Name = "Variable Rate Floor")]
        public decimal? VariableRateFloor { get; set; }
        [Display(Name = "Variable Rate Ceiling")]
        public decimal? VariableRateCeiling { get; set; }
        [Display(Name = "Payment Type")]
        public IList<SelectListItem> PaymentType { get; set; }

        public string SelectedPaymentType { get; set; }
        [Display(Name = "Payment Period")]
        public IList<SelectListItem> PaymentPeriod { get; set; }

        public int? SelectedPaymentPeriod { get; set; }
        [Display(Name = "Number of Payments")]
        public int? NumberOfPayments { get; set; }
        [Display(Name = "Amortization Term")]
        public decimal? AmortizationTerm { get; set; }

非常感谢您的帮助!

从下拉列表中选择的值

在您的视图中,您期望得到一个包含信息的视图模型,但您没有向视图返回任何信息。

@Html.DropDownListFor(model => model.LoanPurposeId, Model.LoanPurpose)

要求填写贷款用途清单。

但由于该模型为null,它只是崩溃了。

要解决这个问题,您需要传递一个新的模型实例:LoanRequestRowViewModel

此外,我还考虑重命名:Model.LoanPurpose。听起来它只返回一个属性,而不是项目列表。