简体   繁体   English

如何将我的 razor model 的属性传递给部分?

[英]How do I pass a property of my razor model to a partial?

I have a razor page, where the SomeList property of my page model is of type List<Blah> , with the following:我有一个 razor 页面,其中我的页面 model 的 SomeList 属性是List<Blah>类型,具有以下内容:

@await Html.PartialAsync("_QueueTable", Model.SomeList)

I have a partial with the following:我有以下部分:

@page
@model List<Blah>

When I hit my breakpoint, trying to access Model gives me a NullReferenceException.当我到达断点时,尝试访问 Model 会给我一个 NullReferenceException。

If I inspect ViewData inside the Non-Public members of the ViewBag I see the Model I'm looking for.如果我检查 ViewBag 的非公共成员中的 ViewData,我会看到我正在寻找的 Model。

How do I set this up properly so that I can simply reference Model in my partial so I can act on my list?如何正确设置它,以便我可以在我的部分中简单地引用 Model 以便我可以对我的列表采取行动?

the problem was that I had an @page directive at the top of my partial问题是我的部分顶部有一个@page 指令

I tried to remove the directive when I created the partial, but VS gave me an error saying that it must be present.我在创建部分时尝试删除该指令,但 VS 给了我一个错误,说它必须存在。

I didn't give it another thought, until a co-worker finally pointed it out.我没有再想它,直到一位同事终于指出了这一点。 I removed the directive, did a clean, did a build and I'm in business.我删除了指令,进行了清理,进行了构建,然后我就开始营业了。

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

相关问题 如何将模型属性传递给具有其他模型的局部视图? - How do I pass a model property to a partial view with a different model? 如何将属性从剃刀页面模型传递给 asp-route-id 标签助手 - How do I pass a property from a razor page model to asp-route-id tag helper 在Razor中,如果URL已存在于模型的属性中,该如何在图像中显示? - In Razor, how do I show in image if the URL is already in a property of my model? 如何将ViewModel属性作为模型传递给局部视图? - How to pass ViewModel property as model to partial view? 如何将倍数和选择性模型传递到MVC剃刀局部视图中? - How to pass multiples and selective Model into MVC razor Partial View? MVC 4 - 如何将模型数据传递给局部视图? - MVC 4 - how do I pass model data to a partial view? 如果另一个局部视图使用其他模型,如何传递具有特定模型的局部视图? - How do I pass a partial view with a specific model if another partial view is using a different model? 如何将 model 属性从 Razor 查看到 JavaScript ZC1C425268E68385D1AB5074C17A - How to pass model property from Razor View to JavaScript function 如何将HTML传递到Razor部分视图? - How to pass in HTML to a Razor Partial View? 如何在剃刀页面中将模型传递给_Layout.cshtml 页面中的局部视图 - How to pass a model to a partial view in _Layout.cshtml page in razor pages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM