0

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

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

when i post the after select gender and hobbies then nothing happen but in action there is code which return to show posted data. can u tell me why this happen?
I don't see anything obvious that would cause a completely blank page to be returned on post. However, at this point, as in the previous, you might simply be debugging dotnetfiddle rather than your own code. It's a neat idea, but it is, at the end of the day, a website trying to dynamically piece together an MVC app, compile it, and proxy back a result to your browser. It's never going to be perfect. Just create a local MVC project and run your code in Visual Studio. Then, you'll truly know that its your code and not some third-party website causing issues.
0

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

public class Test{}

it will compile then.

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.