asp.NET MVC3 中使用静态对象的向导

发布于 2024-11-16 16:41:40 字数 2740 浏览 3 评论 0原文

我正在尝试使用实体框架在 MVC3 中制作一个向导。它需要通过几个步骤来保持对象(在本例中为文章)的状态。
我的控制器中有一个静态变量,它实例化了一篇新文章。在不同的操作中,我使用 TryUpdateModel 将表单映射到静态变量。问题是,TryUpdateModel() 似乎也更新了数据库。我需要 TryUpdateModel 进行自动映射,并更新静态 _article 变量,但我不希望它保留到数据库直到最后一步!

注意:我知道在 MVC 中创建向导有很多可能的解决方案,但我想知道如何才能使这种方式发挥作用,因此请不要选择 MVC 向导模式。

谢谢。

namespace website.Controllers
{
    public class ArticlesController : BaseController
    {
        // private static variable to hold the chosen article in the wizard
        private static articles _article = new articles();

    /// <summary>
    /// Index page shows a list of articles in a webgrid
    /// </summary>
    /// <returns></returns>
    public ActionResult Index()
    {
        List<articles> _articles = Data.getArticles();
        return View(_articles);
    }

    /// <summary>
    /// First page of the article wizard
    /// </summary>
    /// <returns></returns>
    public ActionResult BasicDetails(string id, string nextButton)
    {

        // back or next doesn't matter - store form values
        if (_article != null) TryUpdateModel(_article);

        if (nextButton != null)
        {
            return RedirectToAction("ArticleGroup");
        }
        else
        {
            _article = Data.GetArticleById(id);
            return View(_article);
        }
    }

    /// <summary>
    /// Second page of the article wizard
    /// </summary>
    /// <returns></returns>
    public ActionResult ArticleGroup(string nextButton, string backButton)
    {
        TryUpdateModel(_article);

        if (backButton != null)
            return RedirectToAction("BasicDetails");
        else if (nextButton != null)
        {
            return RedirectToAction("Price");
        }
        else
        {
            return View(_article);
        }
    }

    /// <summary>
    /// Third page of the article wizard
    /// </summary>
    /// <returns></returns>
    public ActionResult Price(string nextButton, string backButton)
    {

        TryUpdateModel(_article);

        if (backButton != null)
        {
            return RedirectToAction("ArticleGroup");
        }
        else if (nextButton != null)
            return RedirectToAction("LinkedClubs");
        else
        {
            return View(_article);
        }
    }

    /// <summary>
    /// Last page of the article wizard
    /// </summary>
    /// <returns></returns>
    public ActionResult LinkedClubs(string backButton)
    {

        if (backButton != null)
            return RedirectToAction("Price");
        else
            return View(_article);
    }


}
}

I'm trying to make a wizard in MVC3 using Entity Framework. It needs to keep the state of an object (an article in this case) across a couple of steps.
I have a static variable in my controller that instantiates a new Article. In the different Actions I use TryUpdateModel to map the form to the static variable. The problem is, it seems that TryUpdateModel() updates the database as well. I need TryUpdateModel to do the automatic mapping, and update the static _article variable, but I don't want it to persist to the database until the last step!

N.B: I know there are a lot of possible solutions for creating a wizard in MVC, but I'd like to know what to do to make this way work, so please no alternatives for an MVC wizard-pattern.

Thanks.

namespace website.Controllers
{
    public class ArticlesController : BaseController
    {
        // private static variable to hold the chosen article in the wizard
        private static articles _article = new articles();

    /// <summary>
    /// Index page shows a list of articles in a webgrid
    /// </summary>
    /// <returns></returns>
    public ActionResult Index()
    {
        List<articles> _articles = Data.getArticles();
        return View(_articles);
    }

    /// <summary>
    /// First page of the article wizard
    /// </summary>
    /// <returns></returns>
    public ActionResult BasicDetails(string id, string nextButton)
    {

        // back or next doesn't matter - store form values
        if (_article != null) TryUpdateModel(_article);

        if (nextButton != null)
        {
            return RedirectToAction("ArticleGroup");
        }
        else
        {
            _article = Data.GetArticleById(id);
            return View(_article);
        }
    }

    /// <summary>
    /// Second page of the article wizard
    /// </summary>
    /// <returns></returns>
    public ActionResult ArticleGroup(string nextButton, string backButton)
    {
        TryUpdateModel(_article);

        if (backButton != null)
            return RedirectToAction("BasicDetails");
        else if (nextButton != null)
        {
            return RedirectToAction("Price");
        }
        else
        {
            return View(_article);
        }
    }

    /// <summary>
    /// Third page of the article wizard
    /// </summary>
    /// <returns></returns>
    public ActionResult Price(string nextButton, string backButton)
    {

        TryUpdateModel(_article);

        if (backButton != null)
        {
            return RedirectToAction("ArticleGroup");
        }
        else if (nextButton != null)
            return RedirectToAction("LinkedClubs");
        else
        {
            return View(_article);
        }
    }

    /// <summary>
    /// Last page of the article wizard
    /// </summary>
    /// <returns></returns>
    public ActionResult LinkedClubs(string backButton)
    {

        if (backButton != null)
            return RedirectToAction("Price");
        else
            return View(_article);
    }


}
}

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

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

发布评论

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

评论(2

简单 2024-11-23 16:41:40

您应该传递一个保存页面之间所需信息的状态包,而不是使用静态变量来保存状态信息(顺便说一句,这是一个严重错误)。

Rather than using a static variable to hold your state information (that is a critical error btw) you should pass a state bag holding the information that you need in between pages.

人生百味 2024-11-23 16:41:40

通常数据实体(映射到数据库的实体)和视图模型实体(与该用户工作的实体)分开使用。当用户在某个步骤后发布数据时 - 您将 TryUpdateModel() 设置为会话对象(特定于用户,而不是作为静态变量用于所有应用程序)。
最后一步,您调用业务逻辑方法 UpdateModel(viewmodel),该方法使用所有填充的属性,通过 viewmodel 的 id 更新所有列。

Usually data entities (entities mapped to database) and viewmodel entities (entities with that user works) used separately. When user posted data after some step - you make TryUpdateModel() to session object (specific for user, not for all application as static variable).
At last step you call business logic method UpdateModel(viewmodel), that update all columns by id of viewmodel, using all filled properties.

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