我正在尝试在ASP.NET中创建一个下拉框,当生成View时,它会给我一个错误,说明在模型分类中找不到Categorie_ID(它确实存在并且它是公共的和所有)。成功地加载了类别和产品(我调试了那部分)。
这是我得到的;
我的观点:
@{
ViewBag.Title = @Model.Product.Merk; @Model.Product.Type;;
Layout = "~/Views/Shared/_Layout.cshtml";
}
@model WebshopProjectBlokC.ViewModels.ProductViewModel
<h2>@Model.Product.Merk @Model.Product.Type wijzigen</h2>
@using (Html.BeginForm("ProductWijzigenForm", "ProductBeheer"))
{
@Html.ValidationSummary(true)
<fieldset>
@Html.HiddenFor(model => model.Product.ID)
@Html.HiddenFor(model => model.Product.Naam)
<div class="editor-label">Prijs</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Product.Prijs)
@Html.ValidationMessageFor(model => model.Product.Prijs)</div>
<div class="editor-label">Maat</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Product.Maat)
@Html.ValidationMessageFor(model => model.Product.Maat)</div>
<div class="editor-label">Merk</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Product.Merk)
@Html.ValidationMessageFor(model => model.Product.Merk)</div>
<div class="editor-label">Type</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Product.Type)
@Html.ValidationMessageFor(model => model.Product.Type)</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.SelectedID, Model.Categorien)
</div>
<div class="editor-label">URL Foto</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Product.Afbeelding)
@Html.ValidationMessageFor(model => model.Product.Afbeelding)</div>
<input type="submit" value="Wijzig" />
</fieldset>
}
控制器:
public ActionResult ProductWijzig(int productid)
{
ProductViewModel viewModel = new ProductViewModel();
Product product = ProductdataController.GetProduct(productid);
List<Categorie> catas = ProductdataController.HaalCategorienOp();
viewModel.Categorien = new SelectList(catas, "Categorie_id", "Naam");
viewModel.Product = product;
return View(viewModel);
}
viewmodel:
namespace WebshopProjectBlokC.ViewModels
{
public class ProductViewModel
{
public Product Product { get; set; }
public Categorie Categorie { get; set; }
public SelectList Categorien { get; set; }
public int SelectedID { get; set; }
}
}
HaalCategorienOp方法;
public List<Categorie> HaalCategorienOp()
{
List<Categorie> categorien = new List<Categorie>();
try
{
conn.Open();
string selecteer = "select * from categorie";
MySqlCommand command = new MySqlCommand(selecteer, conn);
MySqlDataReader dataReader = command.ExecuteReader();
while (dataReader.Read())
{
Categorie categorie = HaalCategorieUitReader(dataReader);
categorien.Add(categorie);
}
}
catch (Exception e)
{
Console.Write("Ophalen van categorien mislukt " + e);
throw e;
}
finally
{
conn.Close();
}
return categorien;
}
HaalCategorieUitReader方法;
protected Categorie HaalCategorieUitReader(MySqlDataReader datareader)
{
if (!datareader.IsDBNull(0))
{
categorie_id = datareader.GetInt32("categorie_id");
}
if (!datareader.IsDBNull(1))
{
categorienaam = datareader.GetString("naam");
}
if (!datareader.IsDBNull(2))
{
hoofdcategorie = datareader.GetInt32("hoofdcategorie");
}
Categorie categorie = new Categorie{ Categorie_id = categorie_id, Hoofdcategorie = hoofdcategorie, Naam = categorienaam};
return categorie;
}