javascript - Iterate through IEnumerable Model without loop - ASP.NET-MVC5 -
i have typed ienumerable of 2 records passing view controller. have maximum 2 records. need print these 2 record in separate form i.e. html beginform.
my question how can print ienumerable model data without using @for or @foreach loop??? can use kind of index read object in model , data based on index???
get data record
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 _emergencycontact.studentid == _studentid select _emergencycontact).tolist(); return _record; } } catch { return null; } }
controller
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); }
view
@model ienumerable<app.dal.model.emergencycontact> .............//other code @html.editorfor(modelitem => model.nameofcontact, new { htmlattributes = new { @class = "form-control" } })
above line @html.editorfor give error; refer screenshot below
you can access it's index if use list
:
@model list<app.dal.model.emergencycontact> .............//other code @html.editorfor(modelitem => model[0].nameofcontact, new { htmlattributes = new { @class = "form-control" } })
but since accessing object it's index recommend checking see if it's null
first, know have 2 items in collection, doesn't harm checking.
update
you can check if null doing following in razer view:
@if(model[0] != null) { @html.editorfor(modelitem => model[0].nameofcontact, new { htmlattributes = new { @class = "form-control" } }) }
Comments
Post a Comment