2

Basically I am a Winforms guy. Learning ASP.Net and MVC and stuck in a problem.

I have page with name Calculation. Contents are as follows

<asp:Content ID="aboutContent" ContentPlaceHolderID="MainContent" runat="server">
    <h2>About</h2>
    <p>
        <%: ViewData["Message"] %>
    </p>
</asp:Content>

HomeController.cs

    public ActionResult Calculation(int value)
    {
        ViewData["Message"] = String.Format("{0} when squared produces {1}", value, Models.Mathematics.Square(value));
        return View();
    }

I pressed ctrl+5 and reached my homepage. Clicked Calculation page and received an error as

The parameters dictionary contains a null entry for parameter 'value' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Calculation(Int32)' in 'MathSolution.Controllers.HomeController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters

I tried passing http://localhost:3670/Home/Calculation/5 and also http://localhost:3670/Home/Calculation?value=5. I am still receiving error. I have also tried placing HttpPost on my Calculation method in HomeController.cs.

However, if in HomeContoller.cs file Calculation method I place a default value for parameter, then error disappears and I get the default value always.

To be honest, my tutorial is able to make it work but I am not sure whats wrong with my solution. Please let me know if you need any more info.

Sandy
  • 11,332
  • 27
  • 76
  • 122
  • Do I have to make any changes to IIS on my machine....or it doesn't matter...? – Sandy Jun 06 '13 at 18:54
  • Are you using MVC with WebForms controls? You might be better off going pure MVC with Razor. It looks like your routing might not be working properly. Check your `RouteConfig.cs`. – Dustin Kingen Jun 06 '13 at 18:55
  • 1
    Try changing param name to 'id', like as : public ActionResult Calculation(int id)... or add route in your global.asax.cs... http://stackoverflow.com/questions/155864/asp-net-mvc-passing-parameters-to-the-controller – Mate Jun 06 '13 at 18:57
  • I am using Webforms with MVC. I chose `ASP.Net MVC2 Web Application` in visual studio 2010. Well, I cant locate any routeconfig.cs file. And regarding Razor, hmmm, for now I am just following a tutorial to get basics. – Sandy Jun 06 '13 at 18:59
  • 1
    I think @Mate has it - the default routing in MVC uses "id" as the parameter name. You can see this in the Global.asax.cs file - from there, you can change it if you want, or add additional routes, but your simplest solution would just be to use "id". – Joe Enos Jun 06 '13 at 19:01
  • 1
    Using id is working. But when I click `Calculation` page why I am getting error? I can see in Global.asax.cs file that the parameter is set to Optional. Why am I not getting my default page? – Sandy Jun 06 '13 at 19:09
  • @Mate: It will be great if you can post the answer and allow me to accept it. Thanks a ton. – Sandy Jun 06 '13 at 19:10

2 Answers2

3

The simplest solution, and not necessarily the best: Try changing param name to 'id'

public ActionResult Calculation(int id)
        {
            ViewData["Message"] = String.Format("{0} when squared produces {1}", id, Models.Mathematics.Square(id));
            return View();
        }

Check this for a more precise explanation

Community
  • 1
  • 1
Mate
  • 4,976
  • 2
  • 32
  • 38
  • 1
    Placing `HttpPost` on top of Calculation methood solved the default page as well. Thanks again. – Sandy Jun 06 '13 at 19:17
0

You either need give your parameter a default value:

public ActionResult Calculation(int value = 0)

Or make it a nullable type:

public ActionResult Calculation(int? value)

Since it is an integer it cannot be null which is what it is when no value specified in the route.

ninlar
  • 1
  • Thanks Ninlar for your effort. But I think passing default value will just help in avoiding error as I will always get the result based on the default value itself as the real problem will still be there. – Sandy Jun 06 '13 at 19:35