一个主主页上的多个部分视图,asp.net mvc
嗨,我对 ASP.NET 及其功能还是有点陌生。我希望在一个主页上使用一些强类型的部分视图。我不知道如何使用存储库和 Irepository 来做到这一点。
到目前为止我只完成了此操作,并且当我尝试加载页面时不断收到错误。
<%Html.RenderPartial("~/Views/Shared/Partial.ascx", Model);%>
我是否必须使用 viewData 将信息传递给视图?任何帮助以及任何例子将不胜感激。 有人可以帮忙吗 问候
Hi I am still kinda new to asp.net and what it can do. I am looking to use a few strongly typed partial views on one main homepage. I am not sure how to do this with a repository and Irepository.
I have only done this up to now and keep getting an error when I try to load the page.
<%Html.RenderPartial("~/Views/Shared/Partial.ascx", Model);%>
Would I have to use viewData to pass information to the view?? any help would be grateful, along with any examples.
Can anybody please help
regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将与其他答案不同并说:不。您不应该不使用临时数据将信息传递到部分视图中。根据您正在做的事情,您应该创建一个复合视图模型。最佳实践表明,您不应将模型直接传递给视图,而应创建 ViewModel。
请参阅此处关于复合视图模型的已接受答案: ViewModel 最佳实践
然后:
这会满足您的情况,因为一切都会是强类型的。您的主页将强类型化为复合视图模型,并且您的部分内容将强类型化为复合视图模型中包含的视图模型。
I'm going to differ from the other answers and say: No. You should not use tempdata to pass information into a partial view. Depending on what you're doing, you should instead create a Composite View Model. Best practices state that you should not be passing Models direct to your Views and should instead create ViewModels.
See the accepted answer here about Composite View Models: ViewModel Best Practices
Then:
This satisfies your case as everything will be strongly typed. Your home page is strongly typed to the composite view model, and your partial will be strongly typed to the view model that's contained within the composite view model.
是的。
如果您不想通过相同的控制器方法将数据传递到分部视图,请考虑使用
RenderAction
而不是RenderPartial
。您将需要更多控制器方法,但每次使用分部视图时,您不必在现有控制器方法中重复数据绑定代码。Yes.
If you don't want to pass data through the same controller method to the partial views, consider using
RenderAction
instead ofRenderPartial
. You will need more controller methods, but you won't have to repeat your data binding code in your existing controller methods every time you use a partial view.