Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.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
Asp.net mvc 在MVC的SelectList中添加额外的数据文本字段_Asp.net Mvc_Linq To Sql_Asp.net Mvc 2_Linq To Entities_Selectlist - Fatal编程技术网

Asp.net mvc 在MVC的SelectList中添加额外的数据文本字段

Asp.net mvc 在MVC的SelectList中添加额外的数据文本字段,asp.net-mvc,linq-to-sql,asp.net-mvc-2,linq-to-entities,selectlist,Asp.net Mvc,Linq To Sql,Asp.net Mvc 2,Linq To Entities,Selectlist,我正在使用选择列表将文本传递给复选框,我想要的是将2个文本字段传递给复选框列表,但选择列表没有任何选项来提供2或3个数据文本字段,我尝试对其进行自定义,但无法使Intellisense正常工作: public ActionResult Create() { IProductRepository ProductResp = new ProductRepository(); IQueryable<Object> getAllProducts =

我正在使用选择列表将文本传递给复选框,我想要的是将2个文本字段传递给复选框列表,但选择列表没有任何选项来提供2或3个数据文本字段,我尝试对其进行自定义,但无法使Intellisense正常工作:

 public ActionResult Create()
    {
        IProductRepository ProductResp = new ProductRepository();
        IQueryable<Object> getAllProducts = ProductResp .GetProductsSelectlist();

        List<object> newList = new List<object>();
        foreach (var events in getAllProducts)
            newList.Add(new
            {
                Id = getAllProducts.Name, // I cant get .Name or DateAdded Intellisense here
                Name = getAllProducts.Name + " " + getAllProducts.DateAdded
            });

        ViewData["events"] = new SelectList(newList.ToList(), "Id","Name");
        return View();
    } 
public ActionResult Create()
{
IPProductRepository ProductResp=新产品存储库();
IQueryable getAllProducts=ProductResp.GetProductsSelectlist();
List newList=新列表();
foreach(getAllProducts中的var事件)
添加(新的)
{
Id=getAllProducts.Name,//我无法在此处获取.Name或DateAdded Intellisense
Name=getAllProducts.Name+“”+getAllProducts.DateAdd
});
ViewData[“events”]=新建SelectList(newList.ToList(),“Id”,“Name”);
返回视图();
} 
产品存储库

 public IQueryable<Object> GetProductssSelectlist()
    {

        ApexWorldEntities entity = new ApexWorldEntities();

        var query = from v in entity.Products
                    where v.Date > DateTime.Now 
                    select new { ProductID = v.ID, v.Name , v.Date};
        return query.OrderBy(v => v.Date);

    }
public IQueryable GetProductsSelectList()
{
ApexWorldEntities实体=新的ApexWorldEntities();
var query=来自entity.Products中的v
其中v.Date>DateTime.Now
选择新{ProductID=v.ID,v.Name,v.Date};
返回query.OrderBy(v=>v.Date);
}

首先,创建一个类来保存结果:

public class ApexWorldResult 
{
    public int ProductID { get; set; }
    public string Name { get; set; }
    public DateTime Date { get; set; }
}
然后,将目标集合强制转换为实际类型:

var query = from v in entity.Products
            where v.Date > DateTime.Now
            select new ApexWorldResult { ProductID = v.ID, v.Name, v.Date };

Create()

您需要定义的是使用“SelectListItem”而不是对象 这是用于选择列表项的生成。可以在其中指定文本、值和值。 及

模型定义:

class FooModel{
 public SelectListItem selectList{get;set;}
...
}
在您使用的控制器中:

public ActionResult YourAction(){

 FooModel model = new FooModel();
  //Define your collection of list items
  List<SelectListItem> listItems = new List<SelectListItem>();
            listItems.Add(new SelectListItem(){Selected = false, Text = "Text", Value = "MyValue"});
  //Assign the list to the collection
  model.selectList = new SelectList(listItems);
  //Pass to the view
  return View(model);
}
public ActionResult YourAction(){
FooModel model=新的FooModel();
//定义列表项的集合
List listItems=新列表();
添加(新建SelectListItem(){Selected=false,Text=“Text”,Value=“MyValue”});
//将列表分配给集合
model.selectList=新的selectList(列表项);
//传递到视图
返回视图(模型);
}

我的问题与自定义selectlist中的文本字段有关,我正在使用存储库界面您的意思是:selectlist没有任何选项来提供2或3个数据文本字段?我不知道你想要实现什么