I am new to MVC. I am trying to create a simple user registration process
When I try to run the application I have the error HTTP 404. The resource cannot be found. Requested URL: /Mvc_Web_Project/Views/CreateNewUser/AddUser.cshtml
Solution i found online did not solve the issue
RouteConfig.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "CreateNewUser", action = "AddUser", id = UrlParameter.Optional }
);
}
Controller.cs
using System.Web.Mvc;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using Mvc_Web_Project.Models;
namespace Mvc_Web_Project.Controllers
{
public class CreateNewUserController : Controller
{
private SqlConnection con;
private void connection()
{
string constr = ConfigurationManager.ConnectionStrings["Molecular"].ToString();
con = new SqlConnection(constr);
}
// GET: CreateNewUser
public ActionResult AddUser(AddUserModel CreateNewUser)
{
//To Prevent firing validation error on first page Load
if (ModelState.IsValid)
{
connection();
SqlCommand com = new SqlCommand("AddUser", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@Email", CreateNewUser.Email);
com.Parameters.AddWithValue("@Password", CreateNewUser.Password);
com.Parameters.AddWithValue("@Type", CreateNewUser.Type);
con.Open();
int i = com.ExecuteNonQuery();
con.Close();
if (i >= 1)
{
ViewBag.Message = "New User Added Successfully";
}
}
ModelState.Clear();
return View();
}
}
}
cshtml
@model Mvc_Web_Project.Models.AddUserModel
@{
ViewBag.Title = "Create New User";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>AddUser</h2>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend> AddUserModel</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Password)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Password)
@Html.ValidationMessageFor(model => model.Password)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Type)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Type)
@Html.ValidationMessageFor(model => model.Type)
</div>
<p>
<input type="submit" value="Save details" />
</p>
</fieldset>
}
