mrblah
mrblah

Reputation: 103497

Passing strongly typed viewdata to view page in asp.net-mvc

Do I have to manually pass my strongly typed viewdata to the call return View(); ?

ie.

MyViewData vd = new MyViewData();

vd.Blah = "asdf asdfsd";

return View();

It seems if I pass it as a parameter, I have to repeat the view name also?

return View("index", vd);

Upvotes: 1

Views: 338

Answers (3)

user434917
user434917

Reputation:

you can simply pass the model the the View method:

MyViewData vd = new MyViewData();

vd.Blah = "asdf asdfsd";

return View(vd);

Upvotes: 1

Garry Shutler
Garry Shutler

Reputation: 32698

You can do this:

public ActionResult Action()
{
    var vd = new MyViewData();

    vd.Blah = "asdf asdfsd";

    ViewData.Model = vd;

    return View();
}

Upvotes: 0

User
User

Reputation: 30945

Normally you don't have to manually pass it, but your model has to have a constructor without parameters. Otherwise the framework won't know what values you would like it to pass there.

As for passing the view name, just check out all method overloads. If there is one with just the model as a parameter, then you can omit the view name.

Upvotes: 0

Related Questions