开发者

Wizard in asp.NET MVC3 using a static object

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 开发者_JAVA百科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);
    }


}
}


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜