簡體   English   中英

ASP.Net MVC-模型和用戶控件

[英]ASP.Net MVC - Models and User Controls

我有一個具有母版頁的視圖。 用戶控件使用模型:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<BudgieMoneySite.Models.SiteUserLoginModel>" %>

此用戶控件顯示在所有屏幕上(母版頁的一部分)。 如果用戶已登錄,則顯示特定文本,如果用戶未登錄,則提供一個登錄框。

沒問題。

現在,我要添加第一個功能屏幕。 因此,我創建了一個新視圖...並且,當我選擇控制器方法並說“創建視圖”時,我為我生成了基本視圖代碼。

我的控制器具有以下代碼:

        public ActionResult Transactions()
    {
        List<AccountTransactionDetails> trans = GetTransactions();
        return View(trans);
    }

    private List<AccountTransactionDetails> GetTransactions()
    {
        List<AccountTransactionDto> trans = Services.TransactionServices.GetTransactions();

        List<AccountTransactionDetails> reply = new List<AccountTransactionDetails>();

        foreach(var t in trans)
        {
            AccountTransactionDetails a = new AccountTransactionDetails();
            foreach (var line in a.Transactions)
            {
                AccountTransactionLine l = new AccountTransactionLine();
                l.Amount = line.Amount;
                l.SubCategory = line.SubCategory;
                l.SubCategoryId = line.SubCategoryId;
                a.Transactions.Add(l);
            }
            reply.Add(a);
        }


        return reply;
    }

因此,我的觀點是這樣產生的:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<System.Collections.Generic.List<BudgieMoneySite.Models.AccountTransactionDetails>>" %>

找到<%= Model.Count()%>事務。

我現在要顯示的是我將要顯示的記錄數。

運行它時,出現錯誤:

“傳遞到字典中的模型項的類型為'System.Collections.Generic.List`1 [BudgieMoneySite.Models.AccountTransactionDetails]',但此字典要求模型項的類型為'BudgieMoneySite.Models.SiteUserLoginModel'。”

似乎首先呈現了用戶控件,並且由於控制器中的Model是我的List <>,所以它很糟糕!

我究竟做錯了什么?

並不是說它本身就是“第一個”渲染的,更多的是所有視圖都采用了控制器傳遞下來的Model,除非另有說明。

要解決此問題,您可以創建所有視圖模型都將繼承的基類,定義Master及其局部視圖所需的內容(然后將適當的屬性傳遞給RenderPartial重載之一),或者可以使用無類型的ViewData包執行類似的操作。

*編輯*因此,我意識到我正在按要求回答您的問題,而不是我會解決的問題。 上面的響應是正確的,如果您想在母版頁中處理模型內容,則需要將它們填充到ViewData字典中(例如ViewData [“ myData”] = mydata;),或者必須為所有您的應用創建基類繼承的模型。

就是說,在大多數情況下(尤其是像您作為例子的profiel之類的東西),我也不做。 在大多數情況下,我改用了render操作,該操作要求另一個控制器的局部視圖。 然后,其他控制器可以提供其部分適當的數據。 有些人認為這是違反MVC的行為,我認為這是一種構成視圖並實際上保持邏輯結構完整的好方法,而不是通過要求控制器知道母版頁什么都不需要的東西來弄混,即使它無所事事/該控制器的工作。 通常的解決方案是也有一個基本控制器,或者使用動作過濾器,但是我發現這是您必須維護的東西。

LoginModel不應設置為頁面的模型,而只能設置為部分視圖的模型。 您可以使用RenderAction來顯示未連接到頁面模型的模型的局部視圖。 這是我實現顯示在母版頁上的登錄狀態控件的方式。

局部視圖(Views / Auth / LoginStatus.ascx):

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

<%    if (Request.IsAuthenticated && Model != null) { %>
    Welcome <b><%= Model.DisplayName %></b>!
    [ <%= Html.ActionLink("Log Off", "LogOff", "Auth") %> ]
<%
}
else {
%> 
    [ <%= Html.ActionLink("Log On", "Login", "Auth") %> ]
<%
} 
%>

在沒有強類型模型的母版頁中:

<% Html.RenderAction("LoginStatus", "Auth"); %>

並在控制器(AuthController)中

    public ActionResult LoginStatus()
    {
        TProfile p = null;
        if (User.Identity.IsAuthenticated)
        {
            p = Data.Profiles.Get(ulong.Parse(User.Identity.Name));
        }
        return View(p);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM