8

Tôi chỉ học ASP.net MVC vì vậy xin vui lòng chịu với tôi nếu tôi xấu giải thích vấn đề của tôi.ASP.net MVC4: Sử dụng mô hình khác trong chế độ xem một phần?

Có thể sử dụng mô hình khác trong chế độ xem một phần so với nội dung đang được kế thừa trong chế độ xem không?

Chế độ xem của tôi Index hiện được kế thừa LoginModel, liên quan đến ủy quyền của người dùng. Khi người dùng được ủy quyền, tôi muốn Index hiển thị danh sách todos người dùng có. todos được truy xuất qua LINQ.

Vì vậy, xem một phần của tôi muốn kế thừa System.Web.Mvc.ViewPage<IEnumerable<todo_moble_oauth.Models.todo>>, nhưng tôi nhận được một lỗi khi tôi sử dụng này: `Mô hình mục đã qua vào từ điển là loại

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

Đây là Index quan điểm của tôi

<%@ 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> 

xem một phần của tôi _ListTodos là như sau:

<%@ 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) %> 
<% } %> 

My LoginModel có sau đây:

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

Các HomeControllerIndex() phương pháp:

[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 appreciated rất nhiều, cảm ơn.

Trả lời

6

Chắc chắn bạn có thể làm điều đó:

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

Vượt qua userTodos như một tham số để helper phần.

Lỗi bạn nhận được là vì bạn đang trả về danh sách các todos cho trang/chế độ xem Chỉ mục với return View(todos); bên trong phương thức hành động Index. Trang Chỉ mục cần một đối tượng LoginModel thay vì một đối tượng cần làm là IEnumerable.

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

Để giải quyết việc này, bạn cần phải thay đổi cách bạn đang đi qua các todos mặc dù. Kể từ khi trang Index của bạn nhận được một LoginModel, bạn có thể thêm một tài sản Todos để lớp này như thế này:

[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; } 

và sau đó, sửa đổi phương thức action Index của bạn:

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

Trong giao diện, thực hiện điều này:

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

Cảm ơn bạn rất nhiều, đây cũng là giải pháp tốt và chi tiết. Tôi đã phải chuyển đổi '<% Html.Partial (" _ ListTodos ", Model.Todos); %> 'to' <% Html.RenderPartial ("_ ListTodos", Model.Todos); %> 'để làm cho nó hoạt động, nhưng mọi thứ bây giờ là vàng, cảm ơn! – Neil

+0

Tôi có một tình huống tương tự nhưng cấu trúc mô hình của tôi hơi khác một chút và không yêu cầu thay đổi vì tôi chỉ cần tạo một phiên bản mới của mô hình. Đối với nhu cầu của tôi, tôi đã sử dụng dòng mã này (Razor Syntax): @ Html.Partial ("hộp thoại/_partial-view", MyObjectModel mới()) – David

Các vấn đề liên quan