简体   繁体   English

从下拉列表中选择的值

[英]Selected Value from DropdownList

I am trying to get the selected value from a dropdownlist which is populated from a database. 我正在尝试从数据库填充的下拉列表中获取选定的值。 The list populates fine and allows me to select a value, but when I click the submit button, I get an error that the list "is not set to instance of an object" 该列表可以很好地填充,并允许我选择一个值,但是当我单击“提交”按钮时,出现错误消息,该列表“未设置为对象的实例”

Here is my view: 这是我的看法:

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

Here is my controller: 这是我的控制器:

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

Here is the Model: 这是模型:

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

Any assistance is greatly appreciated! 非常感谢您的协助!

At your view you are expecting an viewmodel with information but you returning nothing to the view. 在您的视图上,您​​期望的是带有信息的视图模型,但是您不向视图返回任何内容。

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

Asks for a filled list of loan purposes. 要求填写贷款目的清单。

But because the model is null, it simply crashes. 但是由于该模型为null,因此只会崩溃。

To solve this problem you need to pass a new instance of the model: LoanRequestRowViewModel 要解决此问题,您需要传递模型的新实例:LoanRequestRowViewModel

Also i would take to consideration to rename: Model.LoanPurpose. 我还要考虑重命名:Model.LoanPurpose。 It sounds to me that it just returns one property, not a list of items. 在我看来,它只返回一个属性,而不是项目列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM