Nei*_*eil 8 asp.net asp.net-mvc model asp.net-mvc-partialview asp.net-mvc-4
我只是在学习ASP.net MVC,所以如果我不善于解释我的问题,请耐心等待.
是否可以在局部视图中使用与视图中继承的模型不同的模型?
我的视图Index目前继承LoginModel,它处理用户的授权.一旦用户被授权,我希望Index显示todos用户的列表.todos通过LINQ检索.
所以我的部分视图想要继承System.Web.Mvc.ViewPage<IEnumerable<todo_moble_oauth.Models.todo>>,但是当我使用它时我得到一个错误:`传递给字典的模型项是类型的
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有以下几点:
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();
    }
非常感谢任何帮助,谢谢.
当然你可以这样做:
<% Html.Partial("_ListTodos", userTodos); %>
传递userTodos作为参数传递给partial助手.
您得到的错误是因为您在操作方法return View(todos);内部返回了索引页面/视图中的待办事项列表Index.索引页面需要一个LoginModel对象而不是一个IEnumerable待办事项对象.
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
    Inherits="System.Web.Mvc.ViewPage<todo_moble_oauth.Models.LoginModel>" %>
要解决这个问题,你需要改变你通过的方式todos.由于您的Index页面收到了a LoginModel,您可以Todos像这样向这个类添加一个属性:
[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; }
然后,修改您的索引操作方法:
[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); %>
| 归档时间: | 
 | 
| 查看次数: | 16664 次 | 
| 最近记录: |