Hello, I have two ViewBags that are storing a list each, I load them into a form to make a select box, next to them has a Remove button. So far so good, but when I click the first ViewBag button, a reference loss error occurs, the second ViewBag is working 100% perfect.
View:
<label>Especializações:</label>
@{
if (((List<ProfessionalSpecialization>)ViewBag.ListProfessionalSpecialization).Count > 0)
{
using (Html.BeginForm("RemoveProfessionalUserSpecializations", "ProfessionalUser", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="col-lg-6" style="display:inline">
<nav class="mainmenu" style="width: 246px; margin: auto; position: relative; max-width: 100%;">
<select id="specialization" class="form-control" name="specialization">
@foreach (ProfessionalSpecialization item in (List<ProfessionalSpecialization>)ViewBag.ListProfessionalSpecialization)
{
<option>@item.Title</option>
}
</select>
<button type="submit" class="btn btn-primary">Remover</button>
</nav>
</div>
}
}
else
{
<label>Você não possui especializações cadastradas.</label>
}
}
<label>Áreas:</label>
@{
if (((List<ProfessionalType>)ViewBag.ListProfessionalType).Count > 0)
{
using (Html.BeginForm("RemoveProfessionalUserTypes", "ProfessionalUser", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="col-lg-6" style="display:inline">
<nav class="mainmenu" style="width: 246px; margin: auto; position: relative; max-width: 100%;">
<select id="type" class="form-control" name="type">
@foreach (ProfessionalType item in (List<ProfessionalType>)ViewBag.ListProfessionalType)
{
<option>@item.Title</option>
}
</select>
<button type="submit" class="btn btn-primary">Remover</button>
</nav>
</div>
}
}
else
{
<label>Você não possui áreas cadastradas.</label>
}
}
What happens is that if you clicked on the submit of the second form, it follows okay, if clicked on the first, the following error occurs:
Object reference not set to an instance of an object. if (((List) ViewBag.ListProfessionalSpecialization) .Count> 0)
Controller:
public ActionResult Edit(int id)
{
ProfessionalUser pUser = new ProfessionalUser();
using (ProfessionalUserDAO pUserDAO = new ProfessionalUserDAO())
{
pUser = pUserDAO.ListProfessionalUserById(id);
}
List<ProfessionalSpecialization> listSpecializationsOfUser = new List<ProfessionalSpecialization>();
List<ProfessionalSpecialization> listAllSpecializations = new List<ProfessionalSpecialization>();
List<ProfessionalType> listTypesOfUser = new List<ProfessionalType>();
List<ProfessionalType> listAllTypes = new List<ProfessionalType>();
using (ProfessionalUserDAO dao = new ProfessionalUserDAO())
{
try
{
using (ProfessionalSpecializationDAO sDAO = new ProfessionalSpecializationDAO())
{
listAllSpecializations = sDAO.ListProfessionalSpecialization(0);
foreach (int idToGetSpecialization in pUser.Specializations)
{
listSpecializationsOfUser.Add(sDAO.ListProfessionalSpecializationById(idToGetSpecialization));
}
}
using (ProfessionalTypeDAO tDAO = new ProfessionalTypeDAO())
{
listAllTypes = tDAO.ListProfessionalType(0);
foreach (int idToGetType in pUser.Types)
{
listTypesOfUser.Add(tDAO.ListProfessionalTypeById(idToGetType));
}
}
ViewBag.ListAllSpecializations = listAllSpecializations;
ViewBag.ListProfessionalSpecialization = listSpecializationsOfUser;
ViewBag.ListAllTypes = listAllTypes;
ViewBag.ListProfessionalType = listTypesOfUser;
return View(ProfessionalUser.UserLogged);
}
catch (Exception)
{
return HttpNotFound();
}
}
}
Actions that go to forms:
[Authorize]
[HttpPost]
public ActionResult RemoveProfessionalUserSpecializations(FormCollection form)
{
if (ModelState.IsValid && form != null)
{
string spec = form["specialization"].ToString();
using (ProfessionalUserDAO dao = new ProfessionalUserDAO())
{
dao.RemoveSpecializationFromUser(spec);
}
}
return RedirectToAction("Edit", new { id = ProfessionalUser.UserLogged.IdProfessionalUser });
}
[Authorize]
[HttpPost]
public ActionResult RemoveProfessionalUserTypes(FormCollection form)
{
if (ModelState.IsValid && form != null)
{
string type = form["type"].ToString();
using (ProfessionalUserDAO dao = new ProfessionalUserDAO())
{
dao.RemoveTypeFromUser(type);
}
}
return RedirectToAction("Edit", new { id = ProfessionalUser.UserLogged.IdProfessionalUser });
}