Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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 5 DropDownList用于数据库Linq;_Asp.net Mvc_C# 4.0_Razor_Selectlist - Fatal编程技术网

Asp.net mvc MVC 5 DropDownList用于数据库Linq;

Asp.net mvc MVC 5 DropDownList用于数据库Linq;,asp.net-mvc,c#-4.0,razor,selectlist,Asp.net Mvc,C# 4.0,Razor,Selectlist,有谁能告诉我如何为C编写控制器公共操作结果下拉列表的滴状列表生成Linq我想转换此选择不同的名称从位置;为我的下拉列表动态生成Linq Chtml页面如何在@Html.DropDownListFor中书写 Models.Location这段代码将从IQueryable GetAll方法生成一个选择列表,或者您可以在实体上直接使用from c in_context.Set来使用它 其中Id是选择列表选项的值字段,Name是文本字段 这将被指定给模型特性: var model = new MyMod

有谁能告诉我如何为C编写控制器公共操作结果下拉列表的滴状列表生成Linq我想转换此选择不同的名称从位置;为我的下拉列表动态生成Linq

Chtml页面如何在@Html.DropDownListFor中书写
Models.Location

这段代码将从IQueryable GetAll方法生成一个选择列表,或者您可以在实体上直接使用from c in_context.Set来使用它

其中Id是选择列表选项的值字段,Name是文本字段

这将被指定给模型特性:

var model = new MyModel
{
    LocationList = GetAsSelectList();
}
您可以将模型传递到视图,并使用DropDownList:

@Html.DropDownListFor(x => x.MyModel.Location, Model.LocationList)

您的模型还将有一个Location属性,如果您想这样做,您可以将该属性设置为显示默认值。

假设您的模型名为MyModel

控制器

public ActionResult Edit()
{
  var couriers = // get your couriers from the database using your query
  // Is better to assign this to a property in your view model, but ViewBag will do for now
  ViewBag.CourierList = new SelectList(couriers);
  var model = new YourModel();
}
看法


据我所知,你可以这样做:

public ActionResult DropList()
{
    List<SelectListItem> objResult = new List<SelectListItem>();
    var result = dbContext.Locations.Select(x=>x.CouName).Distinct().ToList();
    foreach(var item in result)
    {
      SelectListItem temp = new SelectListItem();
      temp.Text = item;
      temp.Value = item;
      objResult.Add(temp);
    }

    ViewBag.DropdownResult = objResult;

    return View();

}
视图中的下拉列表:

@Html.DropDownListFor(m=>m.ModelLocations, ViewBag.DropdownResult  as List<SelectListItem>)

请根据需要修改代码。

显示要绑定到Location public int LocationId{get;set;}public int OrderId{get;set;}public string CourierName{get;set;}的模型。是否要绑定到LocationId?ConName是否为int?int LocationId字符串CourierName,具有不同的键,例如从位置选择不同的CourierName;你说的没有道理。您的意思是要将所选值绑定到属性CourierName吗?您只能将下拉列表绑定到一个属性如何使用DISTINCT键?例如,从位置选择不同的名称;返回新的SelectListlocs.Distinct、Id、Name;每个人都必须再次使用一个奇怪的viewbagviewbag吗
using System.Web.Mvc;
...
public static List<SelectListItem> GetItemsForDisplay(string listName)
{
    //your data access function should return a list of objects
    return DAL.Table.SelectByName(listName)
        .Select(x=> new SelectListItem{Text=x.DisplayName, Value=x.ID})
        .ToList<SelectListItem>();
}
@Html.DropDownListFor(m=>m.ModelLocations, ViewBag.DropdownResult  as List<SelectListItem>)
using System.Web.Mvc;
...
public static List<SelectListItem> GetItemsForDisplay(string listName)
{
    //your data access function should return a list of objects
    return DAL.Table.SelectByName(listName)
        .Select(x=> new SelectListItem{Text=x.DisplayName, Value=x.ID})
        .ToList<SelectListItem>();
}