sree2010
sree2010

Reputation: 73

How to pass int value as model to a view from an action method

I am using ASP.NET MVC 1. I want to pass an int value to from an action to view. I can use 2 ways.

  1. Use ViewData dictionary.
  2. Define a class to contain the int value.

Other these two, Is there a way to pass the int value to view so that I can get the int value using just Model like

<label><%= Model %></label>

Upvotes: 1

Views: 2086

Answers (2)

Robert Koritnik
Robert Koritnik

Reputation: 105019

Yes it is. Just define your strong type view like this:

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

Upvotes: 1

mare
mare

Reputation: 13083

This works just fine with MVC 2. Don't really know the reason why would you stick with MVC 1. And I also couldn't test it out in MVC1 so I cannot guarantee that it works there.

    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>

Upvotes: 6

Related Questions