热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

MVC4:存储过程带有多个参数-MVC4:storedprocedurew/multipleparameters

UsingVisualStudioExpress2012,MVC4webapplication,andSQL2008R2使用VisualStudioExpress2

Using Visual Studio Express 2012, MVC4 web application, and SQL 2008 R2...

使用Visual Studio Express 2012,MVC4 Web应用程序和SQL 2008 R2 ...

I used to have a stored procedure that only accepted one parameter, and when my MVC model included that stored procedure, I could successfully invoke it from my Controller and get Json results just fine.

我曾经有一个只接受一个参数的存储过程,当我的MVC模型包含该存储过程时,我可以从我的Controller成功调用它并获得Json结果。

Now I have updated the stored procedure to accept two parameters. I've refreshed my MVC model but am unable to invoke the stored procedure correctly from my controller when passing two parameters.

现在我已更新存储过程以接受两个参数。我刷新了我的MVC模型但是在传递两个参数时无法从我的控制器中正确调用存储过程。

Here is my old stored procedure and controller using one parameter (this works):

这是我的旧存储过程和控制器使用一个参数(这工作):

PROCEDURE [dbo].[GetDependencyNodes]     
@CISearchString nvarchar(100)

Old Controller:

老控制器:

namespace CMDB.Controllers
{
public class GoJSDiagramNodesAndLinksController : Controller
{
   private CMDBEntities db = new CMDBEntities();
   public ActionResult GetDependencyNodes(string CISearchString)
    {
        return Json(db.GetDependencyNodes(CISearchString).ToList(), JsonRequestBehavior.AllowGet);
    }
}
}

Here is my new stored procedure and controller accepting two parameters:

这是我的新存储过程和控制器接受两个参数:

PROCEDURE [dbo].[GetDependencyNodes]
@CISearchString nvarchar(100),
@ExcludeString nvarchar(100)

New Controller:

新控制器:

namespace CMDB.Controllers
{
public class GoJSDiagramNodesAndLinksController : Controller
{
   private CMDBEntities db = new CMDBEntities();
   public ActionResult GetDependencyNodes(string CISearchString, string ExcludeString)
    {
   return Json(db.GetDependencyNodes(CISearchString, ExcludeString).ToList(), JsonRequestBehavior.AllowGet);
    }
}
}

In Visual Studio Express 2012, I get an error on my "return Json" line stating:

在Visual Studio Express 2012中,我在“返回Json”行中收到错误,指出:

'int' does not contain a definition for 'ToList' and no extension method 'ToList' accepting a first argument of type 'int' could not be found.

'int'不包含'ToList'的定义,并且找不到接受类型'int'的第一个参数的扩展方法'ToList'。

If I change the line from:

如果我更改以下行:

return Json(db.GetDependencyNodes(CISearchString, ExcludeString).ToList(), JsonRequestBehavior.AllowGet);

to:

至:

return Json(db.GetDependencyNodes(CISearchString, ExcludeString).ToString().ToList(), JsonRequestBehavior.AllowGet);

The error goes away but the controller returns a -1 instead of the correct Json results. I've tested the stored procedure in SQL Manager and can see the expected results.

错误消失但控制器返回-1而不是正确的Json结果。我已经在SQL Manager中测试了存储过程,可以看到预期的结果。

Here is partial code from my file CMDBModels.Desginer.cs with GetDependencyNodes:

以下是我的文件CMDBModels.Desginer.cs中包含GetDependencyNodes的部分代码:

    /// 
    /// No Metadata Documentation available.
    /// 
    /// No Metadata Documentation available.
    /// No Metadata Documentation available.
    public int GetDependencyNodes(global::System.String cISearchString, global::System.String excludeString)
    {
        ObjectParameter cISearchStringParameter;
        if (cISearchString != null)
        {
            cISearchStringParameter = new ObjectParameter("CISearchString", cISearchString);
        }
        else
        {
            cISearchStringParameter = new ObjectParameter("CISearchString", typeof(global::System.String));
        }

        ObjectParameter excludeStringParameter;
        if (excludeString != null)
        {
            excludeStringParameter = new ObjectParameter("ExcludeString", excludeString);
        }
        else
        {
            excludeStringParameter = new ObjectParameter("ExcludeString", typeof(global::System.String));
        }

        return base.ExecuteFunction("GetDependencyNodes", cISearchStringParameter, excludeStringParameter);
    }

Any ideas?

有任何想法吗?

Thanks.

谢谢。

1 个解决方案

#1


1  

Couple of things first move your Data Access to another class, keep your controller as light as possible.

有些事情首先将您的数据访问移动到另一个类,让您的控制器尽可能轻。

Next map your stored procure output to an basic class that has properties that match the returned columns (entity class)

接下来将存储的procure输出映射到具有与返回列匹配的属性的基本类(实体类)

this should let you look at the returned data in debug a lot easier and is just good programming practice

这应该让你更容易看看调试中返回的数据,这只是一个很好的编程实践


推荐阅读
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • 动态规划算法的基本步骤及最长递增子序列问题详解
    本文详细介绍了动态规划算法的基本步骤,包括划分阶段、选择状态、决策和状态转移方程,并以最长递增子序列问题为例进行了详细解析。动态规划算法的有效性依赖于问题本身所具有的最优子结构性质和子问题重叠性质。通过将子问题的解保存在一个表中,在以后尽可能多地利用这些子问题的解,从而提高算法的效率。 ... [详细]
  • Linux环境变量函数getenv、putenv、setenv和unsetenv详解
    本文详细解释了Linux中的环境变量函数getenv、putenv、setenv和unsetenv的用法和功能。通过使用这些函数,可以获取、设置和删除环境变量的值。同时给出了相应的函数原型、参数说明和返回值。通过示例代码演示了如何使用getenv函数获取环境变量的值,并打印出来。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文介绍了使用kotlin实现动画效果的方法,包括上下移动、放大缩小、旋转等功能。通过代码示例演示了如何使用ObjectAnimator和AnimatorSet来实现动画效果,并提供了实现抖动效果的代码。同时还介绍了如何使用translationY和translationX来实现上下和左右移动的效果。最后还提供了一个anim_small.xml文件的代码示例,可以用来实现放大缩小的效果。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 使用Ubuntu中的Python获取浏览器历史记录原文: ... [详细]
  • 本文详细介绍了Linux中进程控制块PCBtask_struct结构体的结构和作用,包括进程状态、进程号、待处理信号、进程地址空间、调度标志、锁深度、基本时间片、调度策略以及内存管理信息等方面的内容。阅读本文可以更加深入地了解Linux进程管理的原理和机制。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • 本文介绍了为什么要使用多进程处理TCP服务端,多进程的好处包括可靠性高和处理大量数据时速度快。然而,多进程不能共享进程空间,因此有一些变量不能共享。文章还提供了使用多进程实现TCP服务端的代码,并对代码进行了详细注释。 ... [详细]
  • 本文介绍了一个程序,可以输出1000内能被3整除且个位数为6的所有整数。程序使用了循环和条件判断语句来筛选符合条件的整数,并将其输出。 ... [详细]
  • 本文介绍了如何使用python从列表中删除所有的零,并将结果以列表形式输出,同时提供了示例格式。 ... [详细]
author-avatar
青春快乐的勇哥
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有