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

My MVC app does not do any crud operation to the database and dont know what could be the issue, i have not configured yet my Web.config file so far for connectionString. Could be this the reason, i have folder called _AppStart and loaded my sql query with tables there but its not doing neither, please help mates.

What I have tried:

// Controller.cs
// POST: /Account/Register
       [HttpPost]
       [AllowAnonymous]
       [ValidateAntiForgeryToken]
       public async Task<ActionResult> Register(RegisterViewModel model)
       {
           if (ModelState.IsValid)
           {
               var user = new ApplicationUser() { UserName = model.UserName };
               user.Email = model.Email;
               user.ConfirmedEmail = false;
               var result = await UserManager.CreateAsync(user, model.Password);
               if (result.Succeeded)
               {
                   System.Net.Mail.MailMessage m = new System.Net.Mail.MailMessage(
                       new System.Net.Mail.MailAddress("ggcobani@gmail.com", "Web Registration"),
                       new System.Net.Mail.MailAddress(user.Email));
                   m.Subject = "Email confirmation";
                   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));
                   m.IsBodyHtml = true;
                   System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.mydomain.com");
                   smtp.Credentials = new System.Net.NetworkCredential("ggcobani@gmail.com", "password");
                   smtp.EnableSsl = true;
                   smtp.Send(m);
                   return RedirectToAction("Confirm", "Account", new { Email = user.Email });
               }
               else
               {
                   AddErrors(result);
               }
           }

           // If we got this far, something failed, redisplay form
           return View(model);
       }


// Model.cs
public class RegisterViewModel
   {
       [Required]
       [Display(Name = "User name")]
       public string UserName { get; set; }



       [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; }



       [DataType(DataType.Password)]
       [Display(Name = "Confirm password")]
       [Compare("Password", ErrorMessage = "The password and confirmation do not match.")]
       public string ConfirmPassword { get; set; }

       [Required]
       [EmailAddress]
       [Display(Name = "Email")]
       public string Email { get; set; }

   }


//View.cshtml
@model ContentManagementSystem.Models.RegisterViewModel

@{
    ViewBag.Title = "Register";
}


<h2>@ViewBag.Title.</h2>

@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
    @Html.AntiForgeryToken()
    <h4>Create a new account.</h4>
    <hr />
    @Html.ValidationSummary()
    <div class="form-group">
        @Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.UserName, new { @class = "form-control" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.PasswordFor(m => m.Password, new { @class = "form-control" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" class="btn btn-info" value="Register" />
        </div>
    </div>
}

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
Posted
Updated 4-Feb-20 12:34pm
Comments
Richard MacCutchan 4-Feb-20 14:50pm    
I don't see any SQL code there, is it automatically handled by the framework?
gcogco10 5-Feb-20 3:23am    
Its handled by the framework and now i am getting an error.

1 solution

Original Poster:
My MVC app does not do any crud operation to the database and dont know what could be the issue
i have not configured yet my Web.config file so far for connectionString
Could be this the reason

Yes. A connection string is needed to know what server and database to connect to.

And either your (Controller) Action will also need to have a call to save the user model to the DB via the connection string
 
Share this answer
 
Comments
gcogco10 5-Feb-20 1:05am    
Is there any default example, i do have an idea but just need to be sure.

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