检索从控制器发送的视图中的数据

时间:2014-04-15 10:55:10

标签: c# linq asp.net-mvc-4

 /// <summary>  Model to fetch data
        /// 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/

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

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

//My View

@{
    ViewBag.Title = "DisplayCountries";
}

<h2>Country list</h2>

   @{
       var countries = ViewBag.countries;

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

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

2 个答案:

答案 0 :(得分:0)

制作一个这样的类:

public class CountryVm
{
public int ID {get;set;} 
public string Description {get;set;}
public string CountryPhoneCode {get;set;}
public double Currency {get;set;}
public string Language {get;set;}
}

和代码:

public List<CountryVm> GetAllCountry()
{
using (Context context = new Context())
            {
                var countries = (from c in context.COUNTRY
                                select new CountryVm
                        {
                         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()
                    }).ToList<CountryVm>();
                }

                return countries;
}

在视图中:

@{

var list = ViewBag.countries as List<CountryVm>;

foreach(var item in list)
{
 item.ID
 ....

}

答案 1 :(得分:0)

您应该切换到强类型视图模型 - 即视图模型类

创建应包含这些属性的视图模型

public class Country ... etc..

     new Country() {                  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()
                    }

在您的控制器中,返回包含所有国家/地区的此类列表,例如

List<Country> country = library.GetAllCountry();

在您的视图中,添加此处添加页面顶部

@model IEnumerable<XNet.Repository.Model.EditRoomTypeViewModel>

在for循环中只需通过属性来解决它......

:)