0

i have an action named PackageIndex in which i am creating a list type view of package class ..in this view i use layout in which i have to to display notification on navbar ...for that purpose i have to pass notification list to layout to display notification..my code is given below...

  public ActionResult PackageIndex()
        {
            //feedback counter
            int count = feedbackCounter();
            ViewData["FeedbackCount"] = count;
            int notificationcount = notificationCounter();
            ViewData["notificationcount"] = notificationcount;
            return View(db.packages.ToList());
        }

in this action i also have to passs (db.notification.ToList())...to provide data to layout ..i can't understand how to solve this...

sajiii
  • 172
  • 1
  • 1
  • 8
  • Possible duplicate of [ASP.NET MVC - How to pass an Array to the view?](http://stackoverflow.com/questions/1405383/asp-net-mvc-how-to-pass-an-array-to-the-view) – killthrush Jun 29 '16 at 00:23

1 Answers1

0

You can create a model for this view, for example:

public class PackageIndexModel()
{
    public List<Package> Packages { get; set; }
    public int NotificationCount { get; set; }
    public int FeedbackCount { get; set; }
}

and you can return this

var obj = new PackageIndexModel() { Packages = db.packages.ToList(), NotificationCount = notificationCounter(), FeedbackCount = feedbackCounter() };
return View(obj);

Or you could just set that object on a TempData:

TempData["Notifications"] = obj;

And in your _Layout.cshtml:

@{
    if (TempData["Notifications"] != null)
    {
        foreach (var notification in ((PackageIndexModel)TempData["Notification"]).Packages)
        {
            <script type="text/javascript">
                jQuery(document).ready(function () {
                    alert('@notification.Message');
                });
            </script>
        }

        TempData["Notifications"] = null;
    }
}

EDIT: I think the second example is cleaner, because on don't have to receive your notifications on every view you create.

gnllucena
  • 804
  • 7
  • 6
  • if i follow second case then i also have to make 'packageIndexmodel' or this is essential for only case first . – sajiii Jun 28 '16 at 20:01
  • No, you don't. You could pass a list of strings to your tempdata, and your foreach would be something like this: foreach (var notification in (List)TempData["Notification"]) – gnllucena Jun 28 '16 at 20:05
  • `@model IEnumerable @foreach (var item in (List)TempData["Notification"]){ }
    @Html.DisplayFor(modelItem => item.email)

    send feedback about

    @Html.DisplayFor(modelItem => item.subject)
    ` item does not contain any email getting error...
    – sajiii Jun 28 '16 at 20:10
  • Of course, you set a list of string and tried to access an email property. – gnllucena Jun 28 '16 at 20:12
  • If you're using a list os string in your tempdata, it just that string. If you set a list of Packages in your tempdata, you have all properties from that object. – gnllucena Jun 28 '16 at 20:13
  • i solved this but i gain i getting error...The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[WebApplication5.Models.UserManager]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[WebApplication5.Models.Feedback]'. – sajiii Jun 28 '16 at 20:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/115888/discussion-between-gnllucena-and-sajiii). – gnllucena Jun 28 '16 at 20:15