User1124429227 posted
I am trying to make a create page have dropdrown lists boxes that are editable if a value is not present in the list.
So what i did was to use javascript to toggle the from @Html.DropDownListFor to @Html.EditorFor
This works great, the problem now starts that if for example in the asset model, staffname is [Required] in the staff model, so then the ModelState.IsValid = false because the i never filled in the @Html.EditorFor for the StaffName I only used the dropdownlist.
If set the StaffName field to allow nulls, everytime i create a new asset i also add a new entry in the Staff model with the StaffName as null.
I have removed the BIND out of the ActionResult and the same issue occurs .
Controller:
```````````
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ClinicalAssetID,AssetTypeID,ProductID,ProductName,ManufacturerID,ManufacturerName,ModelID,ModelName,SupplierID,SupplierName,SerialNo,PurchaseDate,PoNo,Costing,TeamID,StaffID,StaffName,WarrantyEndDate,InspectionDate,InspectionOutcome,InspectionDocumnets,InspectionDueDate,
BudgetCodeID, Filter")] ClinicalAsset clinicalAssets)
{
try
{
if (ModelState.IsValid)
{
db.ClinicalAssets.Add(clinicalAssets);
db.SaveChanges();
return RedirectToAction("Index");
}
}
catch (DataException /* dex */)
{
//Log the error (uncomment dex variable name and add a line here to write a log.
ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
}
PopulateAssetTypeDropDownList(clinicalAssets.AssetTypeID);
PopulateStaffDropDownList(clinicalAssets.StaffID);
PopulateModelDropDownList(clinicalAssets.ModelID);
PopulateProductDropDownList(clinicalAssets.ModelID);
PopulateManufacturerDropDownList(clinicalAssets.ManufacturerID);
PopulateSupplierDropDownList(clinicalAssets.SupplierID);
var Budgets = (from m in db.BudgetCodes
select new SelectListItem
{
Text = m.Code + " | " + m.BudgetCodeName,
Value = m.BudgetCodeID.ToString()
});
ViewBag.BudgetsList = new SelectList(Budgets, "Value", "Text");
ViewBag.TeamID = new SelectList(db.Teams, "TeamID", "TeamName", clinicalAssets.TeamID);
return View(clinicalAssets);
}
````````````
View:
````
<div class="form-group">
<label id="lbltipAddStaff">Select Staff Member:</label> <label id="lbltipAddStaff2" style="display:none;">Enter Staff Member:</label> <label style="float: right;"><i class="myClass fa fa-edit" id="staff"></i></label>
<div id="Staff">
@Html.DropDownListFor(model => model.StaffID, (SelectList)ViewBag.StaffDropDown, "Please Select From The List", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.StaffID, "", new { @class = "text-danger" })
</div>
<div class="js-staff" style="display:none;">
@Html.EditorFor(model => model.StaffName.StaffName, new { htmlAttributes = new { @class = "form-control", @placeholder = "Enter Name Here" } })
@Html.ValidationMessageFor(model => model.StaffName.StaffName, "", new { @class = "text-danger" })
</div>
</div>
````````````
Ideally I want to be able to stay on the same page without popup boxes and just to update the list if an entry is missing. But is is possible to use conditional saving only if the textbox is not null?