1

my form and controller below. basically I'm trying to obtain the form value mode in the controller, the cmd value is always set but my mode isn't set and i don't know how to obtain the value, can anyone tell me how to get it? or how to get it using the same method as the cmd string?

Thanks

using (Ajax.BeginForm("AddEditRecord", "Equipment", new AjaxOptions { HttpMethod = "POST",OnSuccess = "onSuccess()", LoadingElementId = "dvLoading" }))
{    
    @Html.ValidationSummary(true)
<div id="equipmentDialog">    
    @Html.Hidden("hidMode", Request.QueryString["mode"].ToString())
    <fieldset>
        <legend>Product</legend>
        @if (ViewBag.IsUpdate == true)
        {
             @Html.HiddenFor(model => model.ID)
        }
        <div class="editor-label">
            @Html.LabelFor(model => model.MachineName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.MachineName)
            @Html.ValidationMessageFor(model => model.MachineName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.AssetNo)
        </div>
        <div class="editor-field">
            @Html.TextAreaFor(model => model.AssetNo)
            @Html.ValidationMessageFor(model => model.AssetNo)
        </div>
        <p>
            @if (ViewBag.IsUpdate == true)
            {
                <input type="submit" value="Update" id="btnUpdate" name="cmd" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" />
            }
            else
            {
                <input type="submit" value="Add" id="btnSave" name="cmd" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" />
            }
            <input type="button" value="Cancel" id="btncancel" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" />
        </p>
    </fieldset>
</div>
}

my addedit controller is this

public ActionResult AddEditRecord(tblEquipment Equipment, string cmd, string mode)
        {
            if (ModelState.IsValid)
            {
                switch (cmd)
                {
                    case "Add":
                        try
                        {
                            db.tblEquipments.Add(Equipment);
                            db.SaveChanges();
                            return RedirectToAction(mode);
                        }
                        catch { }
                        break;
                    case "Update":
                        try
                        {
                            tblEquipment Item = db.tblEquipments.Where(m => m.ID == Equipment.ID).FirstOrDefault();
                            if (Item != null)
                            {
                                Item.AssetNo = Equipment.AssetNo;
                                Item.MachineName = Equipment.MachineName;
                                db.SaveChanges();
                            }
                            return RedirectToAction(mode);
                        }
                        catch { }
                        break;
                }
            }

            if (Request.IsAjaxRequest())
            {
                return PartialView("_AddEdit", Equipment);
            }
            else
            {
                return View("AddEdit", Equipment);
            }
        }
4

1 に答える 1