Guest User

Guest User

  • Tech Writer
  • 2.1k
  • 473.3k

How to make your controller functional to model and view?

Jan 24 2020 3:13 PM
Hi Team
 
I want to make my controller functional to my model and view, that the application does correcttly it validates user inputs only. I want my create button, to create new user if not exist to my database and cant seem to make this logic right and need some help and guidance. 
 
  1. Controller:  
  2.     [HttpPost]  
  3.            [AllowAnonymous]  
  4.            [ValidateAntiForgeryToken]  
  5.            public ActionResult Create(CreateModel objSubmit)  
  6.            {  
  7.                ViewBag.Msg = "Details submitted successfully";  
  8.      
  9.                return View(objSubmit);  
  10.            }  
  11.      
  12.    // This is for login, and its hits this method each time.  
  13.     [HttpPost]  
  14.            [AllowAnonymous]  
  15.            [ValidateAntiForgeryToken]  
  16.            public ActionResult Login(Login login)  
  17.            {  
  18.                if (ModelState.IsValid)  
  19.                {  
  20.                    bool success = WebSecurity.Login(login.username, login.password, false);  
  21.                    var UserID = GetUserID_By_UserName(login.username);  
  22.                    var LoginType = GetRoleBy_UserID(Convert.ToString(UserID));  
  23.      
  24.                    if (success == true)  
  25.                    {  
  26.                        if (string.IsNullOrEmpty(Convert.ToString(LoginType)))  
  27.                        {  
  28.                            ModelState.AddModelError("Error""Rights to User are not Provide Contact to Admin");  
  29.                            return View(login);  
  30.                        }  
  31.                        else  
  32.                        {  
  33.                            Session["Name"] = login.username;  
  34.                            Session["UserID"] = UserID;  
  35.                            Session["LoginType"] = LoginType;  
  36.      
  37.                            if (Roles.IsUserInRole(login.username, "Admin"))  
  38.                            {  
  39.                                return RedirectToAction("AdminDashboard""Dashboard");  
  40.                            }  
  41.                            else  
  42.                            {  
  43.                                return RedirectToAction("UserDashboard""Dashboard");  
  44.                            }  
  45.      
  46.                        }  
  47.      
  48.                    }  
  49.                    else  
  50.                    {  
  51.                        ModelState.AddModelError("Error""Please enter valid Username and Password");  
  52.                        return View(login);  
  53.                    }  
  54.      
  55.      
  56.      
  57.                }  
  58.                else  
  59.                {  
  60.                    ModelState.AddModelError("Error""Please enter Username and Password");  
  61.                    return View(login);  
  62.                }  
  63.      
  64.            }  
  65.      
  66.    Model:  
  67.    namespace eNtsaPortalWebsiteProject.Models  
  68.    {  
  69.        public class CreateModel  
  70.        {  
  71.            [Required]  
  72.            [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]  
  73.            [DataType(DataType.Password)]  
  74.            [Display(Name = "Password")]  
  75.            public string password { getset; }  
  76.      
  77.            [Required]  
  78.            public string username { getset; }  
  79.        }  
  80.    }  
  81.      
  82.    // View for login  
  83.    <div class="col-md-12 col-md-offset-10 col-xs-12">  
  84.                                <div class="loginPage panel-info">  
  85.                                    <span class=""><i class="glyphicon glyphicon-user">Username:</i></span>  
  86.                                    <div class="form-group text-center">  
  87.                                        @Html.TextBoxFor(model => model.username, new { @class = "form-control text-center", autocomplete = "off" })  
  88.                                        @Html.ValidationMessageFor(model => model.username)  
  89.                                    </div>  
  90.      
  91.      
  92.      
  93.      
  94.                                    <div class="form-group">  
  95.                                        <span class=""><i class="glyphicon glyphicon-user">Password:</i></span>  
  96.                                        @Html.PasswordFor(model => model.password, new { @class = "form-control text-center", autocomplete = "off" })  
  97.                                        @Html.ValidationMessageFor(model => model.password)  
  98.                                    </div>  
  99.                                </div>  
  100.      
  101.                                <div class="form-group">  
  102.                                    <input id="BtnLogin" type="submit" class="btn btn-success btn-pressure" name="BtnLogin" value="Login" />  
  103.                                    <input id="BntCreate" type ="submit" class="btn btn-info btn-pressure" name="BtnCreate" value="Create" />  
  104.                                </div>  
  105.      
  106.                            </div>  
  107.    View for creating user:  
  108.     <div class="col-md-12 col-md-offset-10 col-xs-12">  
  109.                            <div class="glyphicon-registration-mark">  
  110.                                <div class=""><i class="glyphicon glyphicon-user">Username:</i></div>  
  111.                                <div class="form-group text-center">  
  112.                                    @Html.TextBoxFor(model=>model.username, new  {@class ="form-control text-center", automplete="off" })  
  113.                                    @Html.ValidationMessageFor(model=>model.username)  
  114.                                </div>  
  115.      
  116.      
  117.                                <div class="form-group">  
  118.                                    <span class=""><i class="glyphicon glyphicon-user">Password:</i></span>  
  119.                                    @Html.PasswordFor(model=>model.password, new {@class = "form-control text-center", autocomplete="off" })  
  120.                                    @Html.ValidationMessageFor(model=>model.password)  
  121.                                </div>  
  122.                            </div>  
  123.                        </div>  
  124.      
  125.                        <div class="form-group">  
  126.                            <div class="col-md-offset-2 col-md-10">  
  127.                                <input id="BtnSubmit" type="submit" class="btn btn-success btn-pressure" name="BtnSubmit" value="Submit"/>  
  128.                            </div>  
  129.                        </div>  
  130.                      
  131.      
  132.                }  
  133.            </div>