在没有循环的情况下迭代 IEnumerable 模型 - ASP.NET-MVC5
Iterate through IEnumerable Model without loop - ASP.NET-MVC5
我有两条记录的强类型 IEnumerable,我正在传递这些记录以从控制器查看。我总是最多有两个记录。我需要以单独的形式打印这两条记录,即 HTML beginform。
我的问题是如何在不使用@for 或@foreach 循环的情况下打印 IEnumerable 模型数据???你能使用某种索引来读取模型中的对象及其基于索引的数据吗???
从记录中获取数据
public List<EmergencyContact> GetEmergencyContactByStudentID(int _studentID)
{
try
{
using (var _uow = new StudentProfile_UnitOfWork())
{
var _record = (from _emergencyContact in _uow.EmergencyContact_Repository.GetAll()
join _student in _uow.Student_Repository.GetAll() on _emergencyContact.StudentID equals _student.StudentID
where _emergencyContact.StudentID == _studentID
select _emergencyContact).ToList();
return _record;
}
}
catch { return null; }
}
控制器
public ActionResult EditEmergencyContact()
{
int _studentEntityID = 0;
_studentEntityID = _studentProfileServices.GetStudentIDByIdentityUserID(User.Identity.GetUserId());
List<EmergencyContact> _emergencyContactModel = new List<EmergencyContact>();
_emergencyContactModel = _studentProfileServices.GetEmergencyContactByStudentID(_stu
return PartialView("EditEmergencyContact_Partial", _emergencyContactModel);
}
查看
@model IEnumerable<App.DAL.Model.EmergencyContact>
.............//other code
@Html.EditorFor(modelItem => model.NameOfContact, new { htmlAttributes = new { @class = "form-control" } })
以上@html.editorFor行给出错误;参考截图如下
如果您使用List
:
,您可以通过它的索引访问它
@model List<App.DAL.Model.EmergencyContact>
.............//other code
@Html.EditorFor(modelItem => model[0].NameOfContact, new { htmlAttributes = new { @class = "form-control" } })
但是因为你现在是通过它的索引访问对象我总是建议先检查它是否是 null
,即使你说你总是有 2 个项目在集合中,它没有做任何伤害检查。
更新
您可以通过在 Razer 视图中执行以下操作来检查它是否为空:
@if(Model[0] != null)
{
@Html.EditorFor(modelItem => model[0].NameOfContact, new { htmlAttributes = new { @class = "form-control" } })
}
我有两条记录的强类型 IEnumerable,我正在传递这些记录以从控制器查看。我总是最多有两个记录。我需要以单独的形式打印这两条记录,即 HTML beginform。
我的问题是如何在不使用@for 或@foreach 循环的情况下打印 IEnumerable 模型数据???你能使用某种索引来读取模型中的对象及其基于索引的数据吗???
从记录中获取数据
public List<EmergencyContact> GetEmergencyContactByStudentID(int _studentID)
{
try
{
using (var _uow = new StudentProfile_UnitOfWork())
{
var _record = (from _emergencyContact in _uow.EmergencyContact_Repository.GetAll()
join _student in _uow.Student_Repository.GetAll() on _emergencyContact.StudentID equals _student.StudentID
where _emergencyContact.StudentID == _studentID
select _emergencyContact).ToList();
return _record;
}
}
catch { return null; }
}
控制器
public ActionResult EditEmergencyContact()
{
int _studentEntityID = 0;
_studentEntityID = _studentProfileServices.GetStudentIDByIdentityUserID(User.Identity.GetUserId());
List<EmergencyContact> _emergencyContactModel = new List<EmergencyContact>();
_emergencyContactModel = _studentProfileServices.GetEmergencyContactByStudentID(_stu
return PartialView("EditEmergencyContact_Partial", _emergencyContactModel);
}
查看
@model IEnumerable<App.DAL.Model.EmergencyContact>
.............//other code
@Html.EditorFor(modelItem => model.NameOfContact, new { htmlAttributes = new { @class = "form-control" } })
以上@html.editorFor行给出错误;参考截图如下
如果您使用List
:
@model List<App.DAL.Model.EmergencyContact>
.............//other code
@Html.EditorFor(modelItem => model[0].NameOfContact, new { htmlAttributes = new { @class = "form-control" } })
但是因为你现在是通过它的索引访问对象我总是建议先检查它是否是 null
,即使你说你总是有 2 个项目在集合中,它没有做任何伤害检查。
更新
您可以通过在 Razer 视图中执行以下操作来检查它是否为空:
@if(Model[0] != null)
{
@Html.EditorFor(modelItem => model[0].NameOfContact, new { htmlAttributes = new { @class = "form-control" } })
}