如何将int值作为模型传递给action方法中的视图

时间:2010-09-16 12:48:27

标签: asp.net asp.net-mvc

我正在使用ASP.NET MVC 1.我想从要查看的操作传递一个int值。我可以使用2种方式。

  1. 使用ViewData字典。
  2. 定义一个包含int值的类。
  3. 其他这两个,有没有办法将int值传递给view,这样我就可以使用像

    这样的模型来获取int值
    <label><%= Model %></label>
    

2 个答案:

答案 0 :(得分:6)

这适用于MVC 2.不知道为什么你会坚持使用MVC 1.我也无法在MVC1中测试它,所以我不能保证它在那里工作。

    public ActionResult Five()
    {
        int five = 5;
        return View(five);
    }


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

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Five</h2>

    <%: Model %>

</asp:Content>

答案 1 :(得分:1)

是的。只需定义您的强类型视图:

<%@ Page ... Inherits="System.Web.Mvc.ViewPage<int>" %>