As Model is returning empty in the controller. How to load the model in controller?
DbContext
#region RolePermissionCheck
public List<RolePermission> GetRolepermission(int id)
{
connection();
List<SampleWebApplication.Models.RolePermission> rolepermissions = new List<SampleWebApplication.Models.RolePermission>();
SqlCommand cmd = new SqlCommand("GetRolePermission", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Roleid", id);
SqlDataAdapter sd = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
con.Open();
sd.Fill(dt);
con.Close();
foreach (DataRow dr in dt.Rows)
{
role.Add(new RolePermission
{
Roleperid = Convert.ToInt32(dr["Roleperid"]),
Roleid = Convert.ToInt32(dr["Roleid"]),
ControllerName = Convert.ToString(dr["ControlerName"]),
Roles = Convert.ToString(dr["Roles"]),
ViewDet = Convert.ToBoolean(dr["ViewDet"]),
AddDet = Convert.ToBoolean(dr["AddDet"]),
EditDet = Convert.ToBoolean(dr["EditDet"]),
DelDet = Convert.ToBoolean(dr["DelDet"])
});
}
return role;
}
Controller
public ActionResult Index(int id, RolePermission smodel)
{
try
{
DbContext sdb = new DbContext();
sdb.UpdateDetails(smodel, id);
DbContext dbhandle = new DbContext();
RolePermission role = new RolePermission();
//ModelState.Clear();
return View(dbhandle.GetRolepermission(id));
//return View();
// return RedirectToAction("Index");
}
catch(Exception ex)
{
return View();
}
}
public ActionResult Index()
{
int id;
id = (int)Session["Roleid"];
DbContext dbhandle = new DbContext();
return View(dbhandle.GetRolepermission(id));
}
public class RolePermission
{
//[Table("RolePermission")]
[Key]
public int Roleperid { get; set; }
public int Roleid { get; set; }
public string ControllerName { get; set; }
public string Roles { get; set; }
public bool ViewDet { get; set; }
public bool AddDet { get; set; }
public bool EditDet { get; set; }
public bool DelDet { get; set; }
public DbSet<RolePermission> RolePermissions
{
get;
set;
}
}
@model IEnumerable<SampleWebApplication.Models.RolePermission>
@using (Html.BeginForm())
{
<p>@Html.ActionLink("Create New", "Create")</p>
<table class="table">
<tr>
<th>@Html.DisplayNameFor(model => model.Roleid)</th>
<th>@Html.DisplayNameFor(model => model.ControllerName)</th>
<th>@Html.DisplayNameFor(model => model.Roles)</th>
<th>@Html.DisplayNameFor(model => model.ViewDet)</th>
<th>@Html.DisplayNameFor(model => model.AddDet)</th>
<th>@Html.DisplayNameFor(model => model.EditDet)</th>
<th>@Html.DisplayNameFor(model => model.DelDet)</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>@Html.DisplayFor(modelItem => item.Roleid)</td>
<td>@Html.DisplayFor(modelItem => item.ControllerName)</td>
<td><input type="checkbox" value="@item.ViewDet" checked="@item.ViewDet" name="EditDet" /></td>
<td><input type="checkbox" value="@item.AddDet" checked="@item.AddDet" name="EditDet" /></td>
<td><input type="checkbox" value="@item.EditDet" checked="@item.EditDet" name="EditDet" /></td>
<td><input type="checkbox" value="@item.DelDet" checked="@item.DelDet" name="DelDet" /></td>
<td>
@Html.ActionLink("Details", "Details", new { id = item.Roleperid }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Roleperid })*@
</td>
</tr>
<tr>
@Html.ActionLink("Save", "Save", "RolePermission", new { role = item.Roleid }, null)
</tr>
}
</table>
<p>
<input id="Submit" type="submit" value="Save" onclick="@Url.Action("Index", "RolePermission", new { id = Session["Roleid"] })" />leid"] })*@
</p>
}