2

In the following code I'm trying to return a view with an integer as its type. The code below doesn't work. What am I missing?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication5.Controllers
{
    public class HomeController : Controller
    {

        public ActionResult Index(int answer)
        {


           int row = 2;
           int col = 1; 

           answer = row * col ; 
           return View(answer);
        }

    }
}
2
  • 2
    What, exactly, is the error you are getting? This could be caused by a number of things depending on how you have the routes configured in your application as where your View is located/how your view is defined. Commented Mar 10, 2011 at 18:25
  • What is the error you are getting? Are you getting the error when you compile, or when you try view the page? Do you have a view file called Index.aspx in your views folder? Commented Mar 10, 2011 at 18:26

4 Answers 4

4
public ActionResult Index(int answer)
{
    int row = 2;
    int col = 1; 
    int answer = row * col ; 
    return View(answer);
}

and then ensure your view is strongly typed to int:

<%@ Page 
    Language="C#" 
    MasterPageFile="~/Views/Shared/Site.Master" 
    Inherits="System.Web.Mvc.ViewPage<int>" 
%>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    The answer is: <em><%= Html.DisplayForModel() %></em>
</asp:Content>
Sign up to request clarification or add additional context in comments.

3 Comments

i am using mvc empty webapplication. i dont have anything in my views. can you tell me how can i get ~/Views/Shared/Site.Master" . what is the master page here?
Inherits="System.Web.Mvc.ViewPage<int> what is the int means?
@software, it indicates that this View inherits from the generic System.Web.Mvc.ViewPage<int> type where int indicates the type of your model (it is the type you are passing when returning the view return View(answer), answer is int)
0

There is nothing wrong with the code you posted.

The error must be related to something else.

If you want to know the cause of the error it would help to indicate what error you are receiving, as well as stack information.

Comments

0

You're likely requesting the action URL without specifying an answer parameter. Change your method signature to the following and it should all work:

public ActionResult Index()

2 Comments

it tried the way you said. the coding works (break point , f11) but it gives different error again..Server Error in '/' Application. The view 'Index' or its master was not found. The following locations were searched: ~/Views/Home/Index.aspx ~/Views/Home/Index.ascx ~/Views/Shared/Index.aspx ~/Views/Shared/Index.ascx
Okay, you're going to need a tutorial on ASP.NET MVC. I suggest you download the Nerd Dinner tutorial (nerddinnerbook.s3.amazonaws.com/Intro.htm) and complete it before proceeding.
0

As I can see, you're trying to open the URL http://localhost:8080/Home/Index and the problem is that the action "Index" have a parameter called answer.

Try to call http://localhost:8080/Home/Index?answer=10 and you'll see that it will work.

edit:

as Darin said, make sure that the Index.aspx exists.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.