Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#(MVC3)存储过程LINQ SQL_C#_Asp.net Mvc 3_Linq_Stored Procedures - Fatal编程技术网

C#(MVC3)存储过程LINQ SQL

C#(MVC3)存储过程LINQ SQL,c#,asp.net-mvc-3,linq,stored-procedures,C#,Asp.net Mvc 3,Linq,Stored Procedures,我有一个问题,包括通过存储过程显示表中的所有数据。我使用LINQ访问存储过程,但问题是我的结果(显示的数据)只是表中的最后一行。我不能让它工作。。。如果有人能帮助我/解释我做错了什么,我将不胜感激 模型:RecipeModel public class RecipeModel { ..... public void GetAllRecipes() { DataClassesDataContext db = new DataClassesDa

我有一个问题,包括通过存储过程显示表中的所有数据。我使用LINQ访问存储过程,但问题是我的结果(显示的数据)只是表中的最后一行。我不能让它工作。。。如果有人能帮助我/解释我做错了什么,我将不胜感激

模型:
RecipeModel

public class RecipeModel
{
.....
        public void GetAllRecipes()
        {
            DataClassesDataContext db = new DataClassesDataContext();        

            var result = db.p_get_all_recipes();

            foreach (var r in result)
            {
                this.recipeName = r.name;
                this.recipeDescription = r.description;
                this.recipeSteps = r.steps;
                this.createdAt = r.createdAt;
                this.updatedAt = r.updatedAt;
                this.ownerID = r.owner_id;
            }

        }
控制器
往复控制器

public class RecipeController : Controller
{
        [HttpGet]
        public ActionResult Index()
        {
            RecipeModel rec = new RecipeModel();
            rec.GetAllRecipes();

            return View(rec);
}
查看(剃须刀)
索引

@model MVC3_LINQ_SP.Models.RecipeModel
@{
    ViewBag.Title = "Index";
}

<legend>Recipe </legend>

        <p>@Html.DisplayFor(model => model.rName)</p>
@model MVC3\u LINQ\u SP.Models.RecipeModel
@{
ViewBag.Title=“Index”;
}
配方
@DisplayFor(model=>model.rName)


您的模型有问题。从SP加载所有配方,然后在循环中尝试将结果中的值设置为RecipeModel字段。但在你的循环中,如果你有多个配方,你接下来要做的是:在迭代1中,你从配方1中获取值,并将它们设置为RecipeModel,然后你进入迭代2,并且从配方2中获得的所有值,你再次设置为同一个对象RecipeModel,它已经具有配方1中的值,所以你只需覆盖这些值


所以,您需要从数据库中为每个配方创建一个单独的RecipeModel。您传递给视图的模型应该是一个RecipeModel列表,在您的视图中,您应该拥有将配方写入html的foreach cycle

您的模型有问题。从SP加载所有配方,然后在循环中尝试将结果中的值设置为RecipeModel字段。但在你的循环中,如果你有多个配方,你接下来要做的是:在迭代1中,你从配方1中获取值,并将它们设置为RecipeModel,然后你进入迭代2,并且从配方2中获得的所有值,你再次设置为同一个对象RecipeModel,它已经具有配方1中的值,所以你只需覆盖这些值


所以,您需要从数据库中为每个配方创建一个单独的RecipeModel。您传递给视图的模型应该是一个RecipeModel列表,在您的视图中,您应该拥有将配方写入html的foreach cycle

在以下for循环中覆盖局部变量:

foreach (var r in result)
{
    this.recipeName = r.name;
    this.recipeDescription = r.description;
    this.recipeSteps = r.steps;
    this.createdAt = r.createdAt;
    this.updatedAt = r.updatedAt;
    this.ownerID = r.owner_id;
}

您需要将每个迭代添加到模型的新实例中,并返回一个
列表或其他集合类型。

在以下for循环中覆盖局部变量:

foreach (var r in result)
{
    this.recipeName = r.name;
    this.recipeDescription = r.description;
    this.recipeSteps = r.steps;
    this.createdAt = r.createdAt;
    this.updatedAt = r.updatedAt;
    this.ownerID = r.owner_id;
}

您需要将每个迭代添加到模型的新实例中,并返回一个
列表或其他集合类型。

您实际上并没有返回存储过程的值,而是覆盖了
RecipeModel
的属性

您应该创建一个Recipe类来保存存储过程的返回:

public class Recipe
{
    public string recipeName { get; set; }
    public string recipeDescription { get; set; }
    public string recipeSteps { get; set; }
    public DateTime createdAt { get; set; }
    public DateTime updatedAt { get; set; }
    public DateTime ownerID { get; set; }
}
然后更改您的过程以填写此内容-我假设db.p_get_all_recipes()返回一个可查询或列表:

public IQueryable<Recipe> GetAllRecipes()
{
    DataClassesDataContext db = new DataClassesDataContext();

    return db.p_get_all_recipes().Select(r => new Recipe()
        {
        recipeName = r.name;
        recipeDescription = r.description;
        recipeSteps = r.steps;
        createdAt = r.createdAt;
        updatedAt = r.updatedAt;
        ownerID = r.owner_id;
        });

}

实际上,您并没有返回存储过程的值,而是覆盖了
RecipeModel
的属性

您应该创建一个Recipe类来保存存储过程的返回:

public class Recipe
{
    public string recipeName { get; set; }
    public string recipeDescription { get; set; }
    public string recipeSteps { get; set; }
    public DateTime createdAt { get; set; }
    public DateTime updatedAt { get; set; }
    public DateTime ownerID { get; set; }
}
然后更改您的过程以填写此内容-我假设db.p_get_all_recipes()返回一个可查询或列表:

public IQueryable<Recipe> GetAllRecipes()
{
    DataClassesDataContext db = new DataClassesDataContext();

    return db.p_get_all_recipes().Select(r => new Recipe()
        {
        recipeName = r.name;
        recipeDescription = r.description;
        recipeSteps = r.steps;
        createdAt = r.createdAt;
        updatedAt = r.updatedAt;
        ownerID = r.owner_id;
        });

}

您有一个
RecipeModel
类,其中有一个获取所有项的方法(RecipeModel的集合)。仔细看看你的方法。您正在循环遍历集合,并一次又一次地设置类(类的对象)的属性(覆盖到同一实例)

理想情况下,在循环中,您需要创建RecipeModel的实例并设置值,然后将其添加到RecipeModel的集合中


我不会在模型类中混合使用此方法。我将把它转移到一个单独的类中,其中将有一个方法返回
RecipeModel
类的列表。一个repository方法。

您有一个
RecipeModel
类,在该类中您有一个获取所有项的方法(RecipeModel的集合)。仔细看看你的方法。您正在循环遍历集合,并一次又一次地设置类(类的对象)的属性(覆盖到同一实例)

理想情况下,在循环中,您需要创建RecipeModel的实例并设置值,然后将其添加到RecipeModel的集合中


我不会在模型类中混合使用此方法。我将把它转移到一个单独的类中,其中将有一个方法返回
RecipeModel
类的列表。一种存储方法。

如果希望我们帮助您,您确实需要告诉我们您遇到了什么错误。您正在覆盖foreach循环中每个迭代的值GetAllRecipes方法不应该在您的模型中。它应该在你的控制器里。贾斯汀:是的。该过程只是一个select语句,返回表中的所有数据(如:select*from tblx)。没什么特别的。@LordHits:我想要不同的方法,比如GetAllRecipes方法,我想在我的控制器中调用它。我不想在我的控制器中使用方法…如果希望我们能帮助您,您真的需要告诉我们您遇到了什么错误。您正在覆盖foreach循环中每个迭代的值GetAllRecipes方法不应该在您的模型中。它应该在你的控制器里。贾斯汀:是的。该过程只是一个select语句,返回表中的所有数据(如:select*from tblx)。没什么特别的。@LordHits:我想要不同的方法,比如GetAllRecipes方法,我想在我的控制器中调用它。我不想在我的控制器中使用方法…亚历克斯:是的,这就是我认为我必须要做的。。。但是我不知道怎么做。我一直在用谷歌搜索我的屁股。亚历克斯:是的,这就是我想我必须做的。。。但是我不知道怎么做。我一直在用谷歌搜索我的屁股。它抱怨“演员失踪”。我试过了,但是没有成功。你能发布你的模型吗?你们有食谱课吗?我希望你能,但是你