开发者

multi steps information submission using TemData IN ASP.Net MVC

I am working on a MVC web form where user will submit the Album information in the first step, after submitting the Album in开发者_如何学Cformation in the second step user can submit the track information data.But i am little confused how i ll do in MVC , in normal webform it is easy for me to do .I am looking for some sloution for this.


If you're not writing the Album information to some kind of permanent storage at the end of step 1, e.g. database, file etc., then you could use TempData to keep track of the information entered into the wizard between page requests.

TempData uses Session state under the hood and is only persistent across a single request, so you would need write your album/track information to TempData at the end of every controller action and read it in again at the beginning of every action. Then, once you have accumulated all the album/track information, write it to permanent storage.


You might also consider just storing the album and Track ids in the URL. There are some benefits to this, like the ability to bookmark and email the URL to friends.

When you pass a RouteValueDIctionary to the UrlHelper.Action() method, any parameters in the dictionary that are not part of the route will be added into the query. So you could do this:

return Redirect(Url.Action("myaction", "mycontroller", 
    new { album = selectedAlbumVariable, track = selectedTrackVariable }));

And them retrieve them in the action by adding them to the action parameters like this:

public ActionResult SomeAction(string album, string track) { ... }

You would have to check for null on album and track before using them.


You can follow what pmarflee suggested, here is a litle example, using TempData:

public ActionResult Step1()
{
    var MyObject = new List<string>();
    //Some Logic
    //Set the TempData
    TempData["data"] = MyObject;
    return RedirectToAction("Step2");
}

public ActionResult Step2()
{
    //Get the Data From the TempData and Cast it
    var MyObject = (List<string>)TempData["data"];
    //Continue working
    return View();
}

Just remember that this data will be available just for 1 more action, if you want to continue passing data you must set the data again in the TempData Dictionary.

You can also use another approach and serialize the data into a hidden field, this approach is cleaner but require more work, if you just want to save a couple of fields, TempData is good to go.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜