当用户选择资产类型时,我希望我的“创建”表单根据所选的资产类型显示不同的字段。
例如:如果用户选择资产类型“笔记本电脑”,我希望表单显示笔记本电脑资产的所有相关字段。但是,如果用户选择资产类型“打印机”,我希望表单显示打印机资产的相关字段。
这是我的资产模型:
public class Asset
{
[Key]
public int AssetID { get; set; }
public int AssetMakeID { get; set; }
public int AssetTypeID { get; set; }
public string User { get; set; }
public int DepartmentID { get; set; }
public string AssetModel { get; set; }
public string AssetSerialNo { get; set; }
public string PurchaseOrderNo { get; set; }
public string Supplier { get; set; }
public string WarrantyInfo { get; set; }
public DateTime DatePurchased { get; set; }
public string ComputerName { get; set; }
public int CPUID { get; set; }
public string RAM { get; set; }
public string HardDrive { get; set; }
public int OperatingSystemID { get; set; }
public string OperatingSystemProductKey { get; set; }
显然,并非所有这些属性都适用于打印机资产。
这是我的资产管理器中的Create方法:
// GET: Asset/Create
public ActionResult Create()
{
ViewBag.AssetMakeID = new SelectList(db.AssetMake, "AssetMakeID", "Name");
ViewBag.AssetTypeID = new SelectList(db.AssetType, "AssetTypeID", "Type");
ViewBag.CPUID = new SelectList(db.CPU, "CPUID", "Name");
ViewBag.DepartmentID = new SelectList(db.Department, "DepartmentID", "DepartmentName");
ViewBag.OperatingSystemID = new SelectList(db.OperatingSyst, "OperatingSystemID", "Name");
return View();
}
// POST: Asset/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "AssetID,AssetMakeID,AssetTypeID,User,DepartmentID,AssetModel,AssetSerialNo,PurchaseOrderNo,Supplier,WarrantyInfo,DatePurchased,ComputerName,CPUID,RAM,HardDrive,OperatingSystemID,OperatingSystemProductKey")] Asset asset)
{
if (ModelState.IsValid)
{
db.Asset.Add(asset);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.AssetMakeID = new SelectList(db.AssetMake, "AssetMakeID", "Name", asset.AssetMakeID);
ViewBag.AssetTypeID = new SelectList(db.AssetType, "AssetTypeID", "Type", asset.AssetTypeID);
ViewBag.CPUID = new SelectList(db.CPU, "CPUID", "Name", asset.CPUID);
ViewBag.DepartmentID = new SelectList(db.Department, "DepartmentID", "DepartmentName", asset.DepartmentID);
ViewBag.OperatingSystemID = new SelectList(db.OperatingSyst, "OperatingSystemID", "Name", asset.OperatingSystemID);
return View(asset);
}
我如何根据用户选择的AssetTypeID在我的创建视图中显示字段?