开发者

on asp.net mvc 3 using the razor engine, what's the best practice to pass data between multiple views?

first of all, sorry for my english

I am new to ASP.NET MVC and was trying to develop a simple web application using the Razor Engine

so I have this view called Extract, which accepts an url as input:

@using (Html.BeginForm("Begin", "Rss"))
{
@Html.LabelFor(m => m.Url) @Html.TextBoxFor(m => m.Url)
<开发者_开发技巧button>Extrair</button>
}

when submited, it will send the url to my controller:

public ActionResult Begin(ExtractModel m)
        {
            if (ModelState.IsValid)
            {
                var extractedData = ExtractorService.Extract(m.Url);
                if (extractedData != null)
                {
                    TempData["extractedData"] = extractedData;
                    return RedirectToAction("Extracted", extractedData);
                }
            }
            return View();
        }

then a new view called Extracted will show all the links extracted from the rss passed:

public ActionResult Extracted(ExtractedModel m)
        {
            if (TempData["extractedData"] != null)
            {
                ViewData["extractedData"] = TempData["extractedData"];
                return View(ViewData["extractedData"] as ExtractedModel);
            }
            else
            {
                return RedirectToAction("Extract");
            }
        }

-

@using (Html.BeginForm())
{
    foreach (var data in Model.Data)
    {
<ul>
    <li><a href="@data.Link">@data.Link</a></li>
</ul>
    }
<button>Converter</button>
}

bottom line what I want to ask is: how do I get the ViewData["extractedData"] which I set when loading this View so I can pass it back to the controller and parse all the info inside of it? because when I click on the button Converter my ViewData is empty and I can't process anything without it =\


I wouldn't use TempData for passing complex objects between the views. I would also get rid of ViewData.

Then I would rather have the controller action rendering the view fetch whatever information it needs:

public class RssController: Controller
{
    public ActionResult Extract()
    {
        var model = new ExtractModel();
        return View(model);
    }

    [HttpPost]
    public ActionResult Begin(string url)
    {
        if (ModelState.IsValid)
        {
            return RedirectToAction("Extracted", new { url = url });
        }
        return View();
    }
}

have the corresponding view which allows for entering the url (~/Views/Rss/Extract.cshtml):

@model AppName.Models.ExtractModel
@using (Html.BeginForm("Begin", "Rss"))
{
    @Html.LabelFor(m => m.Url) 
    @Html.TextBoxFor(m => m.Url)
    <input type="submit" value="Extrair" />
}

and in the other action you are redirecting to:

public ActionResult Extracted(string url)
{
    var extractedData = ExtractorService.Extract(url);
    if (extractedData != null)
    {
        return View(extractedData);
    }
    return RedirectToAction("Extract");
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜