ASP.net核心:从视图中调用不同控制器的方法是否正常?

时间:2016-11-07 17:45:23

标签: c# asp.net asp.net-mvc razor

我有两个控制器:FirstControllerSecondController

第一个控制器包含GET方法GetFirstData,用于发送一些数据。

第二个控制器包含类似的方法GetSecondData,用于发送不同的数据。

我还为每个控制器提供了2个视图Index.cshtml。

我知道Index.cshtml视图中的call GetFirstData方法是正常的。但是从GetSecondData的Index.cshtml调用FirstController方法是否合适?例如。将AJAX与@Url.Action("GetSecondData", "Second")

一起使用

2 个答案:

答案 0 :(得分:1)

是的,你可以这样做。如果您使用Ajax调用该方法,则不会有任何问题。

答案 1 :(得分:1)

是的,你可以肯定这样做,你可以简单地使用控制器方法作为AJAX的网络电话,但我建议:

1 - 如果GetSecondDataviews之间controllers使用了controller,我建议您将其分开controller

为什么呢?因为看起来这种方法比一般具有viewsActions

的特定AJAX function更为通用

2 - 我还建议您将views分隔在一个单独的JS文件中,并将其包含在对function感兴趣的SELECT * FROM ( --Get delta from 0 to 150 SELECT s1.hole ,s1.depth ,s1.az ,s1.dip --Create a row number for each hole (each hole starts at 1), ordered by the depth in ascending order (0,150,200 etc) ,row_number() OVER ( PARTITION BY s1.hole ORDER BY s1.depth ) AS rn ,s1.az - s2.az / (s1.depth - s2.depth) AS DeltaAZ ,convert(DECIMAL(5, 2), s1.dip) - convert(DECIMAL(5, 2), s2.dip) / (s1.depth - s2.depth) AS DeltaDip FROM survey s1 INNER JOIN ( SELECT hole ,depth ,az ,dip ,row_number() OVER ( PARTITION BY hole ORDER BY depth ) AS rn FROM survey ) s2 --Using the same row number calculation as above, we can specific exactly which record to return. Because we have explicitly defined how to calculate the row number, --we know 1 will always be the lowest depth (0), therefore 2 will be the second lowest ON s1.hole = s2.hole AND s2.rn = 1 -- Return the first result, which is the lowest WHERE left(s1.hole, 3) = 'AB1' ) s1 WHERE s1.rn = 2 --Limit the result to the second highest depth, since that's what you showed on your question (depth = 150) --Using the same logic, but different row numbers, run the same result again as a union UNION ALL SELECT * FROM ( SELECT s2.hole ,s2.depth ,s2.az ,s3.az AS s2az ,s2.dip ,s3.dip AS s2dip ,row_number() OVER ( PARTITION BY s2.hole ORDER BY s2.depth ) AS rn ,s2.az - s3.az / (s2.depth - s3.depth) AS DeltaAZ ,convert(DECIMAL(5, 2), s2.dip) - convert(DECIMAL(5, 2), s3.dip) / (s2.depth - s3.depth) AS DeltaDip FROM survey s2 INNER JOIN ( SELECT hole ,depth ,az ,dip ,row_number() OVER ( PARTITION BY hole ORDER BY depth ) AS rn FROM survey ) s3 ON s2.hole = s3.hole --This time, we will compare the 3rd result with the second result. AND s3.rn = 2 -- only return the second record ) s2 WHERE s2.rn = 3 AND left(hole, 2) = 'AB' ORDER BY s1.hole ,s1.depth 中。