如何让JQuery返回View?

发布于 2024-11-08 06:09:11 字数 535 浏览 0 评论 0原文

以前,我让表单直接调用 Action,但现在,我想在调用 Action 之前使用 JQuery 来做一些事情。我遇到的问题是,之前,当我从控制器调用 return View() 时,它会使用我放入 Viewbag.Message 的消息重新加载部分页面。我如何使用 JQuery 做到这一点?

$("#button").click(function () {
    DoStuff();
});
function DoStuff()
{
    $.ajax("Home/DoStuff"), function() {

    };
}

$

[HttpPost]
public ActionResult DoStuff()
{
    string value = Request.Form["value"];

    Viewbag.Message = TempData["message"];

    CustomView view = new CustomView();
    return View(view);
}

Before, I had my form directly call the Action, but now, I want to use JQuery to do stuff before having the Action called. The problem I have is that before, when I called return View() from the controller, it would reload the partial page with the message I put into the Viewbag.Message. How do I do that with JQuery?

$("#button").click(function () {
    DoStuff();
});
function DoStuff()
{
    $.ajax("Home/DoStuff"), function() {

    };
}

$

[HttpPost]
public ActionResult DoStuff()
{
    string value = Request.Form["value"];

    Viewbag.Message = TempData["message"];

    CustomView view = new CustomView();
    return View(view);
}

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

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

发布评论

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

评论(1

我偏爱纯白色 2024-11-15 06:09:39

考虑这样的事情(未测试):

<html>
    ...
<body>
    ...
    @Html.RenderAction("DoStuff", new { value: "" });
    ...
</body>

部分视图DoStuff.cshtml:

<div class="doStuffContent">
<script type="text/javascript">
$("#button").click(function () {
    DoStuff();
});
function DoStuff()
{
    $.ajax(
    {
        url: @Url.Action("DoStuff"),
        data: {value: "testData"},
        success: function(result) 
            {
                $("doStuffContent").replaceWith(result);
            }});
}
</script>
<a id="button">click me</a>
@ViewBag.message
</div>

操作:

[HttpGet]
public ActionResult DoStuff()
{
    Viewbag.Message = "";

    return View();
}

[HttpPost]
public ActionResult DoStuff(string value)
{
    Viewbag.Message = "Received " + value + "!";

    return View();
}

Consider something like this (not tested):

<html>
    ...
<body>
    ...
    @Html.RenderAction("DoStuff", new { value: "" });
    ...
</body>

Partial View DoStuff.cshtml:

<div class="doStuffContent">
<script type="text/javascript">
$("#button").click(function () {
    DoStuff();
});
function DoStuff()
{
    $.ajax(
    {
        url: @Url.Action("DoStuff"),
        data: {value: "testData"},
        success: function(result) 
            {
                $("doStuffContent").replaceWith(result);
            }});
}
</script>
<a id="button">click me</a>
@ViewBag.message
</div>

Action:

[HttpGet]
public ActionResult DoStuff()
{
    Viewbag.Message = "";

    return View();
}

[HttpPost]
public ActionResult DoStuff(string value)
{
    Viewbag.Message = "Received " + value + "!";

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