Click here to Skip to main content
15,884,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I am new in mvc I want to bind my menu in partial view please tell me how can I do it .When i debug my code it shows null value exception in for each loop of TopMenu Partial view .
I am pasting the code here please let me know what i am doing wrong

This Is MY Controller
C#
public class CatelogController : Controller
    {
        IDataAccessLayer objDataAccess;
        public CatelogController()
        {
            objDataAccess = new DataAccessLayer();
        }

        //
        // GET: /Catelog/
        
        public ActionResult Company()
        {
            //IEnumerable<Menu> menutbl = objDataAccess.GetMenu();
            //Menu ob = new Menu();
            //ob.MenuList = menutbl;

            return View();
        }

        [HttpGet]
        public ActionResult TopMenu() // This action is used for menu 
        {
            IEnumerable<Menu> menutbl = objDataAccess.GetMenu();
       

            return PartialView(menutbl);
        }
    [HttpPost]
        public ActionResult Company(Company company)
        {
            return View();
        }

	}
}


This Is LayOut View
C#
<meta charset="utf-8" />
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   @model  IEnumerable<WebApplication15.Models.Menu>
   @using WebApplication15.Models
   <title>@ViewBag.Title - My ASP.NET Application
                   @Html.ActionLink("Application name", "Index", "Home", null, new { @class = "navbar-brand" })

               @Html.Partial("TopMenu", Model) // I Am calling my partial view here..

This is my Menu Partial View
C#
@model IEnumerable<WebApplication15.Models.Menu>






            @Html.DisplayNameFor(model => model.MenuName)
     
            @Html.DisplayNameFor(model => model.MenuDescription)
    
     
@foreach (var item in Model) { // exception occurs here
   
            @Html.DisplayFor(modelItem => item.MenuName)
      
            @Html.DisplayFor(modelItem => item.MenuDescription)
  
            @Html.ActionLink("Edit", "Edit", new { id=item.MenuId }) |
            @Html.ActionLink("Details", "Details", new { id=item.MenuId }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.MenuId })
        </td>
    </tr>
}

</table>


Please give me solution
Posted
Updated 13-Sep-15 5:20am
v2
Comments
[no name] 13-Sep-15 12:12pm    
When you are loading main view page(first time) what action it is calling in your controller? Because you are sending partialview result in TopMenu().
Member 10952217 13-Sep-15 12:16pm    
Company() controller is being called first time
Swagat Swain 14-Sep-15 1:04am    
Check out my solution and let know if it works.

1 solution

Hello,

It looks like the mistake is there in the
C#
@Html.Partial("TopMenu", Model) // I Am calling my partial view here..


part. When the _Layout partial page is executed, model value is null. Had you been passing the Model to the partial view it would have been ok. If you see, your function is an Action Method, that is returning a Strongly Typed View from the controller. So you should call the Action method instead of the Partial View.

So if you call the
C#
@Html.Action("TopMenu","CatelogController") //This is the right approach

then it will fetch the menus, Send them to the partial view, Build the HTML and will be injected in to the _Layout.

Hope this helps.

I have created a dummy project on uploaded to Github. You can download and check that.
Link to Github Repository. Click here to download[^]
Have a look at the HomeController and _Layout.cshtml page
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900