MVC2 AJAX - 根据返回的数据确定UpdateTargetId

发布于 2024-10-10 15:14:22 字数 1709 浏览 2 评论 0原文

场景:

我正在为 MVC2 应用程序创建登录表单。

我是如何做的

表单提交到验证用户名/密码的 MVC2 操作。如果验证失败,操作将返回表单(部分视图)供用户重试。如果通过验证,该操作将返回用户登录之前访问的页面(视图)。

我想要发生的事情:

1 - 当表单提交并且用户验证成功时,返回的结果应该替换当前页面(就像如果不设置 UpdateTargetId 会发生什么)。

2 - 当提交表单并且用户验证失败时,返回的结果应该替换表单(就像将 UpdateTargetID 设置为表单的包含元素时会发生的情况一样)。

问题:

我可以让这两件事同时发挥作用,但不能同时进行。我可以让它始终替换当前页面,或者始终仅替换 UpdateTargetId 元素的内容。但我需要它能够根据用户是否成功验证来执行任一操作。

我需要什么

理想的解决方案是能够检查 ajax 请求的结果并确定是否使用 UpdateTargetId(仅替换表单)或不使用(替换整个页面)。我希望它会涉及到一些 jquery 的工作(假设这是可能的),但我对 jquery 并不是那么擅长,还没有弄清楚如何自己做。如果不能以这种方式完成,我也愿意接受其他方法/解决方案,以使其以类似的方式工作。

预先感谢..

按要求。操作的代码。但正如我所说。这是每个 mvc 应用程序默认附带的标准登录操作。唯一的区别是当验证失败时它返回部分视图。

[HttpPost]
    [MedLife.Code.Compression.CompressFilter]
    public ActionResult SignIn(LogOnModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            if (MembershipService.ValidateUser(model.UserName, model.Password))
            {
                FormsService.SignIn(model.UserName, model.RememberMe);
                if (!String.IsNullOrEmpty(returnUrl))
                {
                    return Redirect(returnUrl);
                }
                else
                {
                   return RedirectToAction("Index", "Home");
                }
            }
            else
            {
                ModelState.AddModelError("", "The user name or password provided is incorrect.");
            }
        }

        // If we got this far, something failed, redisplay form
        return PartialView("Overlays/LoginControl", model);
    }

The scenario:

I'm creating a login form for an MVC2 application.

How i'm doing it:

The form submits to an MVC2 action which validates the username/password. If it fails validation the action returns the form (a partial view) for the user to try again. If it passes validation the action returns the page the user was visiting before they logged in (a view).

What i want to happen:

1 - when the form is submitted and the user validates successfully, The returned result should replace the current page (like what happens if you don't set an UpdateTargetId).

2 - When the form is submitted and the user fails validation, the returned result should replace the form (like what happens if you set the UpdateTargetID to the form's containing element).

The problem:

I can make both of those things work, but not at the same time. I can either have it always replace the current page, or always just replace the contents of the UpdateTargetId element. But I need it to be able to do either depending on whether the user successfully validated or not.

What I need

The ideal solution would be to be able to examine the result of the ajax request and determine whether to use the UpdateTargetId (replacing just the form) or not (replacing the whole page). I expect it would involve some work with jquery (assuming it's possible) but i'm not really that great with jquery yet to figure out how to do it myself. If it can't be done this way I'm also open to other methods/solutions for making it work in a similar fashion.

Thanks in advance ..

As requested. Code for the action. But as i said. It's the standard LogOn action that ships default with every mvc application. The only difference is it returns a partial view when validation fails.

[HttpPost]
    [MedLife.Code.Compression.CompressFilter]
    public ActionResult SignIn(LogOnModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            if (MembershipService.ValidateUser(model.UserName, model.Password))
            {
                FormsService.SignIn(model.UserName, model.RememberMe);
                if (!String.IsNullOrEmpty(returnUrl))
                {
                    return Redirect(returnUrl);
                }
                else
                {
                   return RedirectToAction("Index", "Home");
                }
            }
            else
            {
                ModelState.AddModelError("", "The user name or password provided is incorrect.");
            }
        }

        // If we got this far, something failed, redisplay form
        return PartialView("Overlays/LoginControl", model);
    }

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

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

发布评论

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

评论(2

谷夏 2024-10-17 15:14:22

假设如果登录名/密码无效,您将返回带有字符串“INVALID”的 Json 结果。然后使用 UpdateTargetId。

如果 Json 结果!=“INVALID”,那么您将用新的部分页面替换容器。

我就是这么做的,而且效果很好。 :-)

Let's assume you return a Json result with the string "INVALID" if the login/password is invalid. Then you use the UpdateTargetId.

If the Json result != "INVALID" then you would replace the container with new partial page.

That's how I do it and it works great. :-)

邮友 2024-10-17 15:14:22

在服务器端代码中,检查验证是否有效,如果无效,则将 RedirectToAction 返回到您需要的任何完整视图。

您遇到的问题是所有应用程序逻辑,而不是显示逻辑,因此应该在控制器而不是视图中完成,以保持您的关注点分开。

您必须执行 RedirectToAction (即,不仅仅是 return View("myViewWhenInvalid");),因为否则视图将在 UpdateTargetID 中呈现,这看起来不太好。

编辑:

即:

public ActionResult myLoginAction(string name, string pwd)
{
// do the login stuff
if (loggedIn) 
{
return RedirectToAction(...);
}
else
{
return MyJsonAction();
}
}

public MyJsonAction()
{
return some Json...;
}

PS:我感冒了,所以不愿意检查我的想法。

In your server side code, check to see if the validation works, if not, return a RedirectToAction to whatever full view you need.

The issue you have is all application logic, as opposed to display logic, so it should be done in the Controller rather than the view to keep your concerns separate.

You have to do a RedirectToAction (ie, not just return View("myViewWhenInvalid");) because otherwise the view will be rendered in the UpdateTargetID, which won't look good.

EDIT:

Ie:

public ActionResult myLoginAction(string name, string pwd)
{
// do the login stuff
if (loggedIn) 
{
return RedirectToAction(...);
}
else
{
return MyJsonAction();
}
}

public MyJsonAction()
{
return some Json...;
}

PS: I have a cold, so disinclined to check my idea.

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