使用MVC4剃须刀中的“远程”验证处理多个提交按钮

时间:2014-01-25 13:24:16

标签: jquery asp.net-mvc validation asp.net-mvc-4 razor

我的活动表单中有两个按钮,如下所示,

@using (Ajax.BeginForm("Create", "Events", new AjaxOptions
        {
            InsertionMode = InsertionMode.Replace,
            HttpMethod = "POST",
            OnSuccess = "updateSuccess"
        }, new { @id = "updateEventForm" }))
        {

            <div class="modal-body form-horizontal">
                <div class="form-group">
                    <label class="col-sm-2 control-label">@Html.LabelFor(model => model.Code)</label>
                    <div class="col-sm-10">
                        @if (Model.EventID <= 0)
                        {
                            @Html.TextBoxFor(model => model.Code, new { id = "txtCode", Class = "form-control", placeholder = "Code", Value = "E_" + new Random().Next().ToString() })
                        }
                        else
                        {
                            @Html.TextBoxFor(model => model.Code, new { id = "txtCode", Class = "form-control", placeholder = "Code" })
                        }
                        @Html.ValidationMessageFor(model => model.Code)
                    </div>
                </div>              
                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <input type="submit" value="Save" class="btn btn-primary" name="action:Save" id="btnSave" />
                        <input type="submit" value="Save & Next" class="btn btn-primary" name="action:SavenNext" id="btnSaveNext" />
                    </div>
                </div>
            </div>
            <!-- // Modal body END -->
        }

在我的控制器中,点击“保存”,

,我有以下操作
[HttpPost]
        [MultipleButton(Name = "action", Argument = "Save")]
        public ActionResult SaveClick(Events objEvents)
        {
            int Result = CreateEvent(objEvents);
            if (Result == 0)
                return null;
            else
                return RedirectToAction("Index");
        }

点击“保存&下一步”,将会调用以下操作,

 [HttpPost]
        [MultipleButton(Name = "action", Argument = "SavenNext")]
        public ActionResult SavenNextClick(Events objEvents)
        {
            int Result = CreateEvent(objEvents);
            if (Result == 0)
                return null;
            else
                return Json(JsonResponseFactory.SuccessResponse(objEvents.SubscriptionID), JsonRequestBehavior.AllowGet);
        }

我的模型上有远程验证,如下所示,

 [Remote("codeExist", "Events", AdditionalFields = "EventID", HttpMethod = "POST", ErrorMessage = "Code must be unique!")]
        public string Code { get; set; }

如果我将评论远程验证行,那么一切都很完美,但如果我将继续验证,则调用后面的Create()动作(当我需要打开表单时必须调用它),

 public ActionResult Create(int SubscriptionID)
        {
            FillEventTypeDropDown();
            objEvents.SubscriptionID = SubscriptionID;
            return PartialView(objEvents);
        }

但是我需要调用SaveClick和SavenNextClick,请使用多个提交按钮进行远程验证。

2 个答案:

答案 0 :(得分:0)

您可以为HTML按钮添加操作名称。

<input type="submit" value="Save" class="btn btn-primary" actionname="save" id="btnSave" />
<input type="submit" value="Save & Next" class="btn btn-primary" actionname="savenext" id="btnSaveNext" />

在控制器端有一个方法:

[HttpPost]
public ActionResult Create(..., string actionname = "") {
    if(actionname == "save") then //do something
    else //do someting else

}

我的意思是将actionname参数添加到您的查询字符串中,并根据点击的按钮显示正确的值。

答案 1 :(得分:0)

<input type="submit" value="Save" class="btn btn-primary" name="actionname" id="btnSave" />
<input type="submit" value="Save & Next" class="btn btn-primary" name="actionname" id="btnSaveNext" />

和控制器端仍然与之前的答案保持一致。 actionname参数将从按钮中获取值:所以在这种情况下保存或保存&amp;下一步

对不起我之前回答的错误