
یادگیری سی شارپ از مفاهیم پایه تا پروژه محور: شیگرایی، کار با SQL و LINQ، ORMها (Entity Framework)، ساخت پروژه مدیریت رستوران با گزارشات حرفهای و امکانات کامل!
مشاهده بیشتر
یادگیری MVC Core از مبانی تا پیشرفته: شیگرایی، Routing، Entity Framework، امنیت، تست یونیت، Razor، Ajax، و پروژههای کاربردی! یک دوره کامل برای تسلط بر توسعه وب با ASP.NET Core. به صورت حضوری و آنلاین!
مشاهده بیشترمشخصات مقاله
آموزش ادغام Model، View و کنترلر در ASP.Net MVC
آموزش ادغام (Model)، نما (View) و کنترلر (Controller) در ASP.Net MVC :
در بخش های قبل، کنترلر StudentController و مدل (Model) و نما (View) متناظر با آن را ایجاد کردیم. اما در عمل آن ها را با هم ادغام نکرده ایم تا خروجی نهایی را اجرا کنیم.
کدهای زیر، نشان می دهند که کنترلر، مدل و نما را چگونه طراحی کرده ایم :
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MVC_BasicTutorials.Controllers { public class StudentController : Controller { // GET: Student public ActionResult Index() { return View(); } } } </pre> <pre id="codes" class="brush: c#;"> namespace MVC_BasicTutorials.Models { public class Student { public int StudentId { get; set; } public string StudentName { get; set; } public int Age { get; set; } } } </pre> <pre id="codes" class="brush: c#;"> @model IEnumerable<mvc_basictutorials.models.student> @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } <h2>Index</h2> <p> @Html.ActionLink("Create New", "Create") </p> <table class="table"> <tr> <th> @Html.DisplayNameFor(model => model.StudentName) </th> <th> @Html.DisplayNameFor(model => model.Age) </th> <th></th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.StudentName) </td> <td> @Html.DisplayFor(modelItem => item.Age) </td> <td> @Html.ActionLink("Edit", "Edit", new { id=item.StudentId }) | @Html.ActionLink("Details", "Details", new { id=item.StudentId }) | @Html.ActionLink("Delete", "Delete", new { id = item.StudentId }) </td> </tr> } </table>
اکنون، برای اجرای موفق پروژه، بایستی یک شی Model object را از کنترلر به View ارسال کنیم. همانطور که در کد فایل Index.cshtml مشاهده می کنید، View از یک عنصر شمارشی (IE numerable Object) به عنوان شی Model استفاده می کند. بنابراین بایستی یک شی قابل شمارش Student را از student Model به متد Index action کنترلر StudentController، همانند کد زیر ارسال کنیم :
public class StudentController : Controller { // GET: Student public ActionResult Index() { var studentList = new List<student>{ new Student() { StudentId = 1, StudentName = "John", Age = 18 } , new Student() { StudentId = 2, StudentName = "Steve", Age = 21 } , new Student() { StudentId = 3, StudentName = "Bill", Age = 25 } , new Student() { StudentId = 4, StudentName = "Ram" , Age = 20 } , new Student() { StudentId = 5, StudentName = "Ron" , Age = 31 } , new Student() { StudentId = 4, StudentName = "Chris" , Age = 17 } , new Student() { StudentId = 4, StudentName = "Rob" , Age = 19 } }; // Get the students from the database in the real application return View(studentList); } }
همانطور که در کد مثال فوق مشاهده می کنید، ابتدا یک لیست از اشیای student object را برای نمونه ایجاد کرده ایم (در یک برنامه واقعی می توانید این اطلاعات را از پایگاه داده، دریافت کنید). سپس این شی List را به عنوان یک پارامتر به متد View ارسال کرده ایم. متد View() در کلاس پایه و مادر Controller Class تعریف شده و به صورت اتوماتیک شی Model را به View متصل می کند.
اکنون می توانید با زدن دکمه F5 پروژه MVC را اجرا کرده و به آدرس http://localhost/student بروید. برنامه خروجی زیر را تولید خواهد کرد :

در درس بعدی، به آموزش کد نویسی با ساختار Razor خواهیم پرداخت.