0

I tried making a partial view, actually it works but when I tried to put my code in html, I always get a Nullable error. This work :

public PartialViewResult Notification()
{
    ViewBag.Notification = db.Notification.ToList();
    return PartialView("Notification");
}

@Html.Action("Notification", "Project");

but when I tried to make it like this:

public PartialViewResult Notification()
{
    ViewBag.Notification = db.Notification.ToList();
    return PartialView("Notification");
}

@Html.RenderPartial("Notification", "Project");

or

@Html.RenderPartial("Notification", "Project");

Such codes give me a Nullable error

Here are my PartialView

@{
    foreach (var item in ViewBag.Notification)
    {
         @item.Name
    }
}
kendj
  • 51
  • 1
  • 6
Captain
  • 113
  • 2
  • 9
  • 2
    Firstly its would need to be `@{ Html.RenderPartial("Notification", "Project"); }` But `RenderPartial()` does not call your controller - it just outputs the html generate by the view, so unless you pass a model to `RenderPartial()`, then you model is `null`, hence the exception –  Mar 09 '16 at 08:12
  • 1
    If you also switch to using RenderAction consider using ViewModels instead of storing data in ViewBag - that's a subject to huge magical issues happening occasionally. – Red Mar 09 '16 at 08:31

2 Answers2

0

if you want to use @html.RenderPartail then this ViewBag thing will not work because this will not hit the action, still you want to use it then you should pass a model object in the @Html.RendrePartial itself.

Varun Vasishtha
  • 461
  • 2
  • 9
  • Note that your now deleted answer to [this question](http://stackoverflow.com/questions/35892025/error-with-dropdownlist-when-ckeditor-is-also-in-use-on-page) was correct. If you un-delete it, I will be happy to upvote. –  Mar 10 '16 at 00:16
  • Thanks @StephenMuecke, I have un deleted it. – Varun Vasishtha Mar 10 '16 at 05:46
0

If you want to use @Html.RenderPartial you no longer need your Notification Action.

When you use @Html.Action you have to handle the flow, what view will you render, what model it will be and others...

When you call @Html.RenderPartial you are asking for microsoft code to handle the flow, and it will do, it will provide the view you want as you did by using @Html.Action, you just have to fill parameters.

I guess that this partial view you want to render is a not typed one, ok then.

You should do this way:

View action:

public ActionResult PreviousAction()
{    
    ViewBag.Notification = db.Notification.ToList();    
    return View();
}

View code:

@{ Html.RenderPartial("Notification"); }

The parameter Notification you are passing, is not an Action name anymore, now it's the name of partial view that you are asking for Microsoft code to render.

Partial view code:

In the partial view you have your code as it currently is:

@{
    foreach (var item in ViewBag.Notification)
    {
         @item
    }    
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Raphael Ribeiro
  • 529
  • 5
  • 18