Error Creating Partial View

0

Trying to do a partial view in ASP.NET with C #, it throws me the following error, it is worth noting that I try to do it in different ways and it keeps marking the same error, it is the first time it happens to me with a view and I do not have idea of why, because if the Model object exists.

How can I solve it?

@model IEnumerable<MvcBackBolsaTrabajo.Models.EntrevistaIdiomaModel>

@foreach (var item in Model) { 
    <li>
        @Html.DisplayFor(modelItem => item.nombreCand) @Html.DisplayFor(modelItem => item.apellidoCnad)
    </li>}

Controller

namespace MvcBackBolsaTrabajo.Controllers
  {
    public class EntrevistaIdiomaController : Controller
     {
         IdiomaDAO m_idiomasDAO = new IdiomaDAO();
         EntrevistaIdiomaDAO m_entrevistaIdiomaDAO = new EntrevistaIdiomaDAO();

    // GET: EntrevistaIdioma
    public ActionResult Index()
    {
        return View(m_entrevistaIdiomaDAO.Todos());
    }


    //Detalle

    public ActionResult Details(int id)
    {
        EntrevistaIdiomaModel entrevistaIdiomaModel = m_entrevistaIdiomaDAO.Obtener(id);

        return View(entrevistaIdiomaModel);
    }

    public ActionResult Notificacion()
    {
        ViewBag.Notificacion = m_entrevistaIdiomaDAO.TodosXFecha();

        return View();
    }

Model

  namespace MvcBackBolsaTrabajo.Models
   {
   public class EntrevistaIdiomaModel
     {
    public int EntrevistaIdiomaID { get; set; }

    [DisplayName("Nombre")]
    public string nombreCand { get; set; }

    [DisplayName("Apellidos")]
    public string apellidoCnad { get; set; }

    [DisplayName("Fecha")]
    public DateTime fecha { get; set; }

    [DisplayName("C.Auditiva")]
    public string comprencionAuditivaCal { get; set; }

    [DisplayName("E.Oral")]
    public string expresionOralCal { get; set; }

    [DisplayName("E.Escrita")]
    public string expresionEscritaCal { get; set; }

    [DisplayName("Comentarios")]
    public string comentarios { get; set; }

    [DisplayName("Vacante")]
    public string vacante { get; set; }

    [DisplayName("Recomendación")]
    public string recomendacion { get; set; }

    [DisplayName("Idioma")]
    public int IdiomasID { get; set; }

    public IdiomaModel Idioma { get; set; }


}

}

    
asked by Roberto Dominguez 22.06.2017 в 20:28
source

4 answers

0

If you are using

@foreach (var item in Model) {

I think it should be:

@Html.DisplayFor(item.nombreCand) @Html.DisplayFor(item.apellidoCnad)
    
answered by 22.06.2017 в 20:38
0
return View(m_entrevistaIdiomaDAO.Todos());

is returning a null list, not even empty.

In that method try to do something like:

public IEnumerable<EntrevistaIdiomaModel> Todos()
{
    var result = new List<EntrevistaIdiomaModel>();
    //.. tu código de selección de datos
    return result;
}
    
answered by 22.06.2017 в 21:31
0

In the view where you insert the partial view (which I imagine is Index ) you should take advantage of the fact that you already sent the model:

public ActionResult Index()
{
    return View(m_entrevistaIdiomaDAO.Todos());
}

and send it back to partial view:

@Html.Partial("_NotificacionInglrd", Model)

or

@Html.Partial("~/Views/Shared/_NotificacionInglrd.cshtml", Model)
@* Por si requieres la ruta completa *@
    
answered by 23.06.2017 в 00:14
0

NullReferenceException occurs only when you try to access an object that has no reference in memory. In other words: it's empty.

This part of the code is the source of the problem:

 return View(m_entrevistaIdiomaDAO.Todos());

The method Todos() is returning null so when you try to traverse the collection in the view it throws the error.

If you want to prevent the error from occurring when the method returns null you can use the operator Null coalescing :

return View(m_entrevistaIdiomaDAO.Todos() ?? new List<EntrevistaIdiomaModel>());

Of course, it would be best to fix the Todos() method so that it does not return null if it does not find data.

    
answered by 23.06.2017 в 14:50