I want to fire a Jquery function from my MVC Action code. Know that it isn't possible via the direct code, If anyone knows how to do this task via any kind of possible forms such as publishing an event or do that with using ajax call or whatever.
Here I have a JQuery Plugin which can be run with the below code.
$('#myModal').Show();
their original calling mechanism was :
<script type="text/javascript">
$(document).ready(function() {
$('#myButton').click(function(e) {
e.preventDefault();
$('#myModal').Show();
});
});
</script>
A little more explanation:
I don't want most of the code except the line I mentioned.
Maybe here we should make it a more simple function before running the provided hack.
We don't the button event-handler what we want is : after the Action in the Controller have sent an email call this function $('#myModal').Show();
The Action :
[HttpPost]
public ActionResult Contact(Contact message)
{
if (ModelState.IsValid)
{
string stResult ;
Emailing.SendEmail(message, out stResult);
ViewBag.SaveResultMessage = stResult;
}
return View(message);
}
Action is called from the Contact View :
@Html.ValidationSummary(true)
@using (Html.BeginForm("Contact", "Home", FormMethod.Post, new { name = "send-contact",
id = "contactform1"}))
{
if (!String.IsNullOrEmpty(stResult))
{
// $('#myModal').Show(); I hoped we could do this way.
}
...
}
Any sample codes and demo on your provided solution is really appreciated.
Edit (Based on the bAlexandre answer):
your code : $("#btnSubmit").click(function () { // let's show a loading image //$(".myModal").Loading(); // We don't want the load or waiting operations, // Just want to show the final message such as "Successful !" to the user. // I don't think yet it be right but after got sure the Json is returned want to show the message to the user.
var frm = $("#send-contact"), // our form
url = frm.attr("action"), // our post action
dta = frm.serialize(); // our data to be posted
$.post(url, data, function(data) {
// data has our returned Json from our view
//$(".myModal").Show(stResult); Instead I want a place like here to just show the result
$(".error-message").text(data.message); // just to see it, do what you want
//$(".loading").hide(); // no more loading as we have what we need
});
-- Also I'm eager to know how to return and show the stResult when the json returned. Thanks for any further completion of you answer and your help.
$.post()?