locked
New to MVC need help RRS feed

  • Question

  • User1266581083 posted

    Hey guys.

    I am new to MVC and i am in need of some help.

    I am trying to make a registration form by my self without having all of the automated things that asp.net mvc gives.

    I have made my database, and my controller and register form, but now i am wondering how do i 'send' the data from my cshtml page to the Action Result, and how will i obtain it.

    This is my Page:

    <form>
        <div class="speicalwriting">
        <div class="form-group">
            <label for="Username">Username</label>
            <input class="form-control" id="username" placeholder="Username">
        </div>
        <div class="form-group">
            <label for="EmailAddress">Email address</label>
            <input type="email" class="form-control" id="email" placeholder="Email">
        </div>
        <div class="form-group">
            <label for="Password">Password</label>
            <input type="password" class="form-control" id="password" placeholder="Password">
        </div>
        <div class="checkbox">
            <label>
                <input type="checkbox"> Remebmer me
            </label>
        </div>
            </div>
        <button type="submit" class="btn btn-primary">Register</button>
    </form>
    

    And this is my controller which has nothing in it atm.

            public ActionResult Register()
            {
                return View();
            }
            [HttpPost]
            public ActionResult Register(Users user)
            {
    
    
               
                return View();
            }
    

    Thanks for the help.

    Saturday, July 4, 2015 9:54 AM

Answers

  • User-782957977 posted

    Try this code

    MVC view

    @model Movies2.Models.UsersModel
    @{
        ViewBag.Title = "Register";
    }
    
    <h2>@ViewBag.Title.</h2>
    
    @using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
    {
        @Html.AntiForgeryToken()
        <h4>Create a new account.</h4>
        <hr />
        @Html.ValidationSummary("", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(m => m.Username, new { @class = "col-md-2 control-label" })
            <div class="col-md-10">
                @Html.TextBoxFor(m => m.Username, new { @class = "form-control" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
            <div class="col-md-10">
                @Html.PasswordFor(m => m.Password, new { @class = "form-control" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
            <div class="col-md-10">
                @Html.EditorFor(m => m.Email, new { @class = "form-control" })
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" class="btn btn-default" value="Register" id="cmdSubmit" />
            </div>
        </div>
        <button type="submit" class="btn btn-primary">Register</button>
    
    }

    Contrller

     public class AccountController : Controller
        {
    	public ActionResult Register()
            {
                return View();
            }
    
            [HttpPost]
            public ActionResult Register(UsersModel user)
            {
                return View(user));
            }
    
        }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, July 4, 2015 2:22 PM

All replies

  • User1992938117 posted

    I didn't find any Model in your project, You need to have User Model and bind their properties to html controls and using Form post submit the fom and you will get you data filled Model in Controller.

    See some sample projects:

    http://www.asp.net/mvc

    http://www.codeproject.com/Articles/486161/Creating-a-simple-application-using-MVC

    http://www.c-sharpcorner.com/UploadFile/cd3310/create-registration-form-using-Asp-Net-mvc-tools/

    http://dotnetawesome.blogspot.in/2014/03/how-to-create-user-registration-page-in-mvc4.html

    Saturday, July 4, 2015 10:07 AM
  • User1266581083 posted

    I already have a model.

    I just need to pass those objects and later on create the user in my controller, just trying to find out how to pass them into the controller.

    the all thing with the HttpPost :P.

    I'm not trying to use the simple way where the application makes it automatically like in those you showed me.

    I am trying to do it by my self.

    Saturday, July 4, 2015 10:18 AM
  • User1266581083 posted

    Update:

    I think i am going on the right route here.

    This is my new register.cshtml

    @model  System.Collections.Generic.IEnumerable<Movies2.Models.UsersModel>
    @{
        ViewBag.Title = "Register";
    
    }
    
    
    <h2>Register</h2>
    @using(Html.BeginForm())
    { 
    <form>
        <div class="speicalwriting">
        <div class="form-group">
            <label for="Username">Username</label>
            <input class="form-control" id="Username" placeholder="Username">
        </div>
        <div class="form-group">
            <label for="EmailAddress">Email address</label>
            <input type="email" class="form-control" id="Email" placeholder="Email">
        </div>
        <div class="form-group">
            <label for="Password">Password</label>
            <input type="password" class="form-control" id="Password" placeholder="Password">
        </div>
        <div class="checkbox">
            <label>
                <input type="checkbox"> Remebmer me
            </label>
        </div>
            </div>
        <button type="submit" value="Create" class="btn btn-primary">Register</button>
    </form>
    }

    And this is my AccountController:

            public ActionResult Register()
            {
                return View();
            }
            [HttpPost]
            public ActionResult Register(UsersModel user)
            {
                user = user;
    
               
                return View();
            }

    And this is my UserModel class:

        public class UsersModel
        {
    
            [Key]
            [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
            public int ID { get; set; }
            [StringLength(15, MinimumLength = 4, ErrorMessage = "Username must be 4 char long.")]
            [Required(ErrorMessage = "Please provide username", AllowEmptyStrings = false)]
            public string Username { get; set; }
            [Required(ErrorMessage = "Please provide password", AllowEmptyStrings = false)]
            [StringLength(20, MinimumLength = 8, ErrorMessage = "Password must be 8 char long.")]
            public string Password { get; set; }
            [Required(ErrorMessage = "Please provide email", AllowEmptyStrings = false)]
            public string Email { get; set; }
            public virtual UserGroups usergroup { get; set; }
    
        }

    As you can see in my controller i am doing user = user, to see if any data got passed, and everything is null.

    How to i define each textbox to what it needs to fill?

    thanks.

    Saturday, July 4, 2015 12:30 PM
  • User-474980206 posted

    first off, the browser will not post a value unless the form element has a name. you should read the spec:

         http://www.w3.org/TR/html401/interact/forms.html

    second, while not necessary, you should specify the action and method (set to post) of the form tag.

    third the MVC binder uses the element name for binding to the action parameters, so the element name should match the parameter names, or property names if the parameter is a model.

    Saturday, July 4, 2015 12:33 PM
  • User1266581083 posted

     binder uses the element name for binding to the action parameters, so the element name should match the parameter names, or property names if the parameter is a model.

    First of all thanks for responding!

    Second of all i am not quite sure about what you ment by that? can you show me an example so i'll fully understand?

    Thanks!

    Saturday, July 4, 2015 12:36 PM
  • User-782957977 posted

    Are you want submit data using Ajax post? You can use following code to submit data to controller.

    Controller

    [HttpPost]
    [AllowAnonymous]
            public ActionResult Register(Users user)
            {
                return View();
            }

     

    @model User
    @using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
    {
        @Html.AntiForgeryToken()
        <h4>Create a new account.</h4>
        <hr />
        @Html.ValidationSummary("", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(m => m.Login, new { @class = "col-md-2 control-label" })
            <div class="col-md-10">
                @Html.TextBoxFor(m => m.Login, new { @class = "form-control" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
            <div class="col-md-10">
                @Html.PasswordFor(m => m.Password, new { @class = "form-control" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
            <div class="col-md-10">
               @Html.TextBoxFor(m => m.Email, new { @class = "form-control" })        </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" class="btn btn-default" value="Register" id="cmdSubmit" />
            </div>
        </div>
    }
    
    @section Scripts {
        @Scripts.Render("~/bundles/jqueryval")
        <script>
            $(document).ready(function () {
                $("#cmdSubmit").click(function (event) {
                    event.preventDefault();
                    var user = { UserName: $("#Login").val(), "Password": $("#Password").val(),"Email":$("#Email").val() };
                  
                    $.ajax({
                        type: 'post',
                        url: '@Url.Action("Register")',
                        contentType: 'application/json; charset=utf-8',
                        dataType: 'json',
                        data: JSON.stringify(user),
                        success: function (result, status) {
                         },
                        error: function (ex) {
                            alert('Failed');
                        }
                    });
    
                });
            });
        </script>
       
    }
    
       
    }



    Saturday, July 4, 2015 1:05 PM
  • User1266581083 posted

    Thanks for the reply!

    Is there any other way than using ajax post?

    In addition i am getting this error on all of the m.objects.

    Error	2	'System.Collections.Generic.IEnumerable<Movies2.Models.UsersModel>' does not contain a definition for 'Username' and no extension method 'Username' accepting a first argument of type 'System.Collections.Generic.IEnumerable<Movies2.Models.UsersModel>' could be found (are you missing a using directive or an assembly reference?)	
    

    Saturday, July 4, 2015 1:41 PM
  • User-782957977 posted

    Try this code

    MVC view

    @model Movies2.Models.UsersModel
    @{
        ViewBag.Title = "Register";
    }
    
    <h2>@ViewBag.Title.</h2>
    
    @using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
    {
        @Html.AntiForgeryToken()
        <h4>Create a new account.</h4>
        <hr />
        @Html.ValidationSummary("", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(m => m.Username, new { @class = "col-md-2 control-label" })
            <div class="col-md-10">
                @Html.TextBoxFor(m => m.Username, new { @class = "form-control" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
            <div class="col-md-10">
                @Html.PasswordFor(m => m.Password, new { @class = "form-control" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
            <div class="col-md-10">
                @Html.EditorFor(m => m.Email, new { @class = "form-control" })
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" class="btn btn-default" value="Register" id="cmdSubmit" />
            </div>
        </div>
        <button type="submit" class="btn btn-primary">Register</button>
    
    }

    Contrller

     public class AccountController : Controller
        {
    	public ActionResult Register()
            {
                return View();
            }
    
            [HttpPost]
            public ActionResult Register(UsersModel user)
            {
                return View(user));
            }
    
        }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, July 4, 2015 2:22 PM