After posting a form to database is it possible to call a jquery from asp.net mvc.....
将表单发布到数据库后,可以从asp.net mvc调用jquery .....
[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...
我应该在插入后使用ScriptManager.RegisterClientScriptBlock或以任何其他方式执行此操作...任何建议......
1 个解决方案
#1
1
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 .aspx
page 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.
不能。您无法通过操作调用jQuery函数。在MVC中执行此操作的方法是简单地在View的.aspx页面中添加该函数,并将其包装在$(document).ready(function(){})中(或者该函数适用于您的库)这样当页面完全加载时,将调用该函数。
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.
重要的是要注意,使用MVC,您可以完全控制HTML(包含JavaScript)输出,您应该使用它。虽然在WebForms中最好避免使用内联标记<%%>,但在MVC中,您需要使用它们来生成所需的HTML。
So, assuming that the Insert to db
will return something, like an ID
, you can place this ID
value in the ViewData
or TempData
since you're using RedirectToAction
and then use it to call the function.
因此,假设Insert to db将返回一些内容,例如ID,您可以将此ID值放在ViewData或TempData中,因为您正在使用RedirectToAction,然后使用它来调用该函数。
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(FormCollection collection)
{
int newID = InsertToDBAndReturnID();
ViewData["MyID"] = newID;
return View();
}
And somewhere in the .aspx
page that is used for the Create
action:
在.aspx页面中用于Create操作的某处:
<script type="text/javascript">
$(document).ready(function() {
// I have direct access to the "MyID" value from the action
// so I can utilize it to write any JavaScript with it
$('#'+<%: ViewData["MyID"] %>).click(function(){
alert('You clicked the control with the new ID');
});
});
</script>
When the page is rendered, <%: ViewData["MyID"] %>
is replaced with the actual value generated in the action.
呈现页面时,<%:ViewData [“MyID”]%>将替换为操作中生成的实际值。
#1
1
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 .aspx
page 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.
不能。您无法通过操作调用jQuery函数。在MVC中执行此操作的方法是简单地在View的.aspx页面中添加该函数,并将其包装在$(document).ready(function(){})中(或者该函数适用于您的库)这样当页面完全加载时,将调用该函数。
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.
重要的是要注意,使用MVC,您可以完全控制HTML(包含JavaScript)输出,您应该使用它。虽然在WebForms中最好避免使用内联标记<%%>,但在MVC中,您需要使用它们来生成所需的HTML。
So, assuming that the Insert to db
will return something, like an ID
, you can place this ID
value in the ViewData
or TempData
since you're using RedirectToAction
and then use it to call the function.
因此,假设Insert to db将返回一些内容,例如ID,您可以将此ID值放在ViewData或TempData中,因为您正在使用RedirectToAction,然后使用它来调用该函数。
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(FormCollection collection)
{
int newID = InsertToDBAndReturnID();
ViewData["MyID"] = newID;
return View();
}
And somewhere in the .aspx
page that is used for the Create
action:
在.aspx页面中用于Create操作的某处:
<script type="text/javascript">
$(document).ready(function() {
// I have direct access to the "MyID" value from the action
// so I can utilize it to write any JavaScript with it
$('#'+<%: ViewData["MyID"] %>).click(function(){
alert('You clicked the control with the new ID');
});
});
</script>
When the page is rendered, <%: ViewData["MyID"] %>
is replaced with the actual value generated in the action.
呈现页面时,<%:ViewData [“MyID”]%>将替换为操作中生成的实际值。