Passing strongly typed views to pageviews in asp.net-mvc

Do I have to manually pass my strongly typed data to represent the return () call;

t.

MyViewData vd = new MyViewData();

vd.Blah = "asdf asdfsd";

return View();

      

I seem to be passing it as a parameter, I need to echo the view name as well?

return View("index", vd);

      

+1


a source to share


3 answers


you can simply pass the View method to the model:



MyViewData vd = new MyViewData();

vd.Blah = "asdf asdfsd";

return View(vd);

      

+1


a source


You usually don't need to manually pass it, but your model should have a parameterless constructor. Otherwise the framework will not know what values ​​you would like to pass there.



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

0


a source


You can do it:

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

    vd.Blah = "asdf asdfsd";

    ViewData.Model = vd;

    return View();
}

      

0


a source







All Articles