Reputation: 51
The view is not able to get my GridData, not sure why.
The code pass the GridData in the view, but the view page is not able to access GridData object.
UserMaster Models:
namespace Project.Models
{
public class UserMaster
{
[Key]
public int UserId { get; set; }
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required]
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Required]
}
public class UserMasterList
{
public List<UserMaster> GridData { get; set; }
}
}
Control:
UserMasterList userMasterList = new UserMasterList();
List<UserMaster> gl = new List<UserMaster>();
var userMasterListResult = _context.UserMaster.FromSql("EXECUTE [dbo].[UserMaster_Get] {0}", 0).ToList();
foreach (var data in userMasterListResult)
{
gl.Add(data);
}
userMasterList.GridData = gl;
return View(userMasterList);
View:
@model Project.Models.UserMaster
@foreach (var data in @Model.GridData) {
<tr>
<td>
@Html.DisplayFor(modelItem => data.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => data.LastName)
</td>
</tr>
}
Error: The code pass the GridData in the view, but the view page is not able to access GridData object.
Upvotes: 0
Views: 954
Reputation: 1673
In your controller you are passing an object with type of UserMasterList
, but in your view, model is type of UserMaster
so that you should update used model
in view as like as below:
@model Project.Models.UserMasterList
Upvotes: 0