Guest User

Guest User

  • Tech Writer
  • 2.1k
  • 469.6k

How to check your crud if its working or not in sql db?

Feb 4 2020 12:35 PM
Hi Team
 
I have MVC, i am using core development and have class called "ApplicationDbContext". Now my question is how do i check if my crud operation working? Meaning i cant seem to see the operation from the back end. Let me show you my Controller method for Reg, Model(Registration) and View. Can you mates at least tell me where to look? It does not do any crud operation to my tables on the DB. I havent configure yet my Web.Config file for connectionString. Do i also need to do this? Could this be reason, please assist me further.
  1. // Controller.cs  
  2.         [HttpPost]  
  3.         [AllowAnonymous]  
  4.         [ValidateAntiForgeryToken]  
  5.         public async Task<ActionResult> Register(RegisterViewModel model)  
  6.         {  
  7.             if (ModelState.IsValid)  
  8.             {  
  9.                 var user = new ApplicationUser() { UserName = model.UserName };  
  10.                 user.Email = model.Email;  
  11.                 user.ConfirmedEmail = false;  
  12.                 var result = await UserManager.CreateAsync(user, model.Password);  
  13.                 if (result.Succeeded)  
  14.                 {  
  15.                     System.Net.Mail.MailMessage m = new System.Net.Mail.MailMessage(  
  16.                         new System.Net.Mail.MailAddress("[email protected]""Web Registration"),  
  17.                         new System.Net.Mail.MailAddress(user.Email));  
  18.                     m.Subject = "Email confirmation";  
  19.                     m.Body = string.Format("Dear {0}<BR/>Thank you for your registration, please click on the below link to complete your registration: <a href=\"{1}\" title=\"User Email Confirm\">{1}</a>", user.UserName, Url.Action("ConfirmEmail""Account"new { Token = user.Id, Email = user.Email }, Request.Url.Scheme));  
  20.                     m.IsBodyHtml = true;  
  21.                     System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.mydomain.com");  
  22.                     smtp.Credentials = new System.Net.NetworkCredential("[email protected]""password");  
  23.                     smtp.EnableSsl = true;  
  24.                     smtp.Send(m);  
  25.                     return RedirectToAction("Confirm""Account"new { Email = user.Email });  
  26.                 }  
  27.                 else  
  28.                 {  
  29.                     AddErrors(result);  
  30.                 }  
  31.             }  
  32.   
  33.             // If we got this far, something failed, redisplay form  
  34.             return View(model);  
  35.         }  
  36.   
  37. // Model class  
  38.  public class RegisterViewModel  
  39.     {  
  40.         [Required]  
  41.         [Display(Name = "User name")]  
  42.         public string UserName { get; set; }  
  43.   
  44.   
  45.   
  46.         [Required]  
  47.         [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]  
  48.         [DataType(DataType.Password)]  
  49.         [Display(Name = "Password")]  
  50.         public string Password { get; set; }  
  51.   
  52.   
  53.   
  54.         [DataType(DataType.Password)]  
  55.         [Display(Name = "Confirm password")]  
  56.         [Compare("Password", ErrorMessage = "The password and confirmation do not match.")]  
  57.         public string ConfirmPassword { get; set; }  
  58.   
  59.         [Required]  
  60.         [EmailAddress]  
  61.         [Display(Name = "Email")]  
  62.         public string Email { get; set; }  
  63.          
  64.     }  
  65.   
  66. // View cshtml  
  67. @model ContentManagementSystem.Models.RegisterViewModel  
  68.   
  69. @{  
  70.     ViewBag.Title = "Register";  
  71. }  
  72.   
  73.   
  74. <h2>@ViewBag.Title.</h2>  
  75.   
  76. @using (Html.BeginForm("Register""Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))  
  77. {  
  78.     @Html.AntiForgeryToken()  
  79.     <h4>Create a new account.</h4>  
  80.     <hr />  
  81.     @Html.ValidationSummary()  
  82.     <div class="form-group">  
  83.         @Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label" })  
  84.         <div class="col-md-10">  
  85.             @Html.TextBoxFor(m => m.UserName, new { @class = "form-control" })  
  86.         </div>  
  87.     </div>  
  88.     <div class="form-group">  
  89.         @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })  
  90.         <div class="col-md-10">  
  91.             @Html.PasswordFor(m => m.Password, new { @class = "form-control" })  
  92.         </div>  
  93.     </div>  
  94.     <div class="form-group">  
  95.         @Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" })  
  96.         <div class="col-md-10">  
  97.             @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" })  
  98.         </div>  
  99.     </div>  
  100.     <div class="form-group">  
  101.         @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })  
  102.         <div class="col-md-10">  
  103.             @Html.TextBoxFor(m => m.Email, new { @class = "form-control" })  
  104.         </div>  
  105.     </div>  
  106.     <div class="form-group">  
  107.         <div class="col-md-offset-2 col-md-10">  
  108.             <input type="submit" class="btn btn-info" value="Register" />  
  109.         </div>  
  110.     </div>  
  111. }  
  112.   
  113. @section Scripts {  
  114.     @Scripts.Render("~/bundles/jqueryval")  
  115. }  
 

Answers (1)