-1
Hello, I have two Viewbags that are storing a list each, I click them in a form to make a select box, along with them has a Remove button. So far so good, but when I click on the button of the first Viewbag, a reference loss error occurs, while 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 clicked on the second form’s Submit, it follows all right, if clicked on the first, the following error occurs:
Undefined object reference for an object instance. 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 the 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 });
}
Just so I understand: you have two
Actions
, being a callRemoveProfessionalUserTypes
and another callRemoveProfessionalUserSpecializations
? In both elements ofViewBag
are loaded?– Leonel Sanches da Silva
Each Action Treats a Different Viewbag.
– Luiz Negrini
This
Action
which you put is which one?– Leonel Sanches da Silva
This action is the action that loads the lists to the Viewbags, but the "Removes" not even put here. When I give the Ubmit in the form below it enters in the "Removeprofessionalusertype", but if it is clicked in the form above it of the error, nor it enters in the Action, I put the breakpoint and the error before entering the action, I do not even understand why it enters in the if again, error on this if.
– Luiz Negrini
yes both are in the controller. I have no bro. =/
– Luiz Negrini
I discovered the Error, these 2 Forms are inside another one, for some reason the first one was being undone!
– Luiz Negrini