hi guys,
i am new to asp.net mvc, and im coming across this issue and its been days. i have been asked chat GPT to help but, AI cant seem to solve everything.
System.InvalidOperationException: There is no ViewData item of type IEnumerable<SelectListItem> that has the key DisciplineId.
the system i want right now is like this where, admin is able to edit the discipline in a seperate page. and when they do they, it affects the database.
likewise, when a user wants to login they can see the discipline table, (which was edited by the admin)
they can pick a discipline, and it will use that to populate a userRole table where there will be userID, roleID, and disicplineID to ensure users have right role,and discpline. that table then is linking to other tables.
public class HomeController : Controller
{
private readonly EREVIEW_SM_FEEDEntities1 dbContext = new EREVIEW_SM_FEEDEntities1();
public ActionResult Index()
{
//var disciplines = dbContext.disciplines.ToList();
////var disciplines = dbContext.disciplines.ToList(); // Assuming 'discipline' is the DbSet name for the discipline table.
//string selectedName = disciplines[0].disciplineName.ToString();
//ViewBag.Disciplines = new SelectList(disciplines, "disciplineName", "disciplineName", selectedName);
//return View();
ViewBag.DisciplineList = new SelectList(dbContext.disciplines, "DisciplineID", "DisciplineName");
return View();
}
public ActionResult LandingPage()
{
var comments = dbContext.GetAllComments().ToList();
return View(comments);
}
public ActionResult Search()
{
// Your logic for the landing page goes here
return View();
}
public ActionResult Activity()
{
// Your logic for the landing page goes here
return View();
}
[HttpPost]
public ActionResult Register(string FirstName, string LastName, string email, string password, int DisciplineId)
{
// Generate salt and hash password
HashedPassword hpob = new HashedPassword();
string hashedPassword = hpob.getHashed(password);
string salt = hpob.getSalt();
try
{
// Fetch the discipline name based on DisciplineId
string disciplineName = dbContext.disciplines
.Where(d => d.disciplineID == DisciplineId)
.Select(d => d.disciplineName)
.FirstOrDefault();
if (string.IsNullOrEmpty(disciplineName))
{
ViewBag.ErrorMessage = "Invalid Discipline selected.";
return View("Index");
}
// Call the stored procedure with the discipline name
dbContext.RegisterUser2(FirstName, LastName, email, hashedPassword, salt, disciplineName);
ViewBag.SuccessMessage = "Registration successful!";
}
catch (Exception ex)
{
string errorMessage = ex.Message;
if (ex.InnerException != null)
errorMessage += " | Inner Exception: " + ex.InnerException.Message;
ViewBag.ErrorMessage = errorMessage;
}
return View("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
dbContext.Dispose();
}
base.Dispose(disposing);
}
}