我正在开始使用 MVC,并且我有以下模型
public class FormControls
{
//Properties of the FormControls object
public string formCName { get; set; }
public string formCType { get; set; }
public string formCCss { get; set; }
public string formCEnabled { get; set; }
public string formCDefaultVal { get; set; }
}
我还创建了以下控件,并使用 linq 查询数据库以选择记录。然后,每条记录都必须添加到列表中。
public ActionResult Index()
{
var DataContext = new EditProfileFormFieldsDataContext();
var controls = from c in DataContext.Controls
select c;
List<FormControls> Fields = new List<FormControls>();
foreach(var fc in controls)
{
//Create Object for Generic List
FormControls epc = new FormControls();
epc.formCName = fc.Control_Name;
epc.formCType = fc.Control_Type;
epc.formCCss = fc.Control_CSS;
epc.formCEnabled = fc.Control_Enabled;
epc.formCDefaultVal = fc.Control_DefaultVal;
//Add Object to FormControls Generic List
Fields.Add(epc);
}
return View("EditProfile");
}
我的问题是如何在 View 中使用 RAZOR 访问列表?我正在尝试循环浏览我在 View 中创建的列表。我对 MVC 相当陌生,我想我对此想得太多了:) 谢谢!
最佳答案
您可以将 View 模型设为列表。将其放在 View 的顶部:
@model List<FormControls>
更改 Index()
方法的返回值:
return View("EditProfile", Fields);
然后您可以使用@Model从 View 中访问它。例如,要迭代它:
@foreach (var field in Model)
{
<p>@field.formCName</p>
}
关于c# - 在 ASP.NET MVC 5 View 中访问泛型列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29174193/