Unbreakable
Unbreakable

Reputation: 8112

ViewDataDictionary vs anonymous object in ASP.net MVC 5

I am a beginner and I am going through some tutorials in my MVC. So, I came across two scenarios.

Scenario 1.

I had to pass some data to my view on post and then send that data as hidden field. Here is the code.

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult ForgotPassword(ForgotPasswordMV viewModel)
    {
        if (ModelState.IsValid)
        {
             return RedirectToAction("VerifyToken", new { emailId = viewModel.EmailId });
        }
                                           ^^ USING ANONYMOUS OBJECTS
        return View();
    }

    public ActionResult VerifyToken(string emailId = null)
    {
        VerifyTokenMV viewModel = new VerifyTokenMV
        {
            EmailId = emailId
        };
        return View(viewModel);
    }

VerifyToken View

@using (@Html.BeginForm("VerifyToken", "Security"))
{
    @Html.HiddenFor(m => m.EmailId)
    <button class="btn btn-primary">Continue</button>
}

Works Perfectly fine. I am able to receive values of EmailId. So far so good.

Scenario 2.

Needed to open a partial view from Main view, here is the snippet.

Main cshtml file

 <div class="abc">
        @Html.Partial("../Widget/Customize", Model.Unit, new ViewDataDictionary() { { "ElementName", "UnitWidget" } })
    </div>

partial cshtml file

@{ 
    string custWidgetElementName = ViewBag.ElementName;    
}
// some html code below

Observation: In scenario 2 why have I used ViewDataDictionary. Although both example works perfectly fine. But is there any reason that I had to use ViewDataDictionary. In scenraio 1 can we use ViewDataDictionary? If Yes, then which one is optimum solution.

Question: When I need to pass values shall I use new {key : value} or use ViewDataDictionary or there is no corelation? Instead of ViewDataDictionary can I use anonymous object in Senario 2

Upvotes: 1

Views: 704

Answers (1)

CodeNotFound
CodeNotFound

Reputation: 23220

Your two scenarios are totally different. They are not doing the same thing.

In scenario 1 when using this line:

return RedirectToAction("VerifyToken", new { emailId = viewModel.EmailId });

A new URL is genrated and sent back to the client (the browser) with HTTP Code 301 or 302. When received the browser will re-contact your application wiht the received URL. With that URL, your application will execute the associated action. In your case, the client's browser will call VerifyToken action with the emailId parameter setted when you call RedirectionToAction into ForgotPassword action. So using RedirectionToAction method is just telling that method to generate a new URL with parameter defined in the anonymous type.

In scenario 2 is completely different to scenario 1. With this line:

@Html.Partial("../Widget/Customize", Model.Unit, new ViewDataDictionary() { { "ElementName", "UnitWidget" } })

You're telling your view to inject the partial view which path is ../Widget/Customize. Because that partial view the strongly typed, you passed Model.Unit as an second parameter. You use also a third parameter new ViewDataDictionary() { { "ElementName", "UnitWidget" } } because your partial seems to internally need to access to the dynamic property ViewBag or dictionary property ViewData of your view.

Conclusion:

In scenario 1 you are just telling the client's browser to go to the new URL you have generated after requesting ForgetPassword URL. We just call that a rediretion.

In scenario 2, you're just rendering a partial view into a view. The client's broswer doesn't know anything what's going on with partial views they don't know if they exist.

Upvotes: 1

Related Questions