如何显示从控制器发送的数据到视图? [英] How to display data sent from controller to view ?

查看:107
本文介绍了如何显示从控制器发送的数据到视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  ///  获取所有国家/地区
< span class =code-summarycomment> /// < / summary >
/// < span class =code-comment> < 返回 > < / returns >
public IQueryable GetAllCountry()
{
使用(Context context = new Context())
{
var count ries = context.COUNTRY.Select(c = > new
{
country = new
{
ID = c.ID,
说明= c.DESCRIPTION,
CountryPhoneCode = c。 COUNTRY_PHONE_CODE,
货币= c.CURRENCY.CURRENCY,
语言= context.LANGUAGE.Where(l = > l.COUNTRY_ID == c .ID)。选择(l = > new {lang = l.DESCRIPTION})。Distinct( )
}
});

返回个国家/地区;
}

}





 < span class =code-comment> //   GET:/ Country / My controller  

public ActionResult DisplayCountries()
{
DAL library = new DAL();

var country = library.GetAllCountry();
ViewBag.countries = country;
return 查看( DisplayCountries);
}

// 我的观点

@ {
ViewBag.Title = DisplayCountries;
}

国家/地区列表

@ {
var countries = ViewBag.countries;

foreach var country 国家/地区)
{
@ country.country.ID; // 未定义属性国家/地区。错误!
}
}





问题:

如何显示从控制器发送到View的数据。我是否需要创建一个单独的类来显示数据?没有别的方法可以做到这一点吗?如果我不想使用ViewBag,有什么选择?

解决方案

viewbag的替代方法是将countries对象作为模型发送,以查看和添加strogly类型的视图。 />
,代码将是,



  //  控制器 
public ActionResult DisplayCountries()
{
DAL library = new DAL();

var country = library.GetAllCountry();
return 查看( DisplayCountries,国家);
}

// 查看
@model Demo.Counties

< ul>
@foreach( var country in Model.Countries)
{
< li> country.Description < / li >
}
< / ul >


/// Get All Countries
     /// </summary>
     /// <returns></returns>
     public IQueryable GetAllCountry()
     {
         using (Context context = new Context())
         {
             var countries = context.COUNTRY.Select(c => new
             {
                 country = new
                 {
                     ID = c.ID,
                     Description = c.DESCRIPTION,
                     CountryPhoneCode = c.COUNTRY_PHONE_CODE,
                     Currency = c.CURRENCY.CURRENCY,
                     Language = context.LANGUAGE.Where(l => l.COUNTRY_ID == c.ID).Select( l => new { lang = l.DESCRIPTION }).Distinct()
                 }
             });

             return countries;
         }

     }



        // GET: /Country/ My controller 

        public ActionResult DisplayCountries()
        {
            DAL library = new DAL();

            var country = library.GetAllCountry();
            ViewBag.countries = country;
            return View("DisplayCountries");
        }

//My View

@{
    ViewBag.Title = "DisplayCountries";
}

Country list

   @{
       var countries = ViewBag.countries;

        foreach (var country in countries)
        {
            @country.country.ID; // property country is not defined. error!
        }
   }



Problem:
How do I display the data sent from controller to View. Do I need to create a separate class to display the data? Is there no other way to do this ? What are the alternatives if I dont want to use ViewBag ?

解决方案

Alternative for viewbag is send the countries object as a model to view and add strogly typed view.
and the code will be ,

//Controller
 public ActionResult DisplayCountries()
        {
            DAL library = new DAL();
 
            var country = library.GetAllCountry();           
            return View("DisplayCountries",country);
        }

//View
@model Demo.Counties

<ul>
@foreach(var country in Model.Countries)
{
 <li>country.Description</li>
}
</ul>


这篇关于如何显示从控制器发送的数据到视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆