从代码隐藏调用 JQuery - 任何其他解决方案
我正在 MVC 2 中工作。我想从我的代码后面(即从控制器)调用 JQuery 函数...我的 JQuery 函数是...
<script type="text/javascript">
$(function () {
var a = document.getElementById("HidStatus").value;
var b = parseInt(a);
$("#progressbar").progressbar({
value: b,
max: 100
});
});
<div id="progressbar" style="height: 8px; float: left; padding: .3%; margin-right: 274px;
margin-left: 160px; width: 350px;">
</div>
<div id="Div1" style="float: left; margin-left: 300px; margin-right: 100px; font-weight: bold">
<%= Html.Hidden("HidStatus", (double)ViewData["StatusBar"])%>
<%= Html.Label("Status - " + Convert.ToString(ViewData["StatusBar"] + "% Completed"))%>
</div>
我应该在我的控制器中调用它。我的控制器将...
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ProvideDetails(FormCollection formCollection)
{
//Here i should call the JQuery Function
return PartialView("Details", empDetails);
}
这是我的 Ajax 相关部分...
<% AjaxOptions ajaxOption = new AjaxOptions(); ajaxOption.UpdateTargetId = "TargetId"; 使用 (Ajax.BeginForm("Provide", "Emp", new { }, ajaxOption, new { id = "EmpForm" })) { %> <% 使用 (Html.BeginForm("Provide", "Emp")) {%>
<% CurrentFormMode currentMode = new CurrentFormMode(); if (ViewData["FormMode"] != null) currentMode = (CurrentFormMode)ViewData["FormMode"]; %> <%EmpDetails empDetails = new EmpDetails(); if (ViewData.ContainsKey("EmpDetails")) empDetails = (EmpDetails)ViewData["EmpDetails"]; %>
<%if (!string.IsNullOrEmpty(Html.CelloValidationMessage("SuccessMessage"))) {%>
<%}%> <%}%>
如何做到这一点......
I am working in MVC 2.I want to call a JQuery function from my code behind (i.e. From Controller)...My JQuery function is...
<script type="text/javascript">
$(function () {
var a = document.getElementById("HidStatus").value;
var b = parseInt(a);
$("#progressbar").progressbar({
value: b,
max: 100
});
});
<div id="progressbar" style="height: 8px; float: left; padding: .3%; margin-right: 274px;
margin-left: 160px; width: 350px;">
</div>
<div id="Div1" style="float: left; margin-left: 300px; margin-right: 100px; font-weight: bold">
<%= Html.Hidden("HidStatus", (double)ViewData["StatusBar"])%>
<%= Html.Label("Status - " + Convert.ToString(ViewData["StatusBar"] + "% Completed"))%>
</div>
i should call this in my controller. My controller wil be...
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ProvideDetails(FormCollection formCollection)
{
//Here i should call the JQuery Function
return PartialView("Details", empDetails);
}
This is my Ajax Related Part...
<% AjaxOptions ajaxOption = new AjaxOptions();
ajaxOption.UpdateTargetId = "TargetId";
using (Ajax.BeginForm("Provide", "Emp", new { }, ajaxOption, new { id = "EmpForm" }))
{
%>
<% using (Html.BeginForm("Provide", "Emp"))
{%>
<% CurrentFormMode currentMode = new CurrentFormMode();
if (ViewData["FormMode"] != null)
currentMode = (CurrentFormMode)ViewData["FormMode"]; %>
<%EmpDetails empDetails = new EmpDetails();
if (ViewData.ContainsKey("EmpDetails"))
empDetails = (EmpDetails)ViewData["EmpDetails"];
%>
<%if (!string.IsNullOrEmpty(Html.CelloValidationMessage("SuccessMessage")))
{ %>
<%} %>
<%} %>
How to do this....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
后面的代码在服务器上运行。当它运行时,页面尚未交付给用户并且尚未在他们的浏览器中呈现。
这意味着当您无法访问 jQuery 代码时,因为它仅在客户端上运行。
即使它可以运行,您也无法使用
document.getElementById
,因为它获取网页中尚未呈现的元素。The code behind is run on the server. When it is run, the page has not yet been delivered to the user and has not been rendered in their browser.
This means that when you cannot access the jQuery code since it is run only on the client.
Even if it could be run, you would not be able to use
document.getElementById
since it gets an element in the webpage, which has not yet been rendered.如果我理解正确..用户正在执行某些操作,您希望在服务器上执行某些操作,然后更新进度栏以向用户指示发生了某些事情。
我认为你有几个选择。
使用 jQuery post
您可以使用 jQuery 调用控制器操作,然后根据响应更新进度栏。进度条更新回调函数将被放置到“成功”(见下文)
请参阅 jQuery API 文档
使用 Ajax.BeginForm
看来您现在正在使用 PartialView。您可以使用 Ajax 形式,以便您可以提供在发布完成后调用的函数。 Ajax.BeginForm 包含 Success 选项,您可以在其中提供在 Post 成功完成时调用的函数。在该功能中,您可以更新进度条。
这是一个示例如何使用 Ajax.BeginForm
我希望这将给出如何实施正确解决方案的想法。
If I understand correctly.. User is doing some action, you want to do something on the server and then update progress bar to give indication to the user that something has happened.
I think you have couple of options.
Using jQuery post
You could call controller action using jQuery and then update your progress bar according to the response. Progress bar update callback function would be placed to the 'success' (see below)
See jQuery API documentation
Using Ajax.BeginForm
It seems that you're now using PartialView. You could use Ajax form, so that you can provide function that is called when post has been done. Ajax.BeginForm contains Success option where you can give function that is called when Post has been completed successfully. In that function you could update your progress bar.
Here is an example how to using Ajax.BeginForm
I hope that this will give an idea how to implement proper solution.