C# 检索从控制器发送的视图中的数据
//我的控制器C# 检索从控制器发送的视图中的数据,c#,linq,asp.net-mvc-4,C#,Linq,Asp.net Mvc 4,//我的控制器 /// <summary> Model to fetch data /// Get All Countries /// </summary> /// <returns></returns> public IQueryable GetAllCountry() { using (Context context = new Con
/// <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;
}
}
@{
var list = ViewBag.countries as List<CountryVm>;
foreach(var item in list)
{
item.ID
....
}
如何显示从控制器发送到视图的数据。我是否需要创建一个单独的类来显示数据?没有别的办法吗?如果我不想使用ViewBag,有什么替代方案?制作一个如下的类:
//
// 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!
}
}
@{
var list = ViewBag.countries as List<CountryVm>;
foreach(var item in list)
{
item.ID
....
}
和代码:
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;}
}
@{
var list = ViewBag.countries as List<CountryVm>;
foreach(var item in list)
{
item.ID
....
}
鉴于:
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
....
}
您应该切换到强类型视图模型,即视图模型类
@{
var list = ViewBag.countries as List<CountryVm>;
foreach(var item in list)
{
item.ID
....
}
创建应包含这些属性的视图模型
@{
var list = ViewBag.countries as List<CountryVm>;
foreach(var item in list)
{
item.ID
....
}
在控制器中,返回包含所有国家/地区的此类列表,如
@{
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()
}
在您的视图中,将此添加到页面顶部
@{
var list = ViewBag.countries as List<CountryVm>;
foreach(var item in list)
{
item.ID
....
}
List<Country> country = library.GetAllCountry();
在for循环中,只需通过属性
@{
var list = ViewBag.countries as List<CountryVm>;
foreach(var item in list)
{
item.ID
....
}
:您为什么不创建一个包含您所在国家实体的模型?我认为最简单的方法是创建一个模型。有太多类似的linq查询需要执行。如果不创建新类,我就不能这样做。我正在使用EFL。您通过调试检查了它吗?我能够将值获取到视图中。我只是不知道如何显示它。我的错误是:“/”应用程序中的服务器错误对象“”不包含“国家/地区”说明的定义:在执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源。异常详细信息:Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:“对象”不包含“国家”的定义源错误:第12行:国家/地区中的foreach var国家/地区第13行:{14行:@country.countries.ID;第15行:}第16行:}强制转换无效。请再看一遍。谢谢你的回复。我有疑问,请帮我澄清一下。我正在使用EF。因此EF已经创建了所有模型类。那么我还需要创建这样的类吗??这是强制性的吗。我不能直接将值传递给视图并显示它吗?您可以使用country类然后,我传递CountryVM的地方您可以传递country类它在您的模型中扫描您告诉我访问视图中国家的代码吗?
@{
var list = ViewBag.countries as List<CountryVm>;
foreach(var item in list)
{
item.ID
....
}