本文介绍了ASP.net MVC4:在局部视图中使用不同的模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在学习 ASP.net MVC,所以如果我不善于解释我的问题,请多多包涵.

I am just learning ASP.net MVC so please bear with me if I am bad at explaining my issue.

是否可以在局部视图中使用与视图中继承的模型不同的模型?

Is it possible to use a different model in a partial view than what is being inherited in the view?

我的视图Index目前继承了LoginModel,处理用户的授权.一旦用户获得授权,我希望 Index 显示用户拥有的 todos 列表.todos 通过 LINQ 检索.

My view Index currently inherits LoginModel, which deals with the authorization of users. Once a user is authorized, I want the Index to display the list of todos the user has. todos are retrieved via LINQ.

所以我的局部视图想要继承 System.Web.Mvc.ViewPage,但是当我使用它时出现错误:`模型项传入字典的类型是

So my partial view wants to inherit System.Web.Mvc.ViewPage<IEnumerable<todo_moble_oauth.Models.todo>>, but I get an error when I use this: `The model item passed into the dictionary is of type

System.Data.Linq.DataQuery`1[todo_moble_oauth.Models.todo]', but this dictionary requires a model item of type 'todo_moble_oauth.Models.LoginModel'

这是我的Index视图

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<todo_moble_oauth.Models.LoginModel>" %>

<section id="loginForm">
    <% if (Request.IsAuthenticated) { %>

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

    <% } else { %>

        <h1>Todo Mobile</h1>

        <blockquote>Easily store your list of todos using this simple mobile application</blockquote>

        <% using (Html.BeginForm()) { %>
            <%: Html.AntiForgeryToken() %>
            <%: Html.ValidationSummary(true) %>

                    <%: Html.LabelFor(m => m.UserName) %>
                    <p class="validation"><%: Html.ValidationMessageFor(m => m.UserName) %></p>
                    <%: Html.TextBoxFor(m => m.UserName) %>

                    <%: Html.LabelFor(m => m.Password) %>
                    <p class="validation"><%: Html.ValidationMessageFor(m => m.Password) %></p>
                    <%: Html.PasswordFor(m => m.Password) %>

                    <label class="checkbox" for="RememberMe">
                        <%: Html.CheckBoxFor(m => m.RememberMe) %>
                        Remember Me?
                    </label>

            <input type="submit" value="Login" />
        <% } %>
    <% } %>
</section>

我的部分视图_ListTodos如下:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<todo_moble_oauth.Models.todo>>" %>

<% foreach (var item in Model) { %>
      <%: Html.DisplayFor(modelItem => item.title) %>
      <%: Html.DisplayFor(modelItem => item.description) %>
<% } %>

我的 LoginModel 具有以下内容:

My LoginModel has the following:

public class LoginModel
{
    [Required]
    [Display(Name = "User name")]
    public string UserName { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [Display(Name = "Remember me?")]
    public bool RememberMe { get; set; }
}

HomeController Index() 方法:

    [AllowAnonymous]
    public ActionResult Index()
    {
        // if user is logged in, show todo list
        if (Request.IsAuthenticated)
        {
            //var currentUser = Membership.GetUser().ProviderUserKey;
            todosDataContext objLinq = new todosDataContext();
            var todos = objLinq.todos.Select(x => x);
            return View(todos);
        }
        return View();
    }

非常感谢任何帮助,谢谢.

Any help is greatly appreciated, thanks.

推荐答案

当然可以:

<% Html.Partial("_ListTodos", userTodos); %>

userTodos 作为参数传递给 Partial 助手.

Pass the userTodos as a parameter to the Partial helper.

您收到的错误是因为您在 Index 操作中使用 return View(todos); 将待办事项列表返回到索引页面/视图方法.索引页面需要一个 LoginModel 对象,而不是一个 IEnumerable 待办事项对象.

The error you're getting is because you're returning a list of todos to the Index page/view with return View(todos); inside the Index action method. The Index page needs a LoginModel object instead of an IEnumerable of todo objects.

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
    Inherits="System.Web.Mvc.ViewPage<todo_moble_oauth.Models.LoginModel>" %>

要解决这个问题,您需要更改传递 todos 的方式.由于您的 Index 页面接收一个 LoginModel,您可以像这样向此类添加一个 Todos 属性:


To solve this, you need to change the way you're passing the todos though. Since your Index page receives a LoginModel, you can add a Todos property to this class like this:

[Required]
[Display(Name = "User name")]
public string UserName { get; set; }

[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }

[Display(Name = "Remember me?")]
public bool RememberMe { get; set; }

public IEnumerable<todo_moble_oauth.Models.todo> Todos { get; set; }

然后,修改您的索引操作方法:

and then, modify your Index action method:

[AllowAnonymous]
public ActionResult Index()
{
    // if user is logged in, show todo list
    if (Request.IsAuthenticated)
    {
        //var currentUser = Membership.GetUser().ProviderUserKey;
        todosDataContext objLinq = new todosDataContext();
        var todos = objLinq.todos.Select(x => x);

        LoginModel model = new LoginModel();
        model.Todos = todos;

        return View(model);
    }

    return View();
}

在视图中,这样做:

<% Html.Partial("_ListTodos", Model.Todos); %>

这篇关于ASP.net MVC4:在局部视图中使用不同的模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-05 11:57