After posting a form to database is it possible to call a jquery from asp.net mvc…..
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(FormCollection collection)
{
try
{
//Insert to db
//Call a jquery function
return RedirectToAction("Index");
}
catch
{
return View();
}
}
should i use ScriptManager.RegisterClientScriptBlock after the insert or any other way to do this… Any suggestion…
No. You cannot call a jQuery function from an action. The way to do this in MVC is to simply add the function in the View’s
.aspxpage and just wrap it in the$(document).ready(function(){})(or w/e the function is for your library) so that when the page fully loads, the function will be called.It is important to note that with MVC you have complete control of the HTML (JavaScript included) output and you should utilize that. While in WebForms its best to avoid using inline tags
<% %>, in MVC, you’re expected to use them to generate the HTML you want.So, assuming that the
Insert to dbwill return something, like anID, you can place thisIDvalue in theViewDataorTempDatasince you’re usingRedirectToActionand then use it to call the function.And somewhere in the
.aspxpage that is used for theCreateaction:When the page is rendered,
<%: ViewData["MyID"] %>is replaced with the actual value generated in the action.