在 MVC 中使用 MembershipCreateStatus

发布于 2024-08-28 22:17:25 字数 1097 浏览 2 评论 0原文

如何在下面的控制器中使用 MembershipCreateStatus 来识别错误?

我下面的控制器创建了一个新用户,但我想捕获 CreateStatus 中的任何错误并将错误添加到我的模型状态中。

我不断收到以下状态错误。

   [HttpPost]
    public ActionResult CreateUser(user UserToCreate)
    {
        if (ModelState.IsValid)
        {
            // TODO: If the UserToCreate object is Valid we'll
            //Eventually want to save it in a database

            MembershipCreateStatus status;
            MembershipService newMembershipService = new MembershipService();
            MembershipCreateStatus newUser = newMembershipService.CreateUser(UserToCreate.Username, UserToCreate.Password, UserToCreate.Email,out MembershipCreateStatus **status**);

            if (newUser == MembershipCreateStatus.Success)
            {
                return RedirectToAction("Index", "Home");
            }
            else
            {
                ModelState.AddModelError(createStatus);
                return Redirect("/");
            }               
        }
        //Invalid - redisplay form with errors
        return View(UserToCreate);
    }

How can I use the MembershipCreateStatus in my controller below to identify errors?

My controller below creates a new user but I would like to catch any errors from CreateStatus and add the error to my modelstate.

I keep getting errors for status below.

   [HttpPost]
    public ActionResult CreateUser(user UserToCreate)
    {
        if (ModelState.IsValid)
        {
            // TODO: If the UserToCreate object is Valid we'll
            //Eventually want to save it in a database

            MembershipCreateStatus status;
            MembershipService newMembershipService = new MembershipService();
            MembershipCreateStatus newUser = newMembershipService.CreateUser(UserToCreate.Username, UserToCreate.Password, UserToCreate.Email,out MembershipCreateStatus **status**);

            if (newUser == MembershipCreateStatus.Success)
            {
                return RedirectToAction("Index", "Home");
            }
            else
            {
                ModelState.AddModelError(createStatus);
                return Redirect("/");
            }               
        }
        //Invalid - redisplay form with errors
        return View(UserToCreate);
    }

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

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

发布评论

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

评论(1

七月上 2024-09-04 22:17:25

您可以在这篇 msdn 文章中查看如何获取 MembershipCreateStatus 文本。您还必须处理可能发生的其他异常。由于代码很多,我建议将其移到一个名为 MembershipService 的单独类中,然后仅调用其中的方法。

You can see in this msdn article how to get the MembershipCreateStatus text. You also have to handle other exceptions that can occure. Since it is a lot of code I suggest to move it in a separate class called MembershipService and just call a method from it.

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