Handle multiple submit buttons with “Remote” valid

2019-07-07 04:10发布

I have two buttons in my Event form as below,

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

In My Controller I have following action while click on "Save",

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

and while click on "Save & Next", following action will be called,

 [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);
        }

I have Remote validation on my model as below,

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

If i will comment Remote validation line, then everything works perfect but if i will remain validation, then following Create() action is called(which must be called when i need to open form),

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

But i need to call SaveClick ana SavenNextClick, Please any help for remote validation with multiple submit button.

2条回答
干净又极端
2楼-- · 2019-07-07 04:59

you can add an actionname for your HTML buttons.

<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" />

and have one method on controller side:

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

}

I mean that actionname parameter is going to be added to your querystring and bring the proper value based on clicked button.

查看更多
仙女界的扛把子
3楼-- · 2019-07-07 05:00
<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" />

and controller side still remain the same as in previous answer. actionname parameter is going to take the values from the button: so in this case Save or Save & Next

Sorry my mistake on previous answer

查看更多
登录 后发表回答