Mou
Mou

Reputation: 16282

System.ArgumentNullException error in ASP.Net MVC

I am new to MVC. I wrote some code in dotnetfiddle.

I do not understand where I made the mistake which returns a compilation error. Please have a look at the url I gave and guide me.

My code

@model HelloWorldMvcApp.HomeController

@{
    Layout = null;
}

<!DOCTYPE html>
<!-- template from http://getbootstrap.com/getting-started -->

<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Bootstrap 101 Template</title>

    </head>

    <body>

<form id='your-form' action='@Url.Action("Index","HomeController")' method='post'>
     <b>Gender</b><br />
     <input type='radio' name='gender' value='Male' /> Male <br />
     <input type='radio' name='gender' value='Female' /> Female <br />
     <hr />
     <b>Hobbies</b><br />
     <input type='checkbox' name='hobbies' value='Reading' /> Reading <br />
     <input type='checkbox' name='hobbies' value='Sports' /> Sports <br />
     <input type='checkbox' name='hobbies' value='Movies' /> Movies <br />
     <input type='submit' value='Update Profile' />
</form> 
    </body>
</html>

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

namespace HelloWorldMvcApp
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(string gender, string[] hobbies)
        {
            // Example output 
            var output = String.Format("The user's gender is <b>{0}</b> and they enjoy <b>{1}</b>", gender, String.Join(",", hobbies));
            return Content(output);
        }
    }
}

thanks

Upvotes: 0

Views: 113

Answers (2)

Dandy
Dandy

Reputation: 2177

Just add some code in 'Model' frame of fiddle.

public class Test{}

it will compile then.

Upvotes: 0

Chris Pratt
Chris Pratt

Reputation: 239290

I haven't used dotnetfiddle much, but the error doesn't appear to be coming from your code, specifically. I added a basic model, and then the error went away:

namespace HelloWorldMvcApp
{
    public class Foo
    {
    }
}

It appears that dotnetfiddle requires something to be in the model section.

Also, in addition to the problem noted by @AndreiM, you've also set your view model to be your controller in the view. That's not right. If you have no model, then just remove the model declaration line.

Upvotes: 2

Related Questions