如何从ASP调用存储的proc。Net MVC栈通过ORM &返回json?

时间:2022-09-15 11:57:48

i'm a total newbie with asp.net mvc and here's my jam:

我是asp.net mvc的新手,这是我的果酱:

i have a 3 level list box which selection on box A shows options on box B and selection on box B will show the options for box C.

我有一个3级列表框,在a框中选择显示B框中的选项,在B框中选择显示C框的选项。

I'm trying to do the whole thing in asp.net MVC and what i see is that the nerd dinner tutorial uses the ORM method.

我尝试在asp.net MVC中做所有的事情,我看到的是书呆子晚餐教程使用ORM方法。

so i created a dbml to the database and drag the stored proc inside. i create a datacontext object but i don't quite know how to connect the result from the stored proce which should be multiple rows of data and make it into a json.

因此,我创建了一个dbml到数据库,并将存储的proc拖到其中。我创建了一个datacontext对象,但我不太清楚如何连接来自存储过程的结果,存储过程应该是多行数据,并将其变为json。

so i can keep all the json data inside the html page and using jquery i could make the selection process faster.

因此,我可以将所有json数据保存在html页面中,使用jquery可以使选择过程更快。

i don't expect the data inside the three boxes to change so often thus i think this method should be quite viable.

我不认为这三个盒子里的数据会经常变化,因此我认为这个方法应该是可行的。

Questions:

  1. So how do i get the stored proc part to return the data as json?

    那么如何让存储的proc部分以json形式返回数据呢?

  2. i've noticed some tutorial online that the json return result part is at the controller and not at the model end. Why is that?

    我注意到一些在线教程,json返回结果部分在控制器中,而不是在模型端。这是为什么呢?

Edit FYI, i find what i mostly wanted to do here. For the json part, i referenced here.

编辑FYI,我找到我在这里最想做的。对于json部分,我在这里引用。

1 个解决方案

#1


2  

  1. Return a JsonResult from your controller action. You may need to coerce the result from your stored procedure into a C# class serializable to Json.

    从控制器动作返回一个JsonResult。您可能需要将存储过程的结果强制转换成Json格式的c#类。

  2. Json conversion should be done in the controller because it's not really part of the domain. More a DTO in the MVVM (Model-View-ViewModel) style.

    Json转换应该在控制器中进行,因为它不是域的一部分。更多的是MVVM(模型-视图-视图-视图模型)风格中的DTO。

#1


2  

  1. Return a JsonResult from your controller action. You may need to coerce the result from your stored procedure into a C# class serializable to Json.

    从控制器动作返回一个JsonResult。您可能需要将存储过程的结果强制转换成Json格式的c#类。

  2. Json conversion should be done in the controller because it's not really part of the domain. More a DTO in the MVVM (Model-View-ViewModel) style.

    Json转换应该在控制器中进行,因为它不是域的一部分。更多的是MVVM(模型-视图-视图-视图模型)风格中的DTO。