简体   繁体   English

传递强类型的viewdata来查看asp.net-mvc中的页面

[英]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(); 我是否必须手动将我的强类型viewdata传递给调用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);

you can simply pass the model the the View method: 您只需将模型传递给View方法即可:

MyViewData vd = new MyViewData();

vd.Blah = "asdf asdfsd";

return View(vd);

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. 如果有一个仅将模型作为参数,则可以省略视图名称。

You can do this: 你可以这样做:

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

    vd.Blah = "asdf asdfsd";

    ViewData.Model = vd;

    return View();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM