如何使用模型类在布局视图中调用局部视图? [英] How to call a partial view in layout view with model class?

查看:77
本文介绍了如何使用模型类在布局视图中调用局部视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我是mvc的新手我想在部分视图中绑定我的菜单请告诉我该怎么做。当我调试我的代码时,它会在TopMenu局部视图的每个循环中显示空值异常。

我在这里粘贴代码请让我知道我做错了什么



这是我的控制器

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

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();
        }

	}
}





这是LayOut View



This Is LayOut View

<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

@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

推荐答案

你好,



看起来错误就在

Hello,

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





part。执行_Layout部分页面时,模型值为null。如果您将模型传递给局部视图,那就没关系了。如果你看到,你的函数是一个Action Method,那就是从控制器返回一个Strongly Typed View。因此,您应该调用Action方法而不是Partial View。



所以如果你打电话给



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

@Html.Action("TopMenu","CatelogController") //This is the right approach



然后它将获取菜单,将它们发送到局部视图,构建HTML并将注入到_Layout。



希望这个帮助。



我创建了一个上传到Github的虚拟项目。您可以下载并检查它。

链接到Github存储库。点击此处下载 [ ^ ]

查看 HomeController _Layout.cshtml page


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


这篇关于如何使用模型类在布局视图中调用局部视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆