locked
Conditionally saving records where the Textbox is not blank RRS feed

  • Question

  • 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?

    Wednesday, July 10, 2019 3:07 PM

Answers

  • User1520731567 posted

    Hi microchef,

    microchef

    1.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.

    2.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.

    3.I have removed the BIND out of the ActionResult and the same issue occurs .

    According to your above descriptions,these are very reasonable under your design.

    For 1,The way I think of is to a hidden field named staffname,and assign it a value in default or by jquery based on your design.

    (in this way,you could add [Required] and not need to delete BIND)

    like:

    <input type="hidden" name="staffname" value="888">
    

    If you still have problems,please post more details,such as: model,view and your business logic...I can't quite understand your last line.

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 11, 2019 6:15 AM