When a user selects an Asset Type, i want my "Create" form to display different fields depending on the Asset Type selected.
For example: if a user selects Asset Type "Laptop", i want the form to display all the relevant fields for a Laptop Asset. However, if a user selects Asset Type "Printer", i want the form to display relevant fields for a Printer Asset.
Here is my Asset Model:
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; }
Obviously, not all these attributes apply to a Printer Asset.
Here is my Create method in my Asset Controller:
// 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");
retu 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();
retu 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);
retu View(asset);
}
How would i go about displaying fields in my Create View depending on the AssetTypeID a user selects?
