1

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

5
  • what is the problem u r facing? Commented Jul 1, 2014 at 9:12
  • use @Html.TextBoxFor() instead of @Html.EditorFor()... Commented Jul 1, 2014 at 9:13
  • @kartikeya-khosla : I tried using @Html.TextBoxFor() it still doesn't work. The problem is that if I enter the details in the form and submit, those details are not getting passed to the method Register(User user) and all the values for "user" is null. Commented Jul 1, 2014 at 9:16
  • ur code is correct there must be someother problem... Commented Jul 1, 2014 at 9:24
  • @kartikeya-khosla : ive uploaded the code at filedropper.com/mvcregister (My project) and filedropper.com/mvcmovie (Sample project) Commented Jul 1, 2014 at 10:01

1 Answer 1

1

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!

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

2 Comments

Strange. Because shouldn't by default in .Net 4.0 properties have get and set. I tried the following and the value gets assigned. public class UserX { [Required] public string Name; } and inside the register method UserX user1 = new UserX(); user1.Name = user.Name; The value gets assigned to user1.Name. Anyway. Thanks a lot for the help. The code works now.
Previously you have defined fields not properties. Please refer Member Design Guidelines (msdn.microsoft.com/en-us/library/ms229059%28v=vs.110%29.aspx). Happy to help you, Thanks!

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.