我只是一个新手,我的DropDownList有一个问题:(MVC,ASP.NET)
场景:
在“视图”中,选择下拉菜单后,它将显示值(类别名称) 在Controller中,它不会识别(类别名称),而只会识别分配给特定值的ID
问题:
单击保存按钮时,下拉列表的返回值为NULL 然后在CATEGORY表中创建包含EMPTY内容的另一行 型号:(供应商)
$ pip3 install --upgrade pip
Successfully uninstalled pip-10.0.1
Successfully installed pip-18.0
型号:(类别)
public class Supplier
{
public int Id { get; set; }
public string SupplierCode { get; set; }
public string SupplierName { get; set; }
public int SupplierContact { get; set; }
public Category Category { get; set; }
}
控制器(供应商)
public class Category
{
public int Id { get; set; }
public string CatName { get; set; }
}
查看:(供应商)
public ActionResult New()
{
var CategoryMenu = _SBC.Categorys.ToList();
var NewContent = new SupplierandCategoryViewModel()
{
CategoryModel = CategoryMenu,
};
return View(NewContent);
}
public ActionResult Save(SupplierandCategoryViewModel Supply)
{
var DSupply = new Supplier()
{
SupplierName = Supply.SupplierModel.SupplierName,
SupplierCode = Supply.SupplierModel.SupplierCode,
SupplierContact = Supply.SupplierModel.SupplierContact,
Category = Supply.CategoryModel //this part is error; it cannot
recognize
};
_SBC.Suppliers.Add(DSupply);
_SBC.SaveChanges();
return View();
}
ViewModel:(SupplierandCategoryViewModel)
@model ShoeInformation.ViewModel.SupplierandCategoryViewModel
@{
ViewBag.Title = "New";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<br />
<br />
<h2>Create New Customer</h2>
<br />
<br />
@using (Html.BeginForm("Save", "Supplier"))
{
<div class="form-group">
@Html.LabelFor(x=>x.CategoryModel)
@Html.DropDownListFor(x => x.CategoryModel, new SelectList(Model.CategoryModel,"Id","CatName"), "Select Supplier Category", new {id="myCat",@class="form-control" })
</div>
<div class="form-group">
@Html.LabelFor(x=>x.SupplierModel.SupplierCode)
@Html.TextBoxFor(x => x.SupplierModel.SupplierCode, new { @class="form-control"})
</div>
<div class="form-group">
@Html.LabelFor(x=>x.SupplierModel.SupplierName)
@Html.TextBoxFor(x => x.SupplierModel.SupplierName, new { @class="form-control"})
</div>
<div class="form-group">
@Html.LabelFor(x=>x.SupplierModel.SupplierContact)
@Html.TextBoxFor(x => x.SupplierModel.SupplierContact, new { @class="form-control"})
</div>
}
我想保存类别的ID,但是在view(Index)中它必须显示ID的值而不是ID本身
感谢您的回复!
答案 0 :(得分:0)
1)向您的SelectedCategory
添加一个SupplierandCategoryViewModel
属性
public class SupplierandCategoryViewModel
{
public IEnumerable<Category> CategoryModel { get; set; }
public Supplier SupplierModel { get; set; }
public int SelectedCategory { get; set; } <== Add this property
}
2)为Id
public class Category
{
public int CategoryId { get; set; } <== Change the name
public string CatName { get; set; }
}
3)将此CategoryId
引用到您的Supplier
模型中
public class Supplier
{
public int Id { get; set; }
public string SupplierCode { get; set; }
public string SupplierName { get; set; }
public int SupplierContact { get; set; }
public int CategoryId { get; set; } <== Reference to this property
public Category Category { get; set; }
}
4)像修改您的DropDownListFor
@Html.DropDownListFor(x => x.SelectedCategory, new SelectList(Model.CategoryModel, "CategoryId", "CatName"), "Select Supplier Category", new { id = "myCat", @class = "form-control" })
5)最后,您的Save
操作方法就像
public ActionResult Save(SupplierandCategoryViewModel Supply)
{
var DSupply = new Supplier()
{
SupplierName = Supply.SupplierModel.SupplierName,
SupplierCode = Supply.SupplierModel.SupplierCode,
SupplierContact = Supply.SupplierModel.SupplierContact,
CategoryId = Supply.SelectedCategory <==Note here I assigned SelectedCategory to model's CategoryId
};
_SBC.Suppliers.Add(DSupply);
_SBC.SaveChanges();
return View();
}
DropDownList的测试数据:
var CategoryMenu = new List<Category> { new Category { CategoryId = 1, CatName = "Abc" }, new Category { CategoryId = 2, CatName = "Pqr" } };
输出: