如何在类库中创建ASP.NET Core ViewResult?
在我的ASP.NET Core网站中,我有一个类库,它可以执行一些无聊的逻辑,但最终返回:
new RedirectResult(“ - some wory url”);
new ViewResult(... )
现在我不确定如何将模型传递到viewResult
?
我尝试了以下操作:
var viewResult = new ViewResult
{
ViewName = "MarketingPreferences",
};
viewResult.ViewData.Model = myModel; // Exception is thrown :(
但是viewData
属性为null。
所以我想:创建它,对吗?
viewResult.viewData = new ViewDatadictionary(..)
,但我不知道该输入什么:(
有两个选项:
public viewDataDationary(viewDataDadictionary source);
>public ViewDataDictionary(Imodelmetadataprovider Metadataprovider,ModelStateDictionary ModelState);
,我没有第一个选项中的现有viewDataDicticaly
...
我不知道。我不知道imodelmetadataprovider
是什么
?
In my ASP.NET Core website, I have a class library which does some boring logic but finally returns either:
new RedirectResult("-some url");
new ViewResult(....)
now I'm not sure how I can pass a model to the ViewResult
?
I tried this:
var viewResult = new ViewResult
{
ViewName = "MarketingPreferences",
};
viewResult.ViewData.Model = myModel; // Exception is thrown :(
but the viewData
property is null.
So then I thought: just create it, right?
viewResult.ViewData = new ViewDataDictionary(..)
but I don't know what to pass into that :(
There are the two options:
public ViewDataDictionary(ViewDataDictionary source);
public ViewDataDictionary(IModelMetadataProvider metadataProvider, ModelStateDictionary modelState);
and I don't have an existing ViewDataDictionary
in the first option ...
and for the second .. i have no idea what an IModelMetadataProvider
is?
Can anyone help, please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果要将模型传递到
viewResult
,则可以按以下代码:If you want to pass model to the
ViewResult
, You can follow this code: