1

我在下面的表格和控制器。基本上我试图在控制器中获取表单值模式,cmd值总是设置但我的模式没有设置,我不知道如何获取值,谁能告诉我如何获取它?或者如何使用与 cmd 字符串相同的方法来获取它?

谢谢

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>
}

我添加的控制器是这个

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

1

错误在这里:

@Html.Hidden("hidMode", Request.QueryString["mode"].ToString())

hidMode为隐藏字段命名并期望它cmd在您的控制器中。因此,在控制器操作中重命名隐藏字段或重命名输入参数

于 2013-07-23T08:55:46.160 回答