Tuesday, April 8, 2014

Login page in Mvc 4 with Razor.

Model

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcApplication1.Models
{
    public class LoginMod
    {
        public string Username { get; set; }
        public string Password { get; set; }

    }
}

Controller

 public class HomeController : Controller
    {
        public ActionResult loginpage()
        {


            return View();
        }
        [HttpPost]
        public ActionResult loginpage(FormCollection form, string btnSubmit)
        {

            string uname = Convert.ToString(form["txtusername"]);
            string Password = Convert.ToString(form["txtPassword"]);
            return View("loginpage");
        }

        [HttpPost]
        public ActionResult loginpagebymodel(LoginMod ln, string btnSubmit)
        {
            string uname = ln.Username;
            string Password = ln.Password;

            return View("loginpage");
        }
}
}

View//

@model MvcApplication1.Models.LoginMod


@{
    ViewBag.Title = "loginpage";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>loginpage</h2>
@using (Html.BeginForm("loginpage", "Home", FormMethod.Post))
{
    <table>
        <tr>
            <td>UserName</td>
            <td>
                <input type="text" name="txtusername" />
            </td>
        </tr>
        <tr>
            <td>Password</td>
            <td>
                <input type="text" name="txtPassword" /></td>
        </tr>
        <tr>
            <td></td>
            <td>
                <input type="submit" name="btnSubmit" value="Submit" id="btnlogin" />
                <input type="submit" name="btnSubmit" value="Cancel" id="btncancel" />

            </td>
        </tr>
    </table>

}
@using (Html.BeginForm("loginpagebymodel", "Home", FormMethod.Post))
{
    <table>
        <tr>
            <td>UserName</td>
            <td>@Html.TextBoxFor(m => m.Username) </td>
        </tr>
        <tr>
            <td>Password</td>
            <td>@Html.TextBoxFor(m => m.Password) </td>
        </tr>
        <tr>
            <td></td>
            <td>
                <input type="submit" name="btnSubmit" value="Submit" /></td>
        </tr>
    </table>

}

No comments:

Post a Comment