Site.Master 中的 MVC 部分视图

发布于 2024-12-19 09:39:00 字数 3016 浏览 1 评论 0原文

我试图在 Site.Master 文件中渲染部分视图,但不断收到错误(对象引用未设置为对象的实例)。如果我直接访问视图,我会得到所需的信息,但如果我从 HomeController 访问索引视图,则会收到错误。部分视图将显示登录的用户、部门和角色。这是我的代码。

控制器文件夹 -HomeController.cs -UsersController.cs

Models
-Repository Folder
  - UersRepository.cs
-Repository Interface Folder
  - IUsers.cs
-Service Folder
  - UsersService.cs
  - IUserService.cs
-Validation Folder
  - IValidationDictionary
  - ModelStateWrapper

我尝试部分呈现的视图称为 LoginInfo.ascx,它位于共享文件夹中。

LoginInfo.cs 代码:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Users>>" %>

<%@ Import Namespace="TimeAttendanceMVC.Models"%>




<table>
    <tr>
        <th>
            UserName
        </th>
        <th>
            Department
        </th>
        <th>
            UserType
        </th>

    </tr>

<% foreach (var item in Model) { %>
    <tr>
        <td>
            <%: Html.DisplayFor(modelItem => item.UserName) %>
        </td>
        <td>
            <%: Html.DisplayFor(modelItem => item.Department) %>
        </td>
        <td>
            <%: Html.DisplayFor(modelItem => item.UserType) %>
        </td>

    </tr>
<% } %>

</table>

Site.Master 代码:

   <% Html.RenderPartial("LoginInfo"); %>

UserController.cs

 public class UsersController : Controller
    {
        //
        private IUsersService _service;

        //==============================================================================
        //==============================================================================
        public UsersController()
        {
          _service = new UsersService(new ModelStateWrapper(this.ModelState));
         }

        public UsersController(IUsersService service)
        {
            _service = service; 
        }

        //==============================================================================
        //==============================================================================
        // GET: /Employee/
        //==============================================================================
        //==============================================================================
        // GET: /Employee/
        public ActionResult Index()
        {

            var model = _service.Return_UserName_Dept();
            return View("LoginInfo", model);
        }

    }

HomeController.cs

 public ActionResult Index()
        {
            //var model = _service.Return_UserName_Dept();
            //return View(model);
            return View();

        }

使用上面的代码,我收到错误。如果我取消注释 HomeController 中的两行并将模型传递给视图,那么它就可以正常工作。但我的 HomeController 将需要一个不同的模型,那么我如何将 2 个模型传递给视图呢?

有什么想法我做错了吗?我现在仍在学习 MVC,所以我对此不太擅长。任何帮助将不胜感激。谢谢。

I am trying to render a partial view in my Site.Master file but I keep on getting an error (Object reference not set to an instance of an object). If I access the view directly, I get the info that I need, but if I access the Index View from the HomeController, I get an error. The partial view will display the logged in user, department, and role. This is my code.

Controllers Folder
-HomeController.cs
-UsersController.cs

Models
-Repository Folder
  - UersRepository.cs
-Repository Interface Folder
  - IUsers.cs
-Service Folder
  - UsersService.cs
  - IUserService.cs
-Validation Folder
  - IValidationDictionary
  - ModelStateWrapper

The view that I'm trying to partially render is called LoginInfo.ascx and it's located in the Shared Folder.

LoginInfo.cs Code:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Users>>" %>

<%@ Import Namespace="TimeAttendanceMVC.Models"%>




<table>
    <tr>
        <th>
            UserName
        </th>
        <th>
            Department
        </th>
        <th>
            UserType
        </th>

    </tr>

<% foreach (var item in Model) { %>
    <tr>
        <td>
            <%: Html.DisplayFor(modelItem => item.UserName) %>
        </td>
        <td>
            <%: Html.DisplayFor(modelItem => item.Department) %>
        </td>
        <td>
            <%: Html.DisplayFor(modelItem => item.UserType) %>
        </td>

    </tr>
<% } %>

</table>

Site.Master Code:

   <% Html.RenderPartial("LoginInfo"); %>

UserController.cs

 public class UsersController : Controller
    {
        //
        private IUsersService _service;

        //==============================================================================
        //==============================================================================
        public UsersController()
        {
          _service = new UsersService(new ModelStateWrapper(this.ModelState));
         }

        public UsersController(IUsersService service)
        {
            _service = service; 
        }

        //==============================================================================
        //==============================================================================
        // GET: /Employee/
        //==============================================================================
        //==============================================================================
        // GET: /Employee/
        public ActionResult Index()
        {

            var model = _service.Return_UserName_Dept();
            return View("LoginInfo", model);
        }

    }

HomeController.cs

 public ActionResult Index()
        {
            //var model = _service.Return_UserName_Dept();
            //return View(model);
            return View();

        }

With the code above, I get an error. If I un-comment the 2 lines in my HomeController and I pass the model to the View, then it works fine. But my HomeController will need a different model, so how will I pass 2 models to the View?

Any ideas what I'm doing wrong? I'm still learning MVC right now so I'm not that good at this. Any help would be appreciated. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白鸥掠海 2024-12-26 09:39:00

调用 RenderPartial 会直接渲染视图,继承父级的模型。

您应该调用 RenderAction 来渲染子操作,以便该操作可以将正确的模型传递给分部视图。

Calling RenderPartial renders the view directly, inheriting the parent's model.

You should call RenderAction instead to render a child action so that the action can pass the correct model to the partial view.

霊感 2024-12-26 09:39:00

您的控制器能够将单个模型传递给视图进行渲染。您是否考虑过创建父 ViewModel 类型,而不是尝试拥有两个模型,或者使用 RenderAction 在请求中调用两个 Action 方法?

// This model is passed to your view for rendering
public class MyViewModel
{
    public string Title { get; set; }
    public DateTime Date { get; set; }

    // This property is passed as the model to your partial view
    public UserInfo UserInfo { get; set; }
}

public ActionResult Index()
{
    var model = new MyViewModel(); // _service.GetIndexViewModel();
    model.UserInfo = _service.Return_UserName_Dept();;
    return View(model);
}

然后在你的视图中,你会做类似的事情:

<% Html.RenderPartial("LoginInfo", Model.UserInfo); %>

Your Controller is able to pass a single Model to the View for rendering. Rather than attempting to have two models, or to call two Action Methods in a request with RenderAction, have you considered creating a parent ViewModel type?

// This model is passed to your view for rendering
public class MyViewModel
{
    public string Title { get; set; }
    public DateTime Date { get; set; }

    // This property is passed as the model to your partial view
    public UserInfo UserInfo { get; set; }
}

public ActionResult Index()
{
    var model = new MyViewModel(); // _service.GetIndexViewModel();
    model.UserInfo = _service.Return_UserName_Dept();;
    return View(model);
}

Then in your View, you would do something like:

<% Html.RenderPartial("LoginInfo", Model.UserInfo); %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文