I have the following view below when is recieving a Model which is not null because the table is being populated with data, then when I click on the submit button to trigger the Controller method below, the model being passed the controller method is being passed as null.
What am I doing wrong? I have been trying to solve this for hours, why exactly is this happening and why is the model not being passed to the controller method since I am 100% sure that the Model is not null?
Controller Method:
[HttpPost]
public ActionResult AssignRole(PageDetailsModel model)
{
//controller method here which is not being executed since model being passed is null
}
View:
@model OnlineLearningLTD.Models.PageDetailsModel
@{
ViewBag.Title = "Page Details";
}
<h2>Page Details</h2>
<table class="table table-hover">
@using (Html.BeginForm("AssignRole", "Page", FormMethod.Post))
{
if (Model.CurrPage != null)
{
<tr>
<td>Name</td><td>@Model.CurrPage.Name</td>
</tr>
<tr>
<td>Url</td><td>@Model.CurrPage.Url</td>
</tr>
<tr>
<td>Assigned Roles:</td>
@foreach(var role in @Model.CurrPage.Role)
{
<td>@role.Name</td>
}
</tr>
<tr>
<td colspan="2"><h3>Assign Role</h3></td>
</tr>
<tr>
<td>Choose Role</td>
<td>
@Html.DropDownListFor(model => model.RoleId, Model.RoleList)
</td>
</tr>
<tr>
<td colspan="2" align="right">
@if(Model.RoleList.Count() > 0)
{
<input type="submit" value="Assign Role" id = "btnAssignRole"/>
}
else
{
<label>There are no roles to be assigned</label>
}
</td>
</tr>
}
}
Model:
public class PageDetailsModel
{
public SelectList RoleList { get; set; }
public int RoleId { get; set; }
public SelectList AssignedRoleList { get; set; }
public int DeassignRoleID { get; set; }
public CommonLayer.Page CurrPage { get; set; }
public string PageID { get; set; }
public PageDetailsModel() { }
public PageDetailsModel(string pageID)
: this()
{
this.PageID = pageID;
CurrPage = new BusinessLayer.Pages().getPageByID(pageID);
BusinessLayer.Roles role = new BusinessLayer.Roles();
AssignedRoleList = new SelectList(role.getAllPageRoleWithPageID(pageID), "Id", "Name");
RoleList = new SelectList(role.getAllPageRolesByPageId(pageID), "Id", "Name");
}
}