DropDownListFor ViewModel中Collection的属性

时间:2014-09-12 13:00:28

标签: c# asp.net-mvc asp.net-mvc-4

我是ASP.NET MVC的新手,并且/我不确定如何解释我的问题。我必须创建一个页面,用户可以在其中添加多个附属项目列表到贷款申请。每个抵押品的每个“行”需要几个下拉列表来选择抵押品的类型,其类别等。该页面基于ViewModel:

public class CollateralViewModel
{
    public Guid LoanApplicationId { get; set; }
    public IEnumerable<CollateralRowViewModel> Collateral { get; set; }
}

IEnumerable Collat​​eral获得以下内容:

public class CollateralRowViewModel
    {
        public Guid Id { get; set; }

        public Guid LoanApplicationId { get; set; }

        public IEnumerable<SelectListItem> CollateralClass { get; set; }
        public IEnumerable<SelectListItem> CollateralType { get; set; }

        public Guid SelectedCollateralType { get; set; }
        public Guid SelectedCollateralClass { get; set; }

        [DataType(DataType.MultilineText)]
        public string Description { get; set; }

        public decimal? MarketValue { get; set; }
        public decimal? PriorLiens { get; set; }
        public decimal? AdvanceRate { get; set; }

        public string GrantorFirstName { get; set; }
        public string GrantorMiddleName { get; set; }
        public string GrantorLastName { get; set; }
    }

我的控制器看起来像这样:

 public async Task<ActionResult> Create(CollateralViewModel collateralViewModel)
        {
            var collateralServiceProxy = base.ServiceProvider.CollateralServiceProxy;
            var collateralTypes = await GetCollateralTypesByClass(Guid.NewGuid());

            var selectedCollateral = collateralViewModel.Collateral.Select(collateral => new Collateral()
            {
                Id = collateral.Id,
                LoanApplicationId = collateral.LoanApplicationId,
                CollateralTypeId = collateral.SelectedCollateralType,
                Description = collateral.Description,
                GrantorFirstName = collateral.GrantorFirstName,
                GrantorMiddleName = collateral.GrantorMiddleName,
                GrantorLastName = collateral.GrantorLastName,
                PriorLiens = collateral.PriorLiens,
                MarketValue = collateral.MarketValue
            });

            foreach (var collateral in selectedCollateral)
            {
                await collateralServiceProxy.PutCollateralAsync(collateral);
            }
            return View(collateralViewModel);
        }

        private async Task<IEnumerable<SelectListItem>> GetCollateralClasses()
        {
            var collateralServiceProxy = base.ServiceProvider.CollateralServiceProxy;
            var collateralClasses = await collateralServiceProxy.GetAllCollateralClassesAsync();

            if (collateralClasses == null)
            {
                return new List<SelectListItem>();
            }
            return collateralClasses.ToSelectList();
        }

        private async Task<IEnumerable<SelectListItem>> GetCollateralTypesByClass(Guid collateralClassId)
        {

            var allCollateralTypes = await GetAllCollateralTypes();
            var selectedCollateralTypes = allCollateralTypes.Where(collateralType => Guid.Parse(collateralType.Value).Equals(collateralClassId));

            return selectedCollateralTypes;
        }

当我尝试使用@ Html.DropDownListFor(model =&gt; model.Collat​​eral.Collat​​eralClasses)时我不能,因为Collat​​eralClasses不可用。我输入“(model.Collat​​eral。”,属性不在那里。我在这里做错了什么?

非常感谢任何帮助!!

2 个答案:

答案 0 :(得分:1)

请检查syntex的下拉列表。我认为这可能是问题(因为你没有指定错误)。请检查syntex

@Html.DropDownListFor(m => m.ContribType, 
                new SelectList(Model.ContribTypeOptions, 
                               "ContribId", "Value", 
                               Model.ContribTypeOptions.First().ContribId))

所以你可以这样试试。在您的第一个位置“valueItRepresent”,它应该是下拉列表的属性

@Html.DropDownListFor(model => model.valueItRepresent, model.Collateral.FirstOrDefault().CollateralClasses‌ as SelectList, "Select")

答案 1 :(得分:0)

问题是渲染UI时未填充下拉列表。您必须在渲染数据之前正确填充下拉列表。这可以通过多种方式完成,但我建议您阅读以下文章。

article为您的问题提供解决方案

http://odetocode.com/blogs/scott/archive/2013/03/11/dropdownlistfor-with-asp-net-mvc.aspx

希望这有帮助。