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

برنامه نویسی وب و اندروید ، برنامه نویسی با C# & JAVA، کار با پایگاه داده SQL Server

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

برنامه نویسی وب و اندروید ، برنامه نویسی با C# & JAVA، کار با پایگاه داده SQL Server

روش انتقال داده بین View و Controller

روش انتقال داده بین View  و Controller


 

استفاده از ViewData دادن متغییر رشته :


        public ActionResult Index01()

        {

            ViewData["hh"] = " Hello Godarzi";

            return View();

        }

برداشت متغییر رشته :

<body class="container">

    <div>@ViewData["hh"]</div>

یا

    <div>@ViewBag.hh</div>

</body>

 

ارسال با ViewBag


namespace MVCView.Models

{

    public class Person

    {

        public int ID { get; set; }

        public String FullName { get; set; }

    }

}

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++

        public ActionResult Index02()

        {

            Person mPerson = new Person()

            {

                ID = 12,

                FullName ="Salman"

            };

            ViewBag.myPeron = mPerson;

            return View();

        }

دریافت :

<h1>@ViewBag.myPeron.ID</h1>

 

<h1>@ViewBag.myPeron.FullName</h1>

 

روش دوم دریافت شی :

 

@{

    MVCView.Models.Person Mpeson = ViewBag.myPeron as MVCView.Models.Person;

    <h1>-------------------------</h1>

    <h3>@Mpeson.ID</h3>

    <h3>@Mpeson.FullName</h3>

}


 

روش سوم ارسال با Model


 

public ActionResult Index03()

        {

            Person mPerson = new Person()

            {

                ID = 12,

                FullName = "Salman"

            };

            return View(mPerson);

        }

دریافت :

@model MVCView.Models.Person

@{

    ViewBag.Title = "Index03";

}

 

<h2>Index03</h2>

<h1>-------------------------</h1>

 

<h3>ID :  @Model.ID </h3>

<h3>FullName : @Model.FullName</h3>

 

روش چهارم استفاده از ViewModels :  برا ی این کار ابتدا یک پوشه (فضا نام) به نام ViewModels در پروژه ایجاد کرده و کلیه کلاس ها یا متغغیرهایی که نیاز است در View استفاده شود به صورت یک کلاس در آن تعریف میکنیم و آن را برای View  ارسال می کنیم :

 

کلاس مدل برای View

namespace MVCView.ViewMidels

{

    public class Factory

    {

        public String FactoryName { get; set; }

        public Person Person { get; set; }

    }

}

 

 تعریف اکشن :

        public ActionResult Index04()

        {

          

            Person mPerson = new Person()

            {

                ID = 12,

                FullName = "Salman"

            };

 

            Factory factory = new Factory()

            {

                FactoryName ="Kia Sport",

                Person = mPerson

            };

 

            return View(factory);

        }

تعریف view

 

@model MVCView.ViewMidels.Factory

@{

    ViewBag.Title = "Index04";

}

 

<h2>Index04</h2>

 

<h1>-------------------------</h1>

<h3>Factory :  @Model.FactoryName </h3>

<h1>-------------------------</h1>

 

<h3>ID :  @Model.Person.ID </h3>

<h3>FullName : @Model.Person.FullName</h3>


ارسال آرایه :


 

        public ActionResult Index05()

        {

            List<Person> Lperon = new List<Person>()

            {

                new Person(){ID=1,FullName="Ali"},

                new Person(){ID=2,FullName="Hassan"},

                new Person(){ID=3,FullName="Salman"},

                new Person(){ID=4,FullName="Sepehr"}

            };

            return View(Lperon);

        }

دریافت در View

 

@model List<MVCView.Models.Person>

@{

    ViewBag.Title = "Index05";

}

 

<h2>Index05</h2>

 

@{

    if(Model.Count!=0)

    {

        foreach(var pp in Model)

        {

            <h5>ID:  @pp.ID</h5>

            <h5>Name:  @pp.FullName</h5>

            <hr/>

        }

    }

 

}

نظرات 0 + ارسال نظر
برای نمایش آواتار خود در این وبلاگ در سایت Gravatar.com ثبت نام کنید. (راهنما)
ایمیل شما بعد از ثبت نمایش داده نخواهد شد