Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.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# 在ASP.NET MVC中填充DropDownList_C#_Asp.net_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

C# 在ASP.NET MVC中填充DropDownList

C# 在ASP.NET MVC中填充DropDownList,c#,asp.net,asp.net-mvc,asp.net-mvc-4,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,所以基本上,我正在尝试建立一个网站,可以做一些像超市里的节目 数据库中有卖家工作的产品/视图、添加新产品的视图和收据模型。所以我有一个类 public class Product { public int ID { get; set; } public string Title { get; set; } public int Quantity { get; set; } public decimal Price { get; set; } } public c

所以基本上,我正在尝试建立一个网站,可以做一些像超市里的节目 数据库中有卖家工作的产品/视图、添加新产品的视图和收据模型。所以我有一个类

public class Product
{
    public int ID { get; set; }
    public string Title { get; set; }
    public int Quantity { get; set; }
    public decimal Price { get; set; }
}

public class ProduktiDBContext : DbContext
{
    public DbSet<Product> Products { get; set; }
}
在索引视图中,我想添加一个DropDownList,其中包含数据库中所有产品的标题

@model IEnumerable<Produkti.Models.Product>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Title)
        </th>

        <th>
            @Html.DisplayNameFor(model => model.Quantity)
        </th>

        <th>
            @Html.DisplayNameFor(model => model.Price)
        </th>

    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Title)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Quantity)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Price)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
            @Html.ActionLink("Details", "Details", new { id=item.ID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.ID })
        </td>
    </tr>
}


<hr />
    @foreach (var item in Model) {
    <tr>
        <td>
            //I'd like to put the dropdownlist here i dont know if this for loop is necessary
        </td>
       <td>

       </td>
    </tr>
}

</table>
@model IEnumerable
@{
ViewBag.Title=“Index”;
}
指数

@ActionLink(“新建”、“创建”)

@DisplayNameFor(model=>model.Title) @DisplayNameFor(model=>model.Quantity) @DisplayNameFor(model=>model.Price) @foreach(模型中的var项目){ @DisplayFor(modeleItem=>item.Title) @DisplayFor(modelItem=>item.Quantity) @DisplayFor(modelItem=>item.Price) @ActionLink(“编辑”,“编辑”,新的{id=item.id})| @ActionLink(“详细信息”,“详细信息”,新的{id=item.id})| @ActionLink(“删除”,“删除”,新的{id=item.id}) }
@foreach(模型中的var项目){ //我想把下拉列表放在这里,我不知道这个for循环是否必要 }

我想知道是否需要将包含所有标题的列表从控制器传递到视图或返回视图(db.Products.ToList())已经传递了我需要的数据,以及如何从这个DropDownList传递数据

将列表添加到您的模型中

public List<SelectListItem> Products { get; set; }
那么在你看来

@Html.DropDownListFor(x => x.SelectedProduct, model.Products)
您可以通过在get上设置model.SelectedProduct来设置下拉列表,该值将在post方法1上用所选值填充: 在您的行动中,请执行以下操作:

public ActionResult YourAction()
{

ViewBag.DDLProducts = new SelectList(db.Products,"ProductID","ProductName");

return View();

}
鉴于:

@Html.DropDownListFor(model => model.SelectedProduct,  (List<SelectListItem>)ViewBag.DDLProducts, "Select One")
@Html.DropDownListFor(model => model.SelectedProduct,new SelectList(db.Products,"ProductID","ProductName"), "Select One")
鉴于:

@Html.DropDownListFor(model => model.SelectedProduct,  (List<SelectListItem>)ViewBag.DDLProducts, "Select One")
@Html.DropDownListFor(model => model.SelectedProduct,new SelectList(db.Products,"ProductID","ProductName"), "Select One")

我建议创建一个包含
List
List
的新模型类,而不是使用
IEnumerable
作为模型。假设类名是
ProductModel
,下面是类定义的样子

public class ProductModel
{
    public ProductModel()
    {
        this.Products = new List<Product>();
        this.ProductsDropdownItems = new List<SelectListItem>();
    }

    public List<Product> Products { get; set; }
    public int SelectedProductID { get; set; }
    public List<SelectListItem> ProductsDropdownItems { get; set; }
}
然后更改视图代码

@model Produkti.Models.ProductModel
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            Title
        </th>

        <th>
            Quantity
        </th>

        <th>
            Price
        </th>

    </tr>

@foreach (var item in Model.Products) {
    <tr>
        <td>
            @item.Title
        </td>
        <td>
            @item.Quantity
        </td>
        <td>
            @item.Price
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
            @Html.ActionLink("Details", "Details", new { id=item.ID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.ID })
        </td>
    </tr>
}


<hr />
    <tr>
        <td>
            @Html.DropDownListFor(m => m.SelectedProductID, Model.ProductsDropdownItems)
        </td>
       <td>

       </td>
    </tr>
</table>
@model Produkti.Models.ProductModel
@{
ViewBag.Title=“Index”;
}
指数

@ActionLink(“新建”、“创建”)

标题 量 价格 @foreach(Model.Products中的var项){ @项目.标题 @项目.数量 @项目.价格 @ActionLink(“编辑”,“编辑”,新的{id=item.id})| @ActionLink(“详细信息”,“详细信息”,新的{id=item.id})| @ActionLink(“删除”,“删除”,新的{id=item.id}) }
@Html.DropDownListFor(m=>m.SelectedProductID,Model.ProductsDropdownItems)
考虑到这一点,只需写下陈述即可

@Html.DropDownList("Products");

当我在运行后将列表添加到模型中时,我会得到一个错误,没有定义键,而不是循环使用
new SelectList(db.Products,“ProductID”,“ProductName”)
@EhsanSajjad你能写完整的代码吗?我应该把它放在哪里?我不知道你是什么意思。model.SelectedProduct似乎没有参考添加SelectedProduct属性通过将你的模型复制为ViewModel当我遵循本指南时,我的视图中出现了很多错误“@model”接口成员声明错误也更改为此
model.ProductsDropdownItems.Add(新建SelectListItem(){Text=p.Title,Value=p.ID.ToString())
但是我现在在视图中遇到了一个错误
@Html.DisplayNameFor(model=>model.Title)
请查看上面的整个视图代码,我将
@Html.DisplayNameFor(model=>model.Title)
更改为
Title
数量
价格
也是一样。最后我做了谢谢我现在需要检查一下你做了什么非常感谢
@model Produkti.Models.ProductModel
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            Title
        </th>

        <th>
            Quantity
        </th>

        <th>
            Price
        </th>

    </tr>

@foreach (var item in Model.Products) {
    <tr>
        <td>
            @item.Title
        </td>
        <td>
            @item.Quantity
        </td>
        <td>
            @item.Price
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
            @Html.ActionLink("Details", "Details", new { id=item.ID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.ID })
        </td>
    </tr>
}


<hr />
    <tr>
        <td>
            @Html.DropDownListFor(m => m.SelectedProductID, Model.ProductsDropdownItems)
        </td>
       <td>

       </td>
    </tr>
</table>
public ActionResult Index()
 {
  ViewBag.Products = new SelectList(db.Products,"ProductID","ProductName");
  return View();
  }
@Html.DropDownList("Products");