DropdownList中的选定值

时间:2014-09-29 18:15:25

标签: c# asp.net-mvc razor

我正在尝试从数据库中填充的下拉列表中获取所选值。该列表填充正常并允许我选择一个值,但是当我单击提交按钮时,我收到一个错误,列表“未设置为对象的实例”

以下是我的观点:

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

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

在您的视图中,您期望一个包含信息的视图模型,但您不会向视图返回任何内容。

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

要求填写贷款用途清单。

但是因为模型为空,它只是崩溃。

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

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