Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Team

I have a problem, my create button does not seem to work well. What the logic is, simply when button is clicked to register my MVC. Meaning it must hit controller, model and view. What it does now its user validation only and it inherits this logic from login methods and need some help.

What I have tried:

C#
<pre> Controller:
     [HttpPost]
            [AllowAnonymous]
            [ValidateAntiForgeryToken]
            public ActionResult Create(CreateModel objSubmit)
            {
                ViewBag.Msg = "Details submitted successfully";
    
                return View(objSubmit);
            }
    
    // This is for login, and its hits this method each time.
     [HttpPost]
            [AllowAnonymous]
            [ValidateAntiForgeryToken]
            public ActionResult Login(Login login)
            {
                if (ModelState.IsValid)
                {
                    bool success = WebSecurity.Login(login.username, login.password, false);
                    var UserID = GetUserID_By_UserName(login.username);
                    var LoginType = GetRoleBy_UserID(Convert.ToString(UserID));
    
                    if (success == true)
                    {
                        if (string.IsNullOrEmpty(Convert.ToString(LoginType)))
                        {
                            ModelState.AddModelError("Error", "Rights to User are not Provide Contact to Admin");
                            return View(login);
                        }
                        else
                        {
                            Session["Name"] = login.username;
                            Session["UserID"] = UserID;
                            Session["LoginType"] = LoginType;
    
                            if (Roles.IsUserInRole(login.username, "Admin"))
                            {
                                return RedirectToAction("AdminDashboard", "Dashboard");
                            }
                            else
                            {
                                return RedirectToAction("UserDashboard", "Dashboard");
                            }
    
                        }
    
                    }
                    else
                    {
                        ModelState.AddModelError("Error", "Please enter valid Username and Password");
                        return View(login);
                    }
    
    
    
                }
                else
                {
                    ModelState.AddModelError("Error", "Please enter Username and Password");
                    return View(login);
                }
    
            }
    
    Model:
    namespace eNtsaPortalWebsiteProject.Models
    {
        public class CreateModel
        {
            [Required]
            [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
            [DataType(DataType.Password)]
            [Display(Name = "Password")]
            public string password { get; set; }
    
            [Required]
            public string username { get; set; }
        }
    }
    
    // View for login
    <div class="col-md-12 col-md-offset-10 col-xs-12">
                                <div class="loginPage panel-info">
                                    <span class="">class="glyphicon glyphicon-user"></span>
                                        @Html.PasswordFor(model => model.password, new { @class = "form-control text-center", autocomplete = "off" })
                                        @Html.ValidationMessageFor(model => model.password)
                                    </div>
                                </div>
    
                                <div class="form-group">
                                    <input id="BtnLogin" type="submit" class="btn btn-success btn-pressure" name="BtnLogin" value="Login" />
                                    <input id="BntCreate" type ="submit" class="btn btn-info btn-pressure" name="BtnCreate" value="Create" />
                                </div>
    
                            </div>
    View for creating user:
     <div class="col-md-12 col-md-offset-10 col-xs-12">
                            <div class="glyphicon-registration-mark">
                                <div class="">^__i class="glyphicon glyphicon-user">Username:</div>
                                <div class="form-group text-center">
                                    @Html.TextBoxFor(model=>model.username, new  {@class ="form-control text-center", automplete="off" })
                                    @Html.ValidationMessageFor(model=>model.username)
                                </div>
    
    
                                <div class="form-group">
                                    <span class="">^__i class="glyphicon glyphicon-user">Password:</span>
                                    @Html.PasswordFor(model=>model.password, new {@class = "form-control text-center", autocomplete="off" })
                                    @Html.ValidationMessageFor(model=>model.password)
                                </div>
                            </div>
                        </div>
    
                        <div class="form-group">
                            <div class="col-md-offset-2 col-md-10">
                                <input id="BtnSubmit" type="submit" class="btn btn-success btn-pressure" name="BtnSubmit" value="Submit"/>
                            </div>
                        </div>
                    
    
                }
            </div>
Posted
Updated 23-Jan-20 23:56pm
Comments
F-ES Sitecore 24-Jan-20 6:07am    
I don't understand the question, but if the problem is that you need different submit buttons to do different things then ensure each submit has a unique "name" and a "value" and have the names part of your model binding (in the model or as params to the action they are posted to) and the name that is not empty is the button that was clicked.

If you google for "c# mvc multiple submit buttons" you'll find some cleverer solutions to this such as custom attributes etc.
gcogco10 24-Jan-20 7:43am    
I have created a unique name and value for that handler, still not getting it right. What could be an issue? I think my controller does not work well. Each time click create, the validation it gets are from login actionResult method.

1 solution

There is no <form> in your code.
Wrap your form in <form></form> tags and set the action and method attributes.

Follow this link for more info: HTML Forms[^]
 
Share this answer
 
v2
Comments
gcogco10 24-Jan-20 6:02am    
No Mate there is, i checked both createUser.cshtml and login.cshtml.
jimmson 24-Jan-20 6:17am    
Then you haven't included it in your code you sent.
gcogco10 24-Jan-20 7:52am    
@jimmson, i did call it on my controller=>model=>view [HttpPost]
[AllowAnonymous]
public ActionResult Create(CreateModel submit)
{
ViewBag.Msg = "Details submitted successfully";

return View(submit);
}
jimmson 24-Jan-20 7:57am    
This is not what I'm talking about. Show us the code INCLUDING your form tags. There's no form in the code and by the type of questions I usually see from you here on CodeProject, I'm in real doubt you actually have any form. I'm just trying to help..
gcogco10 24-Jan-20 8:01am    
<






@using (Html.BeginForm())
{
@Html.AntiForgeryToken()


@Html.ValidationSummary()




Username:
@Html.TextBoxFor(model=>model.username, new {@class ="form-control text-center", automplete="off" })
@Html.ValidationMessageFor(model=>model.username)




Password:
@Html.PasswordFor(model=>model.password, new {@class = "form-control text-center", autocomplete="off" })
@Html.ValidationMessageFor(model=>model.password)











}

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