在MVC 5中执行存储过程时出现问题 [英] Problem in executing Store Procedure in MVC 5

查看:157
本文介绍了在MVC 5中执行存储过程时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想执行一个Store Procedure并将结果返回到视图,调试后我的参数在SP中传递但是它什么也没有返回,最终导致错误。



传入字典的模型项目类型为System.Data.Entity.Infrastructure.DbRawSqlQuery`1 [SocialAuthentication.Models.User s,但此字典需要类型的模型项SocialAuthentication.Models.Users。



我已经明白我已经将模型声明为字符串预期输出不同我该如何修复它。



型号
public class 用户
{
public string 名称{获得; set ; }
public string 电子邮件{ get ; set ; }
}
查看
@using(Html.BeginForm( 联系人 Home))
{
< input id = Text1 type = < span class =code-string> text
name = txtOne /> ;
< input id = Button1 type = 提交 value = 按钮 />
}

控制器

[HttpPost]
public ActionResult联系人()
{
string textboxValue = Request.Form [ txtOne];
var result = db.Database.SqlQuery< Users>( usp_searchuser,@ UserInput new SqlParameter( @ UserInput,textboxValue));
return 查看(结果);

}





商店程序



  CREATE   procedure  usp_searchuser   -  -   'Sara Nani' 
@ UserInput varchar 50
as
开始
选择电子邮件,名称来自 tblEmployee 其中名称= @ UserInput
结束

解决方案

您尚未将模型正确地继承到视图。你必须像这样继承它

@model StartWithMVC.Models.Employee。



欲了解更多细节,你可以通过开始使用MVC - 推动初学者入门(第1部分) [ ^

Hi,
I want to execute a Store Procedure and return the result to the view,after debugging my parameter is passed in SP but it returns nothing and ends up in error.

The model item passed into the dictionary is of type System.Data.Entity.Infrastructure.DbRawSqlQuery`1[SocialAuthentication.Models.User s, but this dictionary requires a model item of type SocialAuthentication.Models.Users.

I have understood that I have declared the model as string the expected output is different how do I fix it.

Model
public class Users
    {   
        public string Name { get; set; }
        public string Email { get; set; }
    }
View
 @using (Html.BeginForm("Contact", "Home"))
    {
    <input id="Text1" type="text" name="txtOne" />
    <input id="Button1" type="submit" value="button" />
    }

   Controller

   [HttpPost]
        public ActionResult Contact()
        {
            string textboxValue = Request.Form["txtOne"];
            var result = db.Database.SqlQuery<Users>("usp_searchuser, @UserInput", new SqlParameter("@UserInput", textboxValue));
            return View(result);
            
        }



Store Procedure

CREATE procedure usp_searchuser --'Sara Nani'
@UserInput varchar(50)
As
Begin
select Email,Name from tblEmployee where Name = @UserInput
End

解决方案

You have not inherited the model properly to the view. You have to inherit it like this
@model StartWithMVC.Models.Employee.

For more details you can go through
Start With MVC - Push Start For Beginners(Part - 1)[^]


这篇关于在MVC 5中执行存储过程时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆