Thursday 14 July 2016

Posting data from view to controller MVC


Assume that you have Index view, which has two partial views. 1. LogOn 2. Register.
Each partial view has their own submit button and has single controller as "Account".
See the code below:
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; }
    }
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();
        }
Views:
<!-- 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" />
    }
}

No comments:

Post a Comment