美好的一天傢伙,我在這裏有一點點冷漠。我已經使用ASP.NET MVC和C#在Visual Studio中創建了我的數據庫,模型,控制器和視圖,但我無法弄清楚如何調用我創建的存儲過程。如何在ASP.Net中調用和執行存儲過程MVC(C#)
我想讓存儲過程在我放入視圖的按鈕上調用。 該存儲過程應該在單擊該按鈕時執行並顯示結果。 下面是我創建的存儲過程,視圖,模型和控制器。
這是我的 '員工' 型號:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
namespace MVCSimpleApp.Models
{
[Table("Employees")]
public class Employee
{
[Display(Name ="Employee Id")]
public int EmployeeId { get; set; }
[Display(Name ="First Name")]
public string FirstName { get; set; }
[Display(Name ="Last Name")]
public string LastName { get; set; }
}
}
這是我的數據上下文:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace MVCSimpleApp.Models
{
public class EmployeeContext : DbContext
{
public DbSet<Employee> Employee { get; set; }
}
}
這是我的員工控制器:
using MVCSimpleApp.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
namespace MVCSimpleApp.Controllers
{
public class EmployeeController : Controller
{
private EmployeeContext db = new EmployeeContext();
// GET: Employee
public ActionResult Index()
{
var employees = from e in db.Employee select e;
return View(employees);
}
}
}
而現在這是我的存儲過程。它並不多,只是爲了實踐目的。
Create Proc DisplayStudents
AS
BEGIN
/*selecting all records from the table whose name is "Employee"*/
Select * From Employee
END
這是我的看法:
@model IEnumerable<MVCSimpleApp.Models.Employee>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Student List</h2>
<p>
<a href="@Url.Action("Create")" title="Add new" class="btn btn-primary btn-lg">
<span class="glyphicon glyphicon-plus "></span>
Add Student
</a>
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.EmployeeId)
</th>
<th>
@Html.DisplayNameFor(model => model.FirstName)
</th>
<th>
@Html.DisplayNameFor(model => model.LastName)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(model => item.EmployeeId)
</td>
<td>
@Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
<span>
<a href="@Url.Action("Edit", new { id = item.EmployeeId})" title="Edit Record">
<span class="glyphicon glyphicon-pencil"></span>
</a>
</span>
|
<span>
<a href="@Url.Action("Details", new { id = item.EmployeeId})" title="View Details">
<span class="glyphicon glyphicon-th-list"></span>
</a>
</span>
|
<span>
<a href="@Url.Action("Delete", new { id = item.EmployeeId})" title="Delete">
<span class="glyphicon glyphicon-trash"></span>
</a>
</span>
</td>
</tr>
}
/*this is the button I want the stored procedure to be called on when I click it*/
<button>Run</button>
</table>
請傢伙,我需要在這個問題上你的意見和反饋。將接受將參數傳遞給存儲過程的提示。如果我沒有在這裏做事,請糾正我。感謝你的關心。
有噸的在互聯網上的例子 - 這個看起來很不錯 - http://csharp-station.com/Tutorial/AdoDotNet/Lesson07 – 2016-09-19 14:59:25
感謝的人,現在檢查出來。 –
你可以去這個,希望它會幫助你[答案](http:// stackoverflow。com/questions/34761857/mvc-select-from-stored-procedure-in-entity-framework/34762768#34762768) – Viplock