ASP.NET MVC 3 - 告诉partialview重新获取viewdata值?

发布于 2024-12-18 07:46:45 字数 623 浏览 1 评论 0原文

假设我有一个像这样的部分视图 X.ascx:

<div id = "updateTargetIdForAjaxForm">
... javascript code which only has function definitions.
... ajax form with buttons inside
    <div id = "stuff"></div>
    <script type = "text/javascript">
       do things to "stuff" div as soon as X.ascx is loaded. 
    </script>
</div>

现在我想根据 Ajax 响应更新 X.ascx 上的内容。早些时候,我返回了 PartialView,但是内联 javascript 执行了填充 div 的操作,这意味着当 X.ascx 重新加载时,内联 javascript 不会被执行。

那么有没有办法我不能在控制器中返回 PartialView,而只是更新 ViewData 值并告诉 PartialView 重新获取更新的 ViewData 值?也许还可以根据服务器的响应调用一两个 JavaScript 函数?

Let's say I have a partial view X.ascx like this:

<div id = "updateTargetIdForAjaxForm">
... javascript code which only has function definitions.
... ajax form with buttons inside
    <div id = "stuff"></div>
    <script type = "text/javascript">
       do things to "stuff" div as soon as X.ascx is loaded. 
    </script>
</div>

Now I want to update things on X.ascx, based on Ajax response. Earlier, I was returning PartialView but inline javascript that does things to stuff div wouldn't like that, meaning the inline javascript just won't get executed when X.ascx is reloaded.

So is there a way I can not return PartialView in my controller, but just update ViewData values and tell the PartialView to re-grab the updated ViewData values? And also call one or two javascript functions based on the response from server maybe?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

守望孤独 2024-12-25 07:46:45

我看到两种可能的方法:

修复您的 javascript 代码以使用动态加载的部分视图运行:

这两个应该可以帮助你。

另一种方法是在控制器上创建一个操作,该操作将返回 JSON 结果,并且您应该能够从 jQuery 处理 JSON 对象并更新表单。您的操作可能如下所示:

public JsonResult GetFormValues()
{
    var jsonFormValues = new
    {

        key1 = "abc",
        key2 = "123",
        key3 = "abcdef"

    };
    return Json(jsonFormValues, JsonRequestBehavior.AllowGet);
}

并查看 http://api.jquery.com/jQuery.getJSON/< /a> 关于如何处理结果

I see two possible ways to go:

Fixing your javascript code to run with dynamically loaded partial view:

These two should help you out.

The other way is to create an action on your controller that would return JSON result and from jQuery you should be able to handle JSON object and update your form. Your Action could look like :

public JsonResult GetFormValues()
{
    var jsonFormValues = new
    {

        key1 = "abc",
        key2 = "123",
        key3 = "abcdef"

    };
    return Json(jsonFormValues, JsonRequestBehavior.AllowGet);
}

and check out http://api.jquery.com/jQuery.getJSON/ on how to handle the result

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文