在Asp .Net Web Api中使用对象(Using object in Asp .Net Web Api)

编程入门 行业动态 更新时间:2024-10-27 10:26:27
在Asp .Net Web Api中使用对象(Using object in Asp .Net Web Api)

我正在使用淘汰赛,而我正在使用的模型中有很多项目。 我将它发布到控制器,如下所示:

ajaxRequest("post", "/api/care/saveevent?student=" + self.StudentId(),self.CurrentEvent()) .done(function (allData) { alert("ran ok"); }) .fail(function () { alert("An error occurred"); });

我的控制器有这个:

public string SaveEvent(object data, int student) { return "test"; }

我想使用对象,这样我就不必复制控制器或其他任何地方的淘汰模型中的所有东西,所以我可以修改一次。

它发布好,并且在调试对象数据中的所有内容时都是正确的。 但是,我不正常使用对象,也不确定如何获取它的内容,只需放入data.PropertyName会导致智能感知错误。

获取其内容的正确方法是什么,这是我想做的不可能的事情?

I am using knockout and the model I'm using has alot of items in it. I am posting it to a controller like so:

ajaxRequest("post", "/api/care/saveevent?student=" + self.StudentId(),self.CurrentEvent()) .done(function (allData) { alert("ran ok"); }) .fail(function () { alert("An error occurred"); });

And my controller has this:

public string SaveEvent(object data, int student) { return "test"; }

I want to use object so that I don't have to replicate everything in the knockout model on the controller or anywhere else so I can modify it once.

It posts ok and when debugging everything in the object data is correct. However I don't use object normally and not sure how to get it's contents as is, simply putting data.PropertyName causes intellisense errors.

What's the correct way to get its contents and is what I'm trying to do impossible?

最满意答案

如果要避免强类型(C#)模型类,可以使用dynamic类型。

但是,我建议您创建一个具有模型绑定属性的类。 您必须在两个地方进行更改,然后才是真的,但是您可以获得IntelliSense等的所有优势。(此外,在不修改服务器端的情况下,只需向客户端JavaScript代码添加更多属性,在大多数情况下可能都不够用。)

If you want to avoid a strongly typed (C#) model class, you can use the dynamic type.

I would, however, recommend you create a class with the properties you're model binding against. You will have to make changes in two places then, true, but you get all the advantages of IntelliSense etc. (Besides, simply adding more properties to your client-side JavaScript code without modifying the server side will probably be insufficient in most cases anyway.)

更多推荐

本文发布于:2023-08-03 10:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1386887.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   Net   Asp   Web   object

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!