指出重音或视觉工作室表明JSON变量是模型的实例

发布于 2025-01-22 02:04:27 字数 1119 浏览 0 评论 0原文

我的前端库有时需要我的MVC应用程序(例如JQGrid)的JSON数据。我可以在需要时通过将所需的模型类转换为JSON来容纳这种情况。但是,我遇到了一个问题,使这种模型有时会变得困难。只有在C#templating(老式的Webforms或Razor)中使用它,resharper或Visual Studio都无法在模型转换为JSON之后识别此“模型访问”。

我有什么办法可以暗示VS或RESHARPER,例如,JS变量RowObject JQGrid中使用的等同于C#类myModel的使用情况?理想情况下,我可以得到诸如IntelliSense之类的东西,但更重要的是,因此,Resharper的“查找使用情况” /它的属性可以确定在此处使用它。 (例如,mymodel.name =>的“查找用法”的此用法rowobject.name

// <Back-End>
public JsonResult JsonList(string userName)
{
    var model = new List<MyModel>()
    ...
    return Json(model.OrderByDescending(x => x.WhenSent), JsonRequestBehavior.AllowGet);
}

// <Front-End>
$("#jQGridExample").jqGrid({
    url: '<%= Url.Action("JsonList", "MyController") %>',
    datatype: "json",
    ...
    formatter: function (cellvalue, options, rowObject) {
    // rowObject is of type "MyModel" but neither tool will know this.
    // If I remove a property from "MyModel" without updating the JS & rowObject uses it,
    // this can become an easily missed front-end regression.
    }

My front-end libraries sometimes require JSON data from my MVC application (e.g. jQGrid). I'm accommodating this when needed by converting the required model classes to JSON. However I'm running into an issue where refactoring such models can occasionally become difficult; neither ReSharper, nor Visual Studio, can identify this "model access" after the model's been converted into JSON-- only when it's used within C# templating (either old-school WebForms or Razor).

Is there any way I can hint to either VS or ReSharper that, for instance, the JS variable rowObject used within jQGrid is equivalent to a usage of the C# class MyModel? Ideally so I can get things like intellisense, but more importantly, so that ReSharper's "Find Usages" of the class / its properties can identify that it's being used here. (e.g. "Find Usages" of MyModel.Name => this usage of rowObject.Name)

// <Back-End>
public JsonResult JsonList(string userName)
{
    var model = new List<MyModel>()
    ...
    return Json(model.OrderByDescending(x => x.WhenSent), JsonRequestBehavior.AllowGet);
}

// <Front-End>
$("#jQGridExample").jqGrid({
    url: '<%= Url.Action("JsonList", "MyController") %>',
    datatype: "json",
    ...
    formatter: function (cellvalue, options, rowObject) {
    // rowObject is of type "MyModel" but neither tool will know this.
    // If I remove a property from "MyModel" without updating the JS & rowObject uses it,
    // this can become an easily missed front-end regression.
    }

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

好菇凉咱不稀罕他 2025-01-29 02:04:27

不,因为不仅有一个层的休息(前端与后端),而且还有技术 - 即JS vs. C#。只要在编译器/R#级别上C#和JS之间没有直接的桥梁,这将是不可能的。

当然,您可以通过在JS/TypeScript中创建与C#吊坠相似的JS/Typescript(含义相同的名称,属性等)的模型来减轻这些问题,例如.com/esporta/70515122/share-models-betel-bet-bet-c-sharp-and-typescript“>此处和此处。但是它们在技术上不是相同的。

No, because there is not only a layer-wise break (front vs. back end), but a technical one as well - namely JS vs. C#. As long as there is no direct bridge between C# and JS on a compiler/R# level, this will remain impossible.

Of course you can mitigate those issues by creating models in JS/TypeScript that are similar to their C# pendants (meaning same names, properties, etc.) - e. g. here and here. But they are not technically the same.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文