Frenz
Frenz

Reputation: 747

Passing data from view to controller MVC3

I recently came across the MVC tutorial on ASP.Net. I was trying to create a similar project (MVC3 Razor) where you can register the details. However, when I click the submit button the values of all properties in the parameter User is always null. I'm not able to figure out why the data is not getting passed from the view to the controller.

Even in the tutorial in the Create.cshtml they just use the Submit button as

<input type="submit" value="Create" />

and the code in Create Action in MoviesController.cs is as follows

[HttpPost]
        public ActionResult Create(Movie movie)
        {
            if (ModelState.IsValid)
            {
                db.Movies.Add(movie);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(movie);
        }

In this tutorial when I submit, I get the form data in parameter movie. However, In my sample project I get it as null. I'm new to MVC and it would be great if you could help me our with this. Please find my code below.

Register.cshtml - While creating this view I have selected "Create a strongly-typed view" option and the Scaffold template option as "Empty"

@model MvcRegister.Models.User
@using (Html.BeginForm())
{
<div>
<div>Name</div><div>@Html.EditorFor(model => model.Name)</div>
<div>Email</div><div>@Html.EditorFor(model => model.Email)</div>
<div>Phone</div><div>@Html.EditorFor(model => model.Phone)</div>
<div><input type="submit" value="Register" /></div>
</div>
}

RegisterController.cs

public class RegisterController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Register()
        {
            return View();
        } 

        [HttpPost]
        public ActionResult Register(User user)
        {
            return RedirectToAction("Index");  
        }
    }

User.cs

public class User
    {
        [Required]
        public string Name;

        [Required]
        public string Email;

        [Required]
        public string Phone;

    }

My project Source code available at http://www.filedropper.com/mvcregister and the Sample Movie project at http : //www.filedropper.com/mvcmovie

Upvotes: 1

Views: 206

Answers (1)

Saranga
Saranga

Reputation: 3228

Change your User class as below. You have missed getter and setter.

public class User
{
    [Required]
    public string Name { get; set; }

    [Required]
    public string Email { get; set; }

    [Required]
    public string Phone { get; set; }

    public string IP { get; set; }

    public string Password { get; set; }
}

And also you need to add following java-scripts to get the validation work.

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

Thanks!

Upvotes: 1

Related Questions