Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将SelectList.DataValueField设置为列表的整个对象?_C#_Asp.net Mvc - Fatal编程技术网

C# 如何将SelectList.DataValueField设置为列表的整个对象?

C# 如何将SelectList.DataValueField设置为列表的整个对象?,c#,asp.net-mvc,C#,Asp.net Mvc,我用Razor创建了2个Html.DropDownList <p>Category:</p> @Html.DropDownList("Category", new SelectList(catList, "Id", "category"), "Select category"); <p>SubCategory:</p> @Html.DropDownListFor(x=>x.SubCatego

我用Razor创建了2个Html.DropDownList

 <p>Category:</p>
        @Html.DropDownList("Category", new SelectList(catList, "Id", "category"), "Select category");
        <p>SubCategory:</p> 
        @Html.DropDownListFor(x=>x.SubCategory, new SelectList(string.Empty, "Id", "Subcategory"), "Select sub category",new {id="subCategory" });
        @Html.ValidationMessageFor(x => x.SubCategory);
然后我创建了JSon结果方法

  public JsonResult GetSubs(int category)
    {
        List<SubCategory> subs = goods.SubCategorySet.Where(x=>x.Category.Id==category).ToList();
        return Json(new SelectList(subs, "Id", "Subcategory")); //here is row of code that i want to change.
    }
publicjsonresult GetSubs(int类)
{
List subs=goods.SubCategorySet.Where(x=>x.Category.Id==Category.ToList();
返回Json(新的SelectList(subs,“Id”,“Subcategory”);//这是我要更改的代码行。
}

如何将
SelectList.DataValueField
设置为
SubCategory
,而不是
Id

我确信您无法在下拉列表中加载SubCategory类。但您可以做的是从下拉列表中保存ID,并使用该ID从数据库中检索适当的项,然后将其添加到实体中

您可以在操作的参数列表中创建一个与下拉列表具有相同名称和类型的参数,以检索该值

@Html.DropDownList("SubCategoryID", ViewBag.Subs, "Select an Option", new { @class = "pretty" })

[HttpPost]
 public ActionResult Index(Product model, int SubCategoryID)
{

  //here you can retrieve your item from the database using the value in
  //SubCategoryID
  //Psuedo code below:

  Subcategory test = goods.SubCategorySet.Where(sc => sc.id == SubCategoryID);

  model.SubCategory = test;

  //save/update model here
}
创建新产品时,回发时,子类别将为空,但此时使用ID分配选定的子类别

如果您在实体的编辑屏幕上,还将使用id显示当前选定的子类别。只需获取id并在选择列表的selected value属性中使用它

new SelectList(subs, "Id", "Subcategory", SubCategoryID));

你的问题不清楚。将selectlist的值字段设置为子类别是什么意思?选择列表已经由
subs
生成,这是一个类型为SubCategory的列表?您将文本字段设置为什么?选择列表中的数据值字段应与文本字段的值相对应。所以,如果您的数据字段是SubCategory,那么它对应的文本值是什么?我想粘贴类似于这个新选择列表的内容(subs,“SubCategory”,“SubCategory”)。因为使用dropdownlist中的所有选项,在我提交表单时只向控制器发送Id,而不是控制器的子类别对象。这就是为什么在控制器中,我在Product.SubCategory中得到null而不是value。您的第二个dropdownlist没有意义。无法将
绑定到复杂对象(在您的示例中是
子类别
)-您需要将其绑定到简单属性,例如
@Html.DropDownListFor(x=>x.SubCategory.ID,new SelectList)(…
并且您保留了
SelectList
构造函数的原样。建议您也参考如何实现级联DropDownListHank’s作为答案,我是asp.net mvc中的新手,我认为可以使用复杂对象作为SelectListItem的值。是的,但据我所知,值可以是类的任何属性或字段,但不能是全部e类。我需要在subs集合中为整个类创建它。哦,对不起,我误解了你的意思。我不建议在下拉列表中加载整个类实例,因为加载的HTML量是额外的。我会用适当的解决方案编辑答案。好的,我知道了,最好的方法是在id中存储值,然后n按id在控制器集合中找到某个子类别,对吗?是的,没错。我知道这不能完全回答您的问题,但如果它解决了您的问题,请将其标记为正确。或者,如果您还需要其他信息,请告诉我。
new SelectList(subs, "Id", "Subcategory", SubCategoryID));