I don't know how the AjaxForm
's will know when fire OnSuccess
or OnError
method. But, is is possible to make them fire OnSuccess or OnError method based on Boolean
value?
@using(Ajax.BeginForm("AddAttendeeManual", "Attendee", new AjaxOptions { HttpMethod = "POST", OnSuccess = "doneManualEmail" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary()
@Html.HiddenFor(m=>m.SelectedManualEmail.AppointmentId)
<div class="form-group">
@Html.LabelFor(m => m.SelectedManualEmail.Email, new { @class = "col-md-2 control-label" })
<div class="col-md-8 input-group">
@Html.TextBoxFor(m => m.SelectedManualEmail.Email, new {@class = "form-control",PlaceHolder="Email"})
<input type="submit" id="btnManual"class="btn btn-default" value="Add>>" />
</div>
</div>
}
and this is the OnSucess
method(on the same view)
function doneManualEmail() {
alert("Success");
$(@Html.IdFor(m=>m.SelectedManualEmail.Email)).val('');
var url = $("#invitedPeoples").data('url');
$.get(url, function (data) {
$('#invitedPeoples').html(data);
});
};
and this is the controller method
[HttpPost]
[ValidateAntiForgeryToken]
public void AddAttendeeManual(CreateAppointmentSelectPersons manualEmail)
{
_attendeeRepository.AddManualAttendee(manualEmail.SelectedManualEmail.AppointmentId,
manualEmail.SelectedManualEmail.Email);
}
currently when the form is submitted it calls controller's method (where person is added to database) does and after that call's the OnSuccess method mentioned above. No problem till now.
But now, I want to check something (if person exists) in controller, this is my controller's method now
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AddAttendeeManual(CreateAppointmentSelectPersons manualEmail)
{
bool result = _attendeeRepository.CheckIfAttendeeExists(manualEmail.SelectedManualEmail.AppointmentId, manualEmail.SelectedManualEmail.Email);
if(!result)
{
_attendeeRepository.AddManualAttendee(manualEmail.SelectedManualEmail.AppointmentId,
manualEmail.SelectedManualEmail.Email);
//call OnSuccess method
}
else
{
//add ModelStateError on client side?? or make it fire OnError method?
}
}
PS: There is not get method for this view. Based upon bool value I want the form to fire OnSuccess or OnError method, and if it is OnError then add a error(like a modelstate) on the clientside.
Would that be possible?