1

I have a problem that includes presenting all data from a table through a stored procedure. I'm using LINQ to access my stored procedure but the thing is that my result (data shown) is only the last row from my tables. I can't get it to work... would deeply appreciate if someone could help me / explain what im doing wrong.

Model: 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;
            }

        }

Controller: RecipeController

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

            return View(rec);
}

View (Razor): Index

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

<legend>Recipe </legend>

        <p>@Html.DisplayFor(model => model.rName)</p>
5
  • 1
    You really need to give us what error you're getting if you want any hope of us helping you Commented Aug 15, 2012 at 13:54
  • 3
    You're overriding the values in the foreach loop in each iteration Commented Aug 15, 2012 at 13:57
  • GetAllRecipes method should not be in your model. It should be in your controller. Commented Aug 15, 2012 at 13:59
  • Justin: Yes. The Procedure is only a select statement that returns all the data from the table (like: SELECT * FROM tblx). Nothing fancy. Commented Aug 15, 2012 at 14:00
  • @LordHits: I want different methods, like the method GetAllRecipes, that i wanna call in my controller. I dont want methods in my controller... Commented Aug 15, 2012 at 14:03

4 Answers 4

1

You're not actually returning the value of your stored procedure, you're overwriting the properties of RecipeModel instead.

You should create a Recipe class to hold the return from your stored procedure:

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; }
}

Then alter your procedure to fill this in - I'm assuming that db.p_get_all_recipes() is returning a queryable or list:

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;
        });

}

Then you'll need to change your view:

@model IQueryable<Recipe>

And your controller action:

[HttpGet]
        public ActionResult Index()
        {
            RecipeModel rec = new RecipeModel();    
            return View( rec.GetAllRecipes(););
        }
Sign up to request clarification or add additional context in comments.

5 Comments

Its complaining about "missing cast". Ive tried this, but without success.
Can you post your model? Do you have a recipe class? I was hoping you do, but you didn't post it so its possible you dont
@peter: Its only private variables and get / set. Nothing else really.
I've added some details, i hope that helps
it seems like Stored Procedures dont return a queryable lists... im banging my head to against the wall right now. Thx tho!
0

You have a problem in your model. You load all recipes from your SP and after that in cycle you are trying to set values from the result to RecipeModel fields. But in your cycle if you have more than one recipe you are doing next: in iteration 1 you get values from recipe 1 and set them to RecipeModel, after that you goind to iteration 2 and all values from recipe 2 you again set to the same object RecipeModel which already has values from recipe 1, so you just override these values.

SO you need to create a separate RecipeModel per recipe from database. Your model which you pass to view should be a list of RecipeModels and in your View you should have foreach cycle which will write recipes to html.

Comments

0

Your overwriting you local variables in the following for loop:

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;
}

You need to add each iteration to a new instance of your Model and return a List or other collection type.

1 Comment

Alex: Yeah thats what i think i have to do... but i cant figure out how to do it. Ive been googling my arse off. fail
0

You have a RecipeModel class inside which you have a method to get all items ( A Collection of RecipeModel). Look carefully to your method. You are looping thru the collection and setting the proeperties of the Class (object of a class effectivily) again and again (Overwriting to same instance)

Ideally In the loop you need to create an instance of RecipeModel and set the values and Add that to a collection of RecipeModel.

I would not mix this method inside your Model class. I would move that to a seperate class where there will be a method to return a list of RecipeModel class. A Repositary method.

1 Comment

Ok thanks for the architecture tips. How do i add results from my SP to a list? Sorry for the meta question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.