开发者

Why i can't call the Model and ViewData, and Html in the strong typed page?

开发者 https://www.devze.com 2022-12-11 09:39 出处:网络
I created a new strong typed View ,something like this: <%@ Page Title=\"\" Language=\"C#\" MasterPageFile=\"~/Views/Shared/Site.Master\"

I created a new strong typed View ,something like this:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" 
  Inherits="System.Web.Mvc.ViewPage<MiniMain.ViewModel.ArticleViewdata>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    <%Model %>
</asp:Content>

but when I called the Mode ,Html,Viewdata,then had no intellisense tips.I can't figure it,please开发者_StackOverflow中文版 tell me how to do this?


I have figured it.If you create a strong typed view,you should add this config to the web.config under the View path:

<pages
    validateRequest="false"
    pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
    pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
    userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
  <controls>
    <add assembly="System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
  </controls>
</pages> 


See this article on ViewModels.

You need to do some alternative coding in the asp:content instead of just calling <% model %> and hoping that intellisense will work - you have told the page what the "model" is so all you need to do is start to "use" the model on the page.

In this example, from Stephens article, iterate of each item in the model and build a list.

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">  
    <% foreach (var item in Model)    
       { %>  
        <li> <%= item.Name %> </li>  
    <% } %>  
</asp:Content> 


Try using <%= Model.blah %> instead of <% Model.blah %>

0

精彩评论

暂无评论...
验证码 换一张
取 消