locked
Requesting help [ Login , mvc ] RRS feed

  • Question

  • User382350815 posted

    Greetings, I've been using webforms for a long time and last week moved to mvc and I'm having trouble managing login.

    Httpget gets the data if I have it in login view , what I'm tring to do is get input data from _Layout.cshtml

    // User controll


    [HttpGet] public ActionResult LogIn() { // Pass values to user return View(); } [HttpPost] public ActionResult LogIn(Models.UserModel user) { //check if the model that view is sending back to us is valid if (ModelState.IsValid) { if (isValid(user.Email, user.Password)) { FormsAuthentication.SetAuthCookie(user.Email, false); return RedirectToAction("Index", "User"); } else { ModelState.AddModelError("", "Login Data is incorrect."); } } return View(user); }

    //Login View

    @model fusiondigentmvc4webapp.Models.UserModel
    @{
        ViewBag.Title = "Login";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    
    <h2>Login</h2>
    
    @using (Html.BeginForm())
    {
        @Html.ValidationSummary(true, "Login failed. CHeck your login details") //validate
        <div>
            <fieldset>
               <legend>Login</legend> 
                <div>@Html.LabelFor(u => u.Email)</div> <!--email is picking up from model-->
                <div>
                    @Html.TextBoxFor(u => u.Email)
                @Html.ValidationMessageFor(u => u.Email)
            </div>
    
            <div>@Html.LabelFor(u => u.Password)</div>
            <div>
                @Html.PasswordFor(u => u.Password)
                @Html.ValidationMessageFor(u => u.Password)
            </div>
            <input type="submit" value="Log In" />
        </fieldset>
        </div>




    // _Layout.cshtml

    
                            <!--Login------------------------------------->
                            <ul class="nav navbar-nav pull-left">
    
                                <li class="dropdown" id="menuLogin">
    
                                    <a class="dropdown-toggle" href="#" data-toggle="dropdown" id="navLogin">Login</a>
    
                                    <div class="dropdown-menu" style="padding:17px;">
    
                                        @using (Html.BeginForm())
                                        {
                                            @Html.ValidationSummary(true, "Login failed. CHeck your login details") 
                                                 <div>
                                                    <fieldset>
                                                         <legend>Login</legend>
                                                             <div>@Html.LabelFor(u => u.Email)</div> <!--email is picking up from model-->
                                                                  <div>
                                                                        @Html.TextBoxFor(u => u.Email)
                                                                        @Html.ValidationMessageFor(u => u.Email)
                                                                 </div>
                                                                 <div>  @Html.LabelFor(u => u.Password)</div>
                                                                 <div>
                                                                        @Html.PasswordFor(u => u.Password)
                                                                        @Html.ValidationMessageFor(u => u.Password)
                                                                 </div>
                                                                <input type="submit" value="Log In" />
                                                    </fieldset>
                                               </div>
                                        }
                                        <form><a href="#" title="Fast and free sign up!" id="btnNewUser" data-toggle="collapse" data-target="#formRegister">New User? Sign-up..</a></form>
                                               <form id="formRegister" class="form collapse">
                                                @using (Html.BeginForm())
                                                {
                                                @Html.ValidationSummary(true, "Create user failed, check your fields.")
                                                <div>
                                                    <fieldset>
                                                        <legend>Register</legend>
                                                        <div>@Html.LabelFor(u => u.Email)</div>
                                                        <div>
                                                            @Html.TextBoxFor(u => u.Email)
                                                            @Html.ValidationMessageFor(u => u.Email)
                                                        </div>
                                                        <div>@Html.LabelFor(u => u.Password)</div>
                                                        <div>
                                                            @Html.PasswordFor(u => u.Password)
                                                            @Html.ValidationMessageFor(u => u.Password)
                                                        </div>
                                                        <input type="submit" value="Create User" />
                                                    </fieldset>
                                                </div>
    }
                                        </form>
                                    </div>
    
                                </li>
                            </ul>
                            <!--Login-End---------------------------------->
    
    
    


    Any help is appreciated.

    P.S Registration also is in Registration view and not in the _layout

    Wednesday, April 23, 2014 9:29 AM

Answers

  • User-734925760 posted

    what I'm tring to do is get input data from _Layout.cshtml

    Hi,

    So far as I know, if you want to get data from _Layout.cshtml file, you must make sure those fields are existed in model class.

    I think your code is correct.

    There is a demo about your requirement, please refer to the link below:

    http://www.c-sharpcorner.com/UploadFile/raj1979/how-to-make-custom-login-register-and-logout-in-mvc-4-usin/

    Hope it's useful for you.

    Best Regards,

    Michelle Ge

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 24, 2014 2:41 AM
  • User382350815 posted

    Michelle Ge


    Thanks for your help, i've rad some info there and then made some researches and figured out.

    @views/shared/_Layout.cshtml

      @using (Html.BeginForm("Index","User"))

    At Html.BeginForm passed parameters of an actionName and controllerName

                            @using (Html.BeginForm("Index","User"))
                                        {
                                            @Html.ValidationSummary(true, "Login failed. CHeck your login details") 
                                                 <div>
                                                    <fieldset>
                                                         <legend>Login</legend>
                                                             <div>@Html.LabelFor(u => u.Email)</div> <!--email is picking up from model-->
                                                                  <div>
                                                                        @Html.TextBoxFor(u => u.Email)
                                                                        @Html.ValidationMessageFor(u => u.Email)
                                                                 </div>
                                                                 <div>  @Html.LabelFor(u => u.Password)</div>
                                                                 <div>
                                                                        @Html.PasswordFor(u => u.Password)
                                                                        @Html.ValidationMessageFor(u => u.Password)
                                                                 </div>
                                                                <input type="submit" value="Log In" />
                                                    </fieldset>
                                               </div>
          
    }

    Email and Password in UserModel

         public class UserModel
        {
            [Required]//Required field
            [EmailAddress]//Email Type  field
            [StringLength(50)]//Lenght 50
            [Display(Name="Email adress: ")]
            public string Email { get; set; }
            
    [Required]//Required field [DataType(DataType.Password)]//Password Type field [StringLength(50,MinimumLength=6)] [Display(Name="Password : ")] public string Password { get; set; } } }

    Then at user controller Get and Post the data from _layout.cshtml

    @model fusiondigentmvc4webapp.Models.UserModel
    @{
        ViewBag.Title = "Login";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    
    <h2>Login</h2>
    
    @using (Html.BeginForm())
    {
        @Html.ValidationSummary(true, "Login failed. CHeck your login details") //validate
        <div>
            <fieldset>
               <legend>Login</legend> 
                <div>@Html.LabelFor(u => u.Email)</div> <!--email is picking up from model-->
                <div>
                    @Html.TextBoxFor(u => u.Email)
                @Html.ValidationMessageFor(u => u.Email)
            </div>
    
            <div>@Html.LabelFor(u => u.Password)</div>
            <div>
                @Html.PasswordFor(u => u.Password)
                @Html.ValidationMessageFor(u => u.Password)
            </div>
            <input type="submit" value="Log In" />
        </fieldset>
        </div>
        
    }

    Thats it :)

    Thanks for help ^^!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 24, 2014 12:23 PM

All replies

  • User-734925760 posted

    what I'm tring to do is get input data from _Layout.cshtml

    Hi,

    So far as I know, if you want to get data from _Layout.cshtml file, you must make sure those fields are existed in model class.

    I think your code is correct.

    There is a demo about your requirement, please refer to the link below:

    http://www.c-sharpcorner.com/UploadFile/raj1979/how-to-make-custom-login-register-and-logout-in-mvc-4-usin/

    Hope it's useful for you.

    Best Regards,

    Michelle Ge

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 24, 2014 2:41 AM
  • User382350815 posted

    Michelle Ge


    Thanks for your help, i've rad some info there and then made some researches and figured out.

    @views/shared/_Layout.cshtml

      @using (Html.BeginForm("Index","User"))

    At Html.BeginForm passed parameters of an actionName and controllerName

                            @using (Html.BeginForm("Index","User"))
                                        {
                                            @Html.ValidationSummary(true, "Login failed. CHeck your login details") 
                                                 <div>
                                                    <fieldset>
                                                         <legend>Login</legend>
                                                             <div>@Html.LabelFor(u => u.Email)</div> <!--email is picking up from model-->
                                                                  <div>
                                                                        @Html.TextBoxFor(u => u.Email)
                                                                        @Html.ValidationMessageFor(u => u.Email)
                                                                 </div>
                                                                 <div>  @Html.LabelFor(u => u.Password)</div>
                                                                 <div>
                                                                        @Html.PasswordFor(u => u.Password)
                                                                        @Html.ValidationMessageFor(u => u.Password)
                                                                 </div>
                                                                <input type="submit" value="Log In" />
                                                    </fieldset>
                                               </div>
          
    }

    Email and Password in UserModel

         public class UserModel
        {
            [Required]//Required field
            [EmailAddress]//Email Type  field
            [StringLength(50)]//Lenght 50
            [Display(Name="Email adress: ")]
            public string Email { get; set; }
            
    [Required]//Required field [DataType(DataType.Password)]//Password Type field [StringLength(50,MinimumLength=6)] [Display(Name="Password : ")] public string Password { get; set; } } }

    Then at user controller Get and Post the data from _layout.cshtml

    @model fusiondigentmvc4webapp.Models.UserModel
    @{
        ViewBag.Title = "Login";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    
    <h2>Login</h2>
    
    @using (Html.BeginForm())
    {
        @Html.ValidationSummary(true, "Login failed. CHeck your login details") //validate
        <div>
            <fieldset>
               <legend>Login</legend> 
                <div>@Html.LabelFor(u => u.Email)</div> <!--email is picking up from model-->
                <div>
                    @Html.TextBoxFor(u => u.Email)
                @Html.ValidationMessageFor(u => u.Email)
            </div>
    
            <div>@Html.LabelFor(u => u.Password)</div>
            <div>
                @Html.PasswordFor(u => u.Password)
                @Html.ValidationMessageFor(u => u.Password)
            </div>
            <input type="submit" value="Log In" />
        </fieldset>
        </div>
        
    }

    Thats it :)

    Thanks for help ^^!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 24, 2014 12:23 PM