ASP.Net MVC中的System.ArgumentNullException错误

时间:2015-09-02 18:15:01

标签: asp.net-mvc

我是MVC的新手。我在dotnetfiddle中写了一些代码。

我不明白我在哪里犯了返回编译错误的错误。请看看我给的网址并指导我。

我的代码

@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);
        }
    }
}

感谢

2 个答案:

答案 0 :(得分:2)

我没有使用过dotnetfiddle,但错误似乎并非来自您的代码,具体而言。我添加了一个基本模型,然后错误就消失了:

namespace HelloWorldMvcApp
{
    public class Foo
    {
    }
}

似乎dotnetfiddle需要某些在模型部分。

此外,除了@AndreiM注意到的问题之外,您还将视图模型设置为视图中的控制器。那是不对的。如果您没有模型,则只需删除模型声明行。

答案 1 :(得分:0)

只需在小提琴的“模型”框架中添加一些代码即可。

public class Test{}

它将编译然后。