I have the following HTML to show some messages
...
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<span id="fail_message" class="label label-danger"></span>
<span id="success_message" class="label label-success"></span>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="button" value="Invite User" class="btn btn-primary"/>
</div>
</div>
...
The script that fires when the button is pressed is
<script>
$(function () {
$("input[type=button]").click(function () {
var data_email = $('#email').val();
var data_product = $('#product option:selected').text();
$.ajax({
url: 'Tools/SendInvite',
type: 'POST',
data: { email: data_email, product: data_product },
success: function (result_success, result_failure) {
$('#fail_message').val(result_failure);
$('#success_message').val(result_success);
}
});
});
});
</script>
Then in my controller I have
[AllowAnonymous]
public async Task<ActionResult> SendInvite(
string email, string product)
{
// Check if admin.
ApplicationUser user = null;
if (ModelState.IsValid)
{
user = await UserManager.FindByIdAsync(User.Identity.GetUserId());
if (user.IsAdmin != null && (bool)user.IsAdmin)
{
string key = String.Empty;
string subMsg = String.Empty;
var accessDB = new AccessHashesDbContext();
switch (product) { /* do stuff */ }
// Send email.
try
{
await Helpers.SendEmailAsync(new List<string>() { email }, null, "my msg string" );
result = String.Format("Invitation successfully sent to \"{0}\"", email);
return Json(new {
result_success = result,
result_failure = String.Empty
},
JsonRequestBehavior.AllowGet);
}
catch (Exception)
{
result = String.Format("Invitation to \"{0}\" failed", email);
return Json(new {
result_success = String.Empty,
result_failure = result
},
JsonRequestBehavior.AllowGet);
}
}
}
return Json(new {
result_success = String.Empty,
result_failure ="Invite Failure"
},
JsonRequestBehavior.AllowGet);
}
The controller method fires, the email is sent and the view gets returned to, but my messages are not shown. How can I get the lables to display my correct text?
Thank for your time.
Note. I have seen
but these do not help me.