0

Im using VS2012, MVC4 C#, SQL 2008 R2

I have a stored procedure which returns a list contains two computed columns. In my SP I have two parameters which I need to pass from View, these parameters will be generated from javascript on page load and stored in HiddenFields.

I have followed this Post, when I created a reference like

Rest Context = new Rest();
List<Rest> RT = Context.ExecuteStoreQuery......

I get an error.

Could someone tell me the right way to call a stored procedure and pass values from View.

4
  • You might consider posting the error. Commented Jun 28, 2013 at 18:15
  • @alan Model doest contain ExecuteStoreQuery and no extension method Commented Jun 28, 2013 at 18:23
  • Given the error above I'm assuming the code you outlined in your question is in the view. You want to create a model to use in your view, this model can be populated in the controller by calling the sproc. How familiar are you with MVC4? Commented Jun 28, 2013 at 19:38
  • Ive figured out this question, below is the answer. Commented Jun 29, 2013 at 12:55

1 Answer 1

1

This is how I got o/p with Model, View and Controller

In my Model class, I have two .cs files, DefaultConnection.cs and Rest.cs

public class DefaultConnection : DbContext
{
    public DbSet<Rest> Restart { get; set; }
}

In Rest.cs

public class Rest
{
    //These two properties are columns in Stored Procedure, Id -PK, 
    //Distance - Computed column in SP
    public int Id { get; set; }   
    public double Distance { get; set; }
}

In Controller

Public ActionResult Get_Data()
{
    DefaultConnection Context = new DefaultConnection();
    IEnumerable<Rest> results = Context.Database.SqlQuery<Rest>
        ("Your SP @para1 = xyz, @para2 = abc") //If you have parameters or ("Just SP")
        .ToList();
    return View(results);
}

In View

@using MVC.Models
@model IEnumerable<Rest>

@{
     ViewBag.Title = "GetData";
 }

 <h2>GetData</h2>

 @foreach (Rest rt in @Model)
 {
   <table>
       <tr>
           <td>@rt.Id</td><td></td>
           <td>@rt.Distance</td>
        </tr>
   </table>

 }
Sign up to request clarification or add additional context in comments.

Comments

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.