C# 在MVC5中填充DropDownList
这是我的添加新产品视图模型的代码C# 在MVC5中填充DropDownList,c#,asp.net,asp.net-mvc,razor,asp.net-mvc-5,C#,Asp.net,Asp.net Mvc,Razor,Asp.net Mvc 5,这是我的添加新产品视图模型的代码 using AccessorizeForLess.Data; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Web; namespace AccessorizeForLess.ViewModels { public class AddNewProductViewModel { public s
using AccessorizeForLess.Data;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web;
namespace AccessorizeForLess.ViewModels
{
public class AddNewProductViewModel
{
public string Name { get; set; }
[DataType(DataType.MultilineText)]
public string Description { get; set; }
public decimal Price { get; set; }
public string AltText { get; set; }
public int Quantity { get; set; }
public string ImagePath { get; set; }
public HttpPostedFileBase Image { get; set; }
public List<ProductCategory> Category { get; set; }
}
}
using AccessorizeForLess.Data;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web;
namespace AccessorizeForLess.ViewModels
{
public class AddNewProductViewModel
{
public string Name { get; set; }
[DataType(DataType.MultilineText)]
public string Description { get; set; }
public decimal Price { get; set; }
public string AltText { get; set; }
public int Quantity { get; set; }
public string ImagePath { get; set; }
public HttpPostedFileBase Image { get; set; }
public ProductCategory Category { get; set; }
public int SelectedCategoryId { get; set; }
public List<ProductCategory> Categories { get; set; }
}
}
鉴于我的代码,有人能告诉我我做错了什么吗?我从昨晚就一直在努力解决这个问题。使用您的控制器构建列表,然后从视图中调用列表 控制器:
public static List<SelectListItem> GetDropDown()
{
List<SelectListItem> ls = new List<SelectListItem>();
lm = (call database);
foreach (var temp in lm)
{
ls.Add(new SelectListItem() { Text = temp.name, Value = temp.id });
}
return ls;
}
@Html.DropDownListFor(x => x.Field, PathToController.GetDropDown())
首先,我会将您的ViewModel更改为包含一个
SelectedCategoryId
,并将您的选项更改为Categories
没有看到您的get
的代码,我认为ProductCategory
如下所示:
public class ProductCategory {
public int Id {get;set;}
public string Name { get;set;}
}
你的剃须刀标记会变成:
<div class="form-group">
@Html.LabelFor(model => model.Categories, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.SelectedCategoryId,
new SelectList(Model.Categories, "Id", "Name"), "- Please Select -")
@Html.ValidationMessageFor(model => model.Categories)
</div>
</div>
@LabelFor(model=>model.Categories,新的{@class=“controllabel col-md-2”})
@Html.DropDownListFor(model=>model.SelectedCategoryId,
新选择列表(Model.Categories,“Id”,“Name”),“-请选择-”)
@Html.ValidationMessageFor(model=>model.Categories)
第一个参数是所选类别,您的选项将从类别中填充
我解决了这个问题(感谢大家的提示)。这是为任何人谁可能有问题像我一样
我将我的创建方法更改为:
// GET: /Products/Create
public ActionResult Create()
{
var p = new AddNewProductViewModel();
p.Categories = entities.ProductCategories.ToList();
return View(p);
}
我的AddNewProductViewModel看起来是这样的:
using AccessorizeForLess.Data;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web;
namespace AccessorizeForLess.ViewModels
{
public class AddNewProductViewModel
{
public string Name { get; set; }
[DataType(DataType.MultilineText)]
public string Description { get; set; }
public decimal Price { get; set; }
public string AltText { get; set; }
public int Quantity { get; set; }
public HttpPostedFileBase Image { get; set; }
public int SelectedCategoryId {get;set;}
public List<ProductCategory> Categories { get; set; }
public ProductCategory Category { get; set; }
}
}
使用AccessorizeForLess.Data;
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
使用System.Web;
命名空间AccessorizeForLess.ViewModels
{
公共类AddNewProductViewModel
{
公共字符串名称{get;set;}
[数据类型(DataType.multilitext)]
公共字符串说明{get;set;}
公共十进制价格{get;set;}
公共字符串AltText{get;set;}
公共整数数量{get;set;}
公共HttpPostedFileBase映像{get;set;}
public int SelectedCategoryId{get;set;}
公共列表类别{get;set;}
公共产品类别{get;set;}
}
}
我认为:
<div class="form-group">
@Html.LabelFor(model => model.Category, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.Category.Id, new SelectList(Model.Category,"Value","Text"), "- Please Select -")
@Html.ValidationMessageFor(model => model.Category)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.SelectedCategoryId, "Category",new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.SelectedCategoryId, new SelectList(Model.Categories, "CategoryId", "CategoryName"), "- Please Select -")
@Html.ValidationMessageFor(model => model.SelectedCategoryId)
</div>
</div>
@LabelFor(model=>model.SelectedCategoryId,“Category”,新的{@class=“controllabel col-md-2”})
@Html.DropDownListFor(model=>model.SelectedCategoryId,new SelectList(model.Categories,“CategoryId”,“CategoryName”),“-请选择-”)
@Html.ValidationMessageFor(model=>model.SelectedCategoryId)
谢谢大家的帮助:)您没有名为类别
的属性,这是一个复杂的对象,它具有属性ID
(属性类别
是一个列表
),您需要一个附加属性(例如)int CategoryID
来绑定下拉列表(并建议您命名您的集合(例如)CategoryList
因此很清楚您的属性是什么are@StephenMuecke我更新了我的代码,修改的越多,我就越困惑。你能根据我的代码(如果你需要更多的代码,请告诉我)告诉我如何做到这一点吗?到目前为止,我已经尝试了所有方法。最好的猜测是,它在你第一次渲染视图时起作用(假设Categories
已正确填充),并且在发布和返回视图时出现该异常。使用返回视图(“创建”);
不会返回模型,因此新建选择列表(Model.Categories…
引发异常,因为Model.Categories
为空。您需要首先重新填充集合并使用返回视图(Model);
我建议使用非静态帮助器方法,并在操作方法中使用它填充ViewModel。尝试限制Razor模板的调用次数,但从ViewModel读取的调用次数除外。
// GET: /Products/Create
public ActionResult Create()
{
var p = new AddNewProductViewModel();
p.Categories = entities.ProductCategories.ToList();
return View(p);
}
using AccessorizeForLess.Data;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web;
namespace AccessorizeForLess.ViewModels
{
public class AddNewProductViewModel
{
public string Name { get; set; }
[DataType(DataType.MultilineText)]
public string Description { get; set; }
public decimal Price { get; set; }
public string AltText { get; set; }
public int Quantity { get; set; }
public HttpPostedFileBase Image { get; set; }
public int SelectedCategoryId {get;set;}
public List<ProductCategory> Categories { get; set; }
public ProductCategory Category { get; set; }
}
}
<div class="form-group">
@Html.LabelFor(model => model.SelectedCategoryId, "Category",new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.SelectedCategoryId, new SelectList(Model.Categories, "CategoryId", "CategoryName"), "- Please Select -")
@Html.ValidationMessageFor(model => model.SelectedCategoryId)
</div>
</div>