HI WELCOME TO KANSIRIS

Posting data from view to controller MVC

Leave a Comment
Step by step procedure to work:
1. Create MVC Project
2. Add 'LogOnModel.cs' & 'RegisterModel.cs' to your models folder.


Models:
public class LogOnModel
    {
        [Required]
        [Display(Name = "User name")]
        public string UserName { get; set; }

        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

        [Display(Name = "Remember me?")]
        public bool RememberMe { get; set; }
    }


    public class RegisterModel
    {
        [Required]
        [Display(Name = "User name")]
        public string UserName { get; set; }

        [Required]
        [DataType(DataType.EmailAddress)]
        [Display(Name = "Email address")]
        public string Email { get; set; }

        [Required]
        [ValidatePasswordLength]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirm password")]
        [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }
    }

3. Create new Controller called 'Account' or whatever name you want to give.
Controller:
/// <summary>
        /// Indexes this instance.
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }

 [HttpPost]
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                  // do your stuff like: save to database and redirect to required page.
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

 [HttpPost]
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                 // do your stuff like: save to database and redirect to required page.
            }

            return View();
        }

4.  Go to Views folder, create 'Account' folder and create Index.cshtml file.
 iews:
<!-- Index.cshtml -->

@using MvcForums.Models
@{
    ViewBag.Title = "Index";
}

<h2>Welcome</h2>

<div >
    
    <h2> Log on - Existing users</h2>
    @Html.Partial("_logOn",new LogOnModel())
    
    <br/>
    <h2> Register- New users</h2>
    @Html.Partial("_register",new RegisterModel())

</div>

<!-- _logOn.cshtml -->
@model MvcForums.Models.LogOnModel

@{
    using (Html.BeginForm("LogOn", "Account", FormMethod.Post))
    {
        <label>User Name</label>
        @Html.TextBoxFor(m => m.UserName)
        
        <br/>
        
        <label>Password</label>
        @Html.TextBoxFor(m => m.Password)

        <input type="submit" value="Login" />
        
    }
}

<!-- _register.cshtml -->
@model MvcForums.Models.RegisterModel

@{
    using (Html.BeginForm("Register", "Account", FormMethod.Post))
    {
        <label>User Name</label>
        @Html.TextBoxFor(m => m.UserName)
        
        <br/>
        
        <label>Email</label>
        @Html.TextBoxFor(m => m.Email)

        <br/>
        <label>Password</label>
        @Html.TextBoxFor(m => m.Password)
        
        <br/>
        <label>Confirm password</label>
        @Html.TextBoxFor(m => m.ConfirmPassword)

        <input type="submit" value="Register" />
    }
}

5. Go to Views-->Account  folder, create two partial views as '_register.cshtml' & '_logOn.cshtml'
6. Copy all the code I have written from each file ( Models, Controller and Views) and paste them into respective files.
7. Then run the application. or http://localhost:yourport/Account    will get the result.

Note:
You don't require to create each controller every action, instead you use can use one Controller ( like Account) to handle the different actions for the same functionality.
I have NOT created any Home Controller. I used only one Controller Account. Default action is Index
Hope this helps,

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.