Click here to Skip to main content
15,891,409 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have three buttons in one view and if i submit second button it's not submitting.
please help me.

View :

@model sb_admin_2.Web.Models.Account.LoginAuthontication


HTML
@using (Html.BeginForm("HomePage", "UserLoginhome", FormMethod.Post, null))
        {
            @Html.AntiForgeryToken()
            <div class="row ml0 mr0 login-bg">
                <div class="container">
                    <div class="col-md-4 login1">
                        <h3 class="h3 text-center">Corporate Login</h3>
                        <form class="form-horizontal">
                            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                            <div>
                                <span class="text-danger"> @TempData["CorparateError"] </span>
                            </div>
                            <div class="form-group">
                                @Html.LabelFor(model => model.CorpLogin.CorpEmailId, new { @class = "control-label col-md-3", })
                                <div class="col-md-9">
                                    @Html.EditorFor(model => model.CorpLogin.CorpEmailId, new { htmlAttributes = new { @class = "form-control" } })
                                    @Html.ValidationMessageFor(model => model.CorpLogin.CorpEmailId, "", new { @class = "text-danger" })
                                </div>
                            </div>

                            <div class="form-group">
                                @Html.LabelFor(model => model.CorpLogin.CorpPassword, new { @class = "control-label col-md-3", })
                                <div class="col-md-9">
                                    @Html.EditorFor(model => model.CorpLogin.CorpPassword, new { htmlAttributes = new { @class = "form-control" } })
                                    @Html.ValidationMessageFor(model => model.CorpLogin.CorpPassword, "", new { @class = "text-danger" })
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-md-offset-3 col-md-9">
                                    <div class="checkbox">
                                        @*<label><input type="checkbox"> Remember me</label>*@
                                        @Html.CheckBoxFor(m => m.CorpLogin.CorpRemeberMe)
                                        @Html.DisplayNameFor(m => m.CorpLogin.CorpRemeberMe)
                                    </div>
                                </div>
                            </div>
                            
                            <div class="form-group">
                                <div class="col-md-offset-3 col-md-9">
                                    <button type="submit" class="btn btn-primary wide" name="Command" value="CorparateLogin">Login</button>
                                </div>
                            </div>
                        </form>

                    </div>
                    
                    <div class="col-md-4 login1">
                        <h3 class="h3 text-center">Individual Login</h3>
                        <form class="form-horizontal">
                            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                            <div>
                                <span class="text-danger"> @TempData["IndividualError"]</span>
                            </div>
                            <div class="form-group">
                                @Html.LabelFor(model => model.IndLogin.IndEmailId, new { @class = "control-label col-md-3", })
                                <div class="col-md-9">
                                    @Html.EditorFor(model => model.IndLogin.IndEmailId, new { htmlAttributes = new { @class = "form-control" } })
                                    @Html.ValidationMessageFor(model => model.IndLogin.IndEmailId, "", new { @class = "text-danger" })
                                </div>
                            </div>

                            <div class="form-group">
                                @Html.LabelFor(model => model.IndLogin.IndPassword, new { @class = "control-label col-md-3", })
                                <div class="col-md-9">
                                    @Html.EditorFor(model => model.IndLogin.IndPassword, new { htmlAttributes = new { @class = "form-control" } })
                                    @Html.ValidationMessageFor(model => model.IndLogin.IndPassword, "", new { @class = "text-danger" })
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-md-offset-3 col-md-9">
                                    <div class="checkbox">
                                        @Html.CheckBoxFor(m => m.IndLogin.IndRemeberMe)
                                        @Html.DisplayNameFor(m => m.IndLogin.IndRemeberMe)
                                    </div>
                                </div>
                            </div>
                            
                            <div class="form-group">
                                <div class="col-md-offset-3 col-md-9">
                                    <button type="submit" class="btn btn-primary wide" name="Command" value="IndividualLogin">Login</button>
                                </div>
                            </div>
                        </form>
                    </div>

                    <div class="col-md-4 login1">
                        <h3 class="h3 text-center">Candidate Login</h3>
                        <form class="form-horizontal">
                            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                            <div>
                                <span class="text-danger"> @TempData["CandidateError"] </span>
                            </div>
                            <div class="form-group">
                                @Html.LabelFor(model => model.CandLogin.CandUserId, new { @class = "control-label col-md-3", })
                                <div class="col-md-9">
                                    @Html.EditorFor(model => model.CandLogin.CandUserId, new { htmlAttributes = new { @class = "form-control" } })
                                    @Html.ValidationMessageFor(model => model.CandLogin.CandUserId, "", new { @class = "text-danger" })
                                </div>
                            </div>

                            <div class="form-group">
                                @Html.LabelFor(model => model.CandLogin.CandPassword, new { @class = "control-label col-md-3", })
                                <div class="col-md-9">
                                    @Html.EditorFor(model => model.CandLogin.CandPassword, new { htmlAttributes = new { @class = "form-control" } })
                                    @Html.ValidationMessageFor(model => model.CandLogin.CandPassword, "", new { @class = "text-danger" })
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-md-offset-3 col-md-9">
                                    <div class="checkbox">
                                        @Html.CheckBoxFor(m => m.CandLogin.CandRemeberMe)
                                        @Html.DisplayNameFor(m => m.CandLogin.CandRemeberMe)
                                    </div>
                                </div>
                            </div>
                            
                            <div class="form-group">
                                <div class="col-md-offset-3 col-md-9">
                                    <button type="submit" class="btn btn-primary wide" name="Command" value="CandidateLogin">Login</button>
                                </div>
                            </div>
                        </form>
                    </div>

                </div>
            </div>
        <!-- Login form end -->
        }



C#
public ActionResult HomePage()
        {
            LoginAuthontication model = new LoginAuthontication();            
            ViewBag.message = Session["Message"];
            return View(model);
        }
        [HttpPost]
        public ActionResult HomePage(LoginAuthontication model, string Command)
        {
            if (Command == "CorparateLogin")
            {
                var corpLogin = db.ALOrganizationUsers.FirstOrDefault(m => m.UserEmailID == model.CorpLogin.CorpEmailId && m.UserPassword == model.CorpLogin.CorpPassword);
                if (model.CorpLogin.CorpRemeberMe == true)
                {
                    HttpCookie cookie = new HttpCookie("YourAppLogin");
                    cookie.Values.Add("username", corpLogin.UserEmailID);
                    cookie.Expires = DateTime.Now.AddDays(15);
                    Response.Cookies.Add(cookie);
                }

                if (corpLogin != null)
                {
                    return RedirectToAction("Index", "UserLoginhome");
                }
                else
                {
                    TempData["CorparateError"] = "Login data is incorrect";
                   // ModelState.AddModelError("", "Login data is incorrect!");
                }
            }
            else if (Command == "IndividualLogin")
            {
                var indLogin = db.ALLoginIndividuals.FirstOrDefault(m => m.UserEmailID == model.IndLogin.IndEmailId && m.UserPassword == model.IndLogin.IndPassword);

                if (model.IndLogin.IndRemeberMe == true)
                {
                    HttpCookie cookie = new HttpCookie("YourAppLogin");
                    cookie.Values.Add("username", indLogin.UserEmailID);
                    cookie.Expires = DateTime.Now.AddDays(15);
                    Response.Cookies.Add(cookie);
                }
                if (indLogin != null)
                {
                    return RedirectToAction("Index", "UserLoginhome");
                }
                else
                {
                    TempData["IndividualError"] = "Login data is incorrect";
                   // ModelState.AddModelError("", "Login data is incorrect!");
                }
            }
            else
            {
                var candLogin = db.ALUserDetails.FirstOrDefault(m => m.UserID == model.CandLogin.CandUserId && m.Pword == model.CandLogin.CandPassword);

                //if (model.CorpRemeberMe == true)
                //{
                //    HttpCookie cookie = new HttpCookie("YourAppLogin");
                //    cookie.Values.Add("userid", Convert.ToInt32(candLogin.UserID));
                //    cookie.Expires = DateTime.Now.AddDays(15);
                //    Response.Cookies.Add(cookie);
                //}
                if (candLogin != null)
                {
                    //Session["TestId"] = model.TestId;
                    //Session["CompanyId"] = candLogin.CompanyID;
                    //Session["UserId"] = login.UserID;

                    return RedirectToAction("ALLBulkTestUserDetails", "UserLoginhome");
                }
                else
                {
                    TempData["CandidateError"] = "Login data is incorrect";
                    //ModelState.AddModelError("", "Login data is incorrect!");
                }
            }
            //}
            return View();
        }



C#
public class LoginAuthontication
    {
        public CorprateLogin CorpLogin { get; set; }
        public IndividualLogin IndLogin { get; set; }
        public CandidateLogin CandLogin { get; set; }
    }
    public class CorprateLogin
    {
        [Required(ErrorMessage = "*Please Enter Email ID")]
        [Display(Name = "Email ID")]
        public string CorpEmailId { get; set; }
        [Required(ErrorMessage = "*Please Enter Password")]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string CorpPassword { get; set; }
        [Display(Name = "Remember me?")]
        public bool CorpRemeberMe { get; set; }
    }
    public class IndividualLogin
    {
        [Required(ErrorMessage = "*Please Enter Email ID")]
        [Display(Name = "Email ID")]
        public string IndEmailId { get; set; }
        [Required(ErrorMessage = "*Please Enter Password")]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string IndPassword { get; set; }
        [Display(Name = "Remember me?")]
        public bool IndRemeberMe { get; set; }
    }
    public class CandidateLogin
    {
        [Required(ErrorMessage = "*Please Enter Email ID")]
        [Display(Name = "Email ID")]
        public decimal CandUserId { get; set; }
        [Required(ErrorMessage = "*Please Enter Password")]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string CandPassword { get; set; }
        [Display(Name = "Remember me?")]
        public bool CandRemeberMe { get; set; }
    }
Posted
Comments
[no name] 14-Dec-15 4:15am    
Except second button other two buttons are working fine?

Moreover debug the code by setting a breakpoint in HomePage(). Just check what is the command value you are getting when you are clicking Login button.
F-ES Sitecore 14-Dec-15 4:34am    
Define "not working". Does the form not get submitted? Does it get submitted but go to the wrong action? Does it go to the right action but not do what you want it to do?

Use a tool like Fiddler or the browser's dev tools to examine the http traffic between the browser and your site, it might help you see what's going on.

1 solution

Please remove the
C#
<form class="form-horizontal"></form>
and corresponding
C#

from your code

C#
@using (Html.BeginForm("HomePage", "UserLoginhome", FormMethod.Post, null))


will create tag for the generated HTML page. There can be only one form tag in ASP.Net html page

I have modefied the code and it is working

<pre lang="c#">

@model WebApplication4.Models.LoginAuthontication

@using (Html.BeginForm("UserLoginhome", "Home", FormMethod.Post))
{
@Html.AntiForgeryToken()


}
 
Share this answer
 
Comments
F-ES Sitecore 14-Dec-15 6:41am    
"There can be only one form tag in ASP.Net html page"

You can have as many forms as you want, however you can't nest forms which is essentially what the OP is doing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900