I am really not getting why I keep getting an Http Error 500 whenever my RolesController renders logic to its respective View. This is for an MVC5 Application.
Please find the below:
RolesController.cs
[Route("/Roles")]
public ActionResult Index()
{
if (TempData["StatusMessage"] != null)
{
ViewBag.StatusMessage = TempData["StatusMessage"].ToString();
}
else
{
ViewBag.StatusMessage = "";
}
var roles = db.Roles.ToList();
return View("Index", roles);
}
// GET: /Roles/Create
public ActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create([Bind(Include = "Name,Description")] ApplicationRole model)
{
try
{
if (ModelState.IsValid)
{
var role = new ApplicationRole()
{
Name = model.Name,
Description = model.Description
};
var roleManager = new RoleManager<ApplicationRole>(new RoleStore<ApplicationRole>(db));
var result = await roleManager.CreateAsync(role);
if (result.Succeeded)
{
return RedirectToAction("Index", "Roles");
}
else
{
AddErrors(result);
}
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
// If we got this far, something failed, redisplay form
return View(model);
}
View:
@model User_Manager_Interface.Models.ApplicationRole
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="contenttitle">
<h2 class="form"><span>Create A New Role</span></h2>
</div>
@using (Html.BeginForm("Create", "Roles", FormMethod.Post, new { @class = "stdform", role = "form" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary()
<p>
<label>@Html.LabelFor(model => model.Name)</label>
<span class="field">
<input type="text" name="name" id="name" class="smallinput" />
</span>
@Html.ValidationMessageFor(model => model.Name)
<small class="desc">Name of the role.</small>
</p>
<p>
<label>@Html.LabelFor(model => model.Description)</label>
<span class="field">
<input type="text" name="description" id="description" class="longinput" />
</span>
@Html.ValidationMessageFor(model => model.Description)
<small class="desc">A short description for the role.</small>
</p>
<br clear="all" /><br />
<p class="stdformbutton">
<button class="submit radius2">Create</button>
<input type="reset" class="reset radius2" value="Reset Form" />
</p>
}
@section Scripts {
@Scripts.Render("~/bundles/forms")
}
Model:
public class ApplicationRole : IdentityRole
{
[Display(Name = "Description")]
[StringLength(100, MinimumLength = 5)]
public string Description { get; set; }
}
Is it a matter of the compiler not knowing what View to render at runtime? What could I be doing wrong?
UPDATE:
Here is my response header:
HTTP/1.1 500 Internal Server Error
Cache-Control: private
Transfer-Encoding: chunked
Content-Type: text/html; charset=utf-8
Server: Microsoft-IIS/10.0
X-AspNetMvc-Version: 5.2
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcTGluZGFcVXNlck1hbmFnZXJcRlNLVXNlck1hbmFnZXJcRlNLX1VzZXJNYW5hZ2VyX1dlYlxSb2xlc1xDcmVhdGU=?=
X-Powered-By: ASP.NET
Date: Mon, 05 Jun 2017 14:12:10 GMT