ViewData 和 javascript - 类名未被读取

发布于 2024-12-12 05:27:23 字数 1758 浏览 0 评论 0原文

由于某种原因,我在将视图数据分配给某些 javascript 时遇到问题。

我有以下代码:

@{ 
string item = ".playerItem_" + ViewData["itemType"];
}

<script type="text/javascript">
 $(function () {

console.log(item);
$(".items.droppable").droppable({
                    accept: '@item',
                    drop: function (event, ui) {
                    //...
                    }
 });

});

</script>

如果我查看控制台,我可以看到正在打印“.playerItem_hat”。如果我在“accept”属性中输入“.playerItem_hat”,它就可以正常工作。但是,一旦我在接受属性中使用@item,它似乎就不会获取类名。我可以看到源代码中打印的文本,所以它肯定在那里。更奇怪的是,如果我将“item”硬编码为“.playerItem_hat”,则可放置的东西就会起作用。我已经将范围缩小到每次使用 ViewData["itemType"] 时。我查看了一些建议 javascript.serialize 的其他线程,但这似乎是一个单独的问题。

我缺少有关页面生命周期的信息吗?在 jquery 调用中使用 ViewData 是否有任何可能导致问题的情况?如果文本根本没有打印出来,我可以理解,但我可以在那里看到它。很奇怪。

还有其他人遇到过这个问题吗?

*更新*

好的,我想我已经解决了问题。如果其他人遇到这个问题,这似乎是一个相当容易的陷阱。

上面的代码位于通过 Html.Partial 调用的页面内。上面的 ViewData 是我传递到页面的字符串,有 5 个不同的调用。

 Html.Partial("PageModules/Shared/ItemModule", Model, new ViewDataDictionary() {{"moduleType", "head"}})}

 Html.Partial("PageModules/Shared/ItemModule", Model, new ViewDataDictionary() {{"moduleType", "feet"}})}

 Html.Partial("PageModules/Shared/ItemModule", Model, new ViewDataDictionary() {{"moduleType", "hands"}})

等等。问题似乎是生成了上面的代码,但实际上并未输出到所谓的输出流?换句话说,第一个调用生成 html,然后第二个调用生成其 html 并更改 moduleType 的值。此时,html 尚未实际插入到保存这些 Partial 调用的页面中。至少,据我所知,是这样的。

诀窍是强制它输出 html,然后转到下一个部分调用并根据需要修改 ViewData 的值。我的解决方案是添加:

    @{ string itemBarHtml = Html.Partial("PageModules/Shared/GraphicalModuleBar", Model, new ViewDataDictionary() {{"moduleType", "head"}}).ToString();
     }

    @Html.Raw(itemBarHtml)

这听起来合理吗?

For some reason I'm having trouble assigning viewdata to some javascript.

I have the following code:

@{ 
string item = ".playerItem_" + ViewData["itemType"];
}

<script type="text/javascript">
 $(function () {

console.log(item);
$(".items.droppable").droppable({
                    accept: '@item',
                    drop: function (event, ui) {
                    //...
                    }
 });

});

</script>

If I look at the console I can see '.playerItem_hat' being printed. If I type '.playerItem_hat' into the 'accept' attribute it works fine. But as soon as I use @item inside the accept attribute it doesn't seem to be picking up the class name. I can see the text being printed in the source so its definitely there. Even stranger is that if I hardcode 'item' to '.playerItem_hat', the droppable works. I've narrowed it down to whenever I use ViewData["itemType"]. I've looked at some other threads that suggest javascript.serialize, but that seems to be a seperate issue.

Is there something about the page lifecycle I'm missing? Is there anything that might be causing an issue by using ViewData inside a jquery call? If the text wasn't printing at all I could understand but I can see it in there. Very odd.

Anyone else ever run into this issue?

*Update *

Ok so I think I've figured out the problem. If anyone else runs into this it seems like it's a fairly easy trap.

The code above is inside a page that is being called through a Html.Partial. The ViewData above is a string I'm passing through to the page and there are 5 different calls.

 Html.Partial("PageModules/Shared/ItemModule", Model, new ViewDataDictionary() {{"moduleType", "head"}})}

 Html.Partial("PageModules/Shared/ItemModule", Model, new ViewDataDictionary() {{"moduleType", "feet"}})}

 Html.Partial("PageModules/Shared/ItemModule", Model, new ViewDataDictionary() {{"moduleType", "hands"}})

and so on. What the issue appears to be is that the code above is generated, but not actually output to the, what's is called, output stream? In other words, The first call generates the html, then the second generates its html and changes the value of moduleType. At this point the html has not been actually inserted into the page that holds these Partial calls. At least, as far as I can tell it's something along those lines.

The trick is to force it to output the html, THEN move onto the next partial call and modify the value of the ViewData as it sees fit. My solution is to add:

    @{ string itemBarHtml = Html.Partial("PageModules/Shared/GraphicalModuleBar", Model, new ViewDataDictionary() {{"moduleType", "head"}}).ToString();
     }

    @Html.Raw(itemBarHtml)

Does this sound plausible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文