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


 /// <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,有什么替代方案?

创建这样的类:

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
 ....
}

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

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

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循环中,只需通过属性对其进行寻址。。。

:(

相关内容

  • 没有找到相关文章

最新更新