نحوه فرستادن چندین مدل به ویو ها در Asp.Net MVC

یکشنبه 6 اردیبهشت 1394

در این مقاله میخواهیم نحوه فرستادن چندین مدل به ویو ها در Asp.Net MVC را شرح دهیم

در این مقاله میخواهیم نحوه فرستادن چندین مدل به ویو ها در Asp.Net MVC را شرح دهیم چندین روش برای این امر و جود دارد :

1-استفاده از ویومدل

2-استفاده از ویو دیتا

3-استفاده از ویوبگ

4-استفاده از مدل پویا

 

یک پروژه جدید از نوع Mvc بسازید سپس یک کنترلر به نام MultipleModelDemo بسازید در آن برای هر روش یک اکشن متد مینویسیم :

using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PassingMultipleModels.Models;

namespace PassingMultipleModels.Controllers
{
    public class MultiplemodelDemoController : Controller
    {
        public ActionResult UsingViewModel()
        {
            ViewModels vmodel = new ViewModels();
            vmodel.Employees = Employee.GetEmployee();
            vmodel.Departments = Department.GetDepartment();
            return View(vmodel);
        }
        public ActionResult UsingViewData()
        {
            ViewData["Employees"] = Employee.GetEmployee();
            ViewData["Departments"] = Department.GetDepartment();
            return View();
        }
        public ActionResult UsingViewBag()
        {
            ViewBag.Employees = Employee.GetEmployee();
            ViewBag.Department = Department.GetDepartment();
            return View();
        }

        public ActionResult UsingDynamicModel()
        {
            //ExpandoObject class, in the System.Dynamic namespace. What makes this special is,  
            //it allows you to create an object with members that can be dynamically added and removed at run time.  
            dynamic mymodel = new ExpandoObject();
            mymodel.Employee = Employee.GetEmployee();
            mymodel.Department = Department.GetDepartment();
            return View(mymodel);
        }  
    }
}

سپس روی پوشه مدل خود کلیک راست کرده و یک کلاس به نام Department بسازید:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace PassingMultipleModels.Models
{
    public class Department
    {
        
        [Key]
        public int DeptId { get; set; }
        public string DeptName
        {
            get;
            set;
        }
        public static List<Department> GetDepartment()
        {
            PassingMultipleModelsContext db=new PassingMultipleModelsContext();
            List<Department> dept = new List<Department>();

            foreach (var item in db.Departments)
            {
                dept.Add(new Department
                {
                    DeptId = item.DeptId,
                    DeptName = item.DeptName
                });
            }
            return dept;
        }  
    }
}

مجددا کلاس دیگری در پوشه مدل خود به نام Employee  بسازید:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace PassingMultipleModels.Models
{
    public class Employee
    {
        [Key]
        public int EmpId
        { get; set; }
        public string EmpName
        { get; set; }
        public static List<Employee> GetEmployee()
        {
            
            PassingMultipleModelsContext db = new PassingMultipleModelsContext();
            List<Employee> emp = new List<Employee>();

            foreach (var item in db.Departments)
            {
                emp.Add(new Employee
                {
                    EmpId = item.DeptId,
                    EmpName = item.DeptName
                });
            }
            return emp;
        } 
    }
}

حال برای انجام روش اول نیاز به ساخت یک ویو مدل در پوشه مدل داریم پس کلاس دیگری به نام ViewModel به شکل زیر میسازیم:

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
namespace Mvc_Security.Models  
{  
    public class ViewModel  
    {  
        public IEnumerable < Employee > Employees {  
            get;  
            set;  
        }  
        public IEnumerable < Department > Departments {  
            get;  
            set;  
        }  
    }  
} 

حالا ویو های متد های نوشته شده در کنترلر را به ترتیب میسازیم:

 Using Dynamic Model.cshtml :

@using PassingMultipleModels.Models
@model dynamic
@{
    ViewBag.Title = "استفاده از مدل پویا";
}
<h2>استفاده از مدل پویا</h2>
<p>
    <b>لیست کازمندان</b>
</p>
<table>
    <tr>
        <th>ردیف</th>
        <th>نام</th>
    </tr>
    @foreach (Employee emp in Model.Employee)
    {
        <tr>
            <td>@emp.EmpId</td>
            <td>@emp.EmpName</td>
        </tr>
    }
</table>
<p>
    <b>لیست بخش ها</b>
</p>
<table>
    <tr>
        <th>ردیف</th>
        <th>نام</th>
    </tr>
    @foreach (Department dept in Model.Department)
    {
        <tr>
            <td>@dept.DeptId</td>
            <td>@dept.DeptName</td>
        </tr>
    }
</table>
  
UsingViewBag.cshtml :


@using PassingMultipleModels.Models
@{
    ViewBag.Title = "استفاده از ویو بگ";
}
<h2>استفاده از ویو بگ</h2>
<p>
    <b>لیست کارمندان</b>
</p>
<table>
    <tr>
        <th>ردیف</th>
        <th>نام</th>
    </tr>
    @foreach (Employee emp in ViewBag.Employees)
    {
        <tr>
            <td>@emp.EmpId</td>
            <td>@emp.EmpName</td>
        </tr>
    }
</table>
<p>
    <b>لیست بخش ها</b>
</p>
<table>
    <tr>
        <th>ردیف</th>
        <th>نام</th>
    </tr>
    @foreach (Department dept in ViewBag.Department)
    {
        <tr>
            <td>@dept.DeptId</td>
            <td>@dept.DeptName</td>
        </tr>
    }
</table>

UsingViewData.cshtml :


@using PassingMultipleModels.Models
@{
    ViewBag.Title = "استفاده از ویو دیتا";
}
<h2>استفاده از ویو دیتا</h2>
@{
    IEnumerable
    <Employee> employees = ViewData["Employees"] as IEnumerable
        <Employee>;
    IEnumerable
            <Department> departments = ViewData["Departments"] as IEnumerable
                <Department>;
}
<p>
    <b>لیست کارمندان</b>
</p>
<table>
    <tr>
        <th>ردیف</th>
        <th>نام</th>
    </tr>
    @foreach (Employee emp in employees)
    {
        <tr>
            <td>@emp.EmpId</td>
            <td>@emp.EmpName</td>
        </tr>
    }
</table>
<p>
    <b>لیست یخش ها</b>
</p>
<table>
    <tr>
        <th>ردیف</th>
        <th>نام بخش</th>
    </tr>
    @foreach (Department dept in departments)
    {
        <tr>
            <td>@dept.DeptId</td>
            <td>@dept.DeptName</td>
        </tr>
    }
</table>
  
UsingViewmodel.cshtml :

@using PassingMultipleModels.Models;
@using PassingMultipleModels.Models
@model ViewModels
@{
    ViewBag.Title = "استفاده از ویو مدل";
}
<h2>استفاده از ویو مدل</h2>
<p>
    <b>لیست کارمندان</b>
</p>
<table>
    <tr>
        <th style="padding-left: 7px;">ردیف</th>
        <th>نام کارمند</th>
    </tr>
    @foreach (Employee emp in Model.Employees)
    {
        <tr>
            <td>@emp.EmpId</td>
            <td>@emp.EmpName</td>
        </tr>
    }
</table>
<p>
    <b>لیست سازمانی</b>
</p>
<table>
    <tr>
        <th>ردیف</th>
        <th>نام بخش</th>
    </tr>
    @foreach (Department dept in Model.Departments)
    {
        <tr>
            <td>@dept.DeptId</td>
            <td>@dept.DeptName</td>
        </tr>
    }
</table>


حال میتوانید برنامه رو اجرا کنید و انواع روش ها رو تست کنید.

 

فایل های ضمیمه

برنامه نویسان

نویسنده 3355 مقاله در برنامه نویسان

کاربرانی که از نویسنده این مقاله تشکر کرده اند

در صورتی که در رابطه با این مقاله سوالی دارید، در تاپیک های انجمن مطرح کنید