This is my controller ode :
namespace VCS_MVC_project.Controllers
{
public class StudentsController : Controller
{
private VCSContext db = new VCSContext();
// GET: Students
public ActionResult Index()
{
var students = db.Students.Include(s => s.City);
return View(students.ToList());
}
// GET: Students/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Student student = db.Students.Find(id);
if (student == null)
{
return HttpNotFound();
}
return View(student);
}
// GET: Students/Create
public ActionResult Create()
{
ViewBag.Countries = db.Countries;
return View();
}
public JsonResult AjaxMethod(string type, int value)
{
switch (type)
{
case "ddlCountries":
var States = (from State in this.db.States
where State.CountryID == value
select new SelectListItem
{
Value = State.ID.ToString(),
Text = State.StateName
}).ToList();
return Json(States, JsonRequestBehavior.AllowGet);
case "ddlStates":
var Cities = (from City in this.db.Cities
where City.StateId == value
select new SelectListItem
{
Value = City.ID.ToString(),
Text = City.CityName
}).ToList();
return Json(Cities, JsonRequestBehavior.AllowGet);
}
return null;
}
// POST: Students/Create
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ID,Name,Dob,EmailID,Password,PhoneNO,Image,CityId,ZipCode,Address,Gender")] Student student, HttpPostedFileBase image)
{
try
{
if (ModelState.IsValid)
{
string hashedPassword = BCrypt.Net.BCrypt.HashPassword(student.Password);
student.Password = hashedPassword;
var filename = Path.GetFileName(image.FileName);
var path = Path.Combine(Server.MapPath("~/S_Images/" + image.FileName));
image.SaveAs(path);
student.Image = filename;
db.Students.Add(student);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.CityId = new SelectList(db.Cities, "ID", "CityName", student.CityId);
return View(student);
}
catch (Exception ex)
{
return RedirectToAction("Index", "Error");
}
}
// GET: Students/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Student student = db.Students.Find(id);
ViewBag.Countries = db.Countries;
//ViewBag.States = db.States;
if (student == null)
{
return HttpNotFound();
}
//ViewBag.CountryId = new SelectList(db.Countries, "ID", "CountryName", student.CityId.State.CountryID);
//ViewBag.StateId = new SelectList(db.Cities, "ID", "StateName", student.City.StateId);
ViewBag.CityId = new SelectList(db.Cities, "ID", "CityName", student.CityId);
return View(student);
}
// POST: Students/Edit/5
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "ID,Name,Dob,EmailID,Password,PhoneNO,Image,CityId,ZipCode,Address,Gender")] Student student)
{
if (ModelState.IsValid)
{
string hashedPassword = BCrypt.Net.BCrypt.HashPassword(student.Password);
student.Password = hashedPassword;
db.Entry(student).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.CountryId = new SelectList(db.Countries, "ID", "CountryName", student.City.State.CountryID);
ViewBag.StateId = new SelectList(db.Cities, "ID", "StateName", student.City.StateId);
ViewBag.CityId = new SelectList(db.Cities, "ID", "CityName", student.CityId);
return View(student);
}
// GET: Students/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Student student = db.Students.Find(id);
if (student == null)
{
return HttpNotFound();
}
return View(student);
}
// POST: Students/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Student student = db.Students.Find(id);
db.Students.Remove(student);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
this is my view code :
Create :
@model VCS_MVC_project.Models.Student
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Create</h2>
@using (Html.BeginForm("Create", "Students", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Student</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.Name, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Dob, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.Dob, new { @class = "form-control", type = "date" })
@Html.ValidationMessageFor(model => model.Dob, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.EmailID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.EmailID, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.EmailID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.PasswordFor(model => model.Password, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PhoneNO, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.PhoneNO, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.PhoneNO, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Image, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.Image, new { type = "file", @class = "form-control" })
@Html.ValidationMessageFor(model => model.Image, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.Address, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Gender, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.RadioButtonFor(model => model.Gender, "Male")Male
@Html.RadioButtonFor(model => model.Gender, "Female")Female
@Html.ValidationMessageFor(model => model.Gender, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.City.State.Country, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("countryid", new SelectList(ViewBag.Countries, "ID", "CountryName"), "---Select Country---", new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.City.State, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<select id="stateid" name="StateId" class="form-control">
<option value="">---Select State---</option>
</select>
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.City, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<select id="cityid" name="CityId" class="form-control">
<option value="">---Select City---</option>
</select>
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ZipCode, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.ZipCode, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ZipCode, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
@section scripts {
<script>
$(function () {
$("#countryid").change(function () {
$.ajax({
url: "/Students/AjaxMethod",
type: "get",
contentType: 'application/json',
data: { type: "ddlCountries", value: $("#countryid").val() },
success: function (result) {
//$("#City_StateId").html(result);
var options = '';
for (var i = 0; i < result.length; i++) { // Loop through the data & construct the options
options += '<option value="' + result[i].Value + '">' + result[i].Text + '</option>';
}
// Append to the html
$('#stateid').append(options);
},
error: function (ex) {
alert(ex)
}
});
});
});
$(function () {
$("#stateid").change(function () {
$.ajax({
url: "/Students/AjaxMethod",
type: "get",
contentType: 'application/json',
data: { type: "ddlStates", value: $("#stateid").val() },
success: function (result) {
//$("#City_StateId").html(result);
var options = '';
for (var i = 0; i < result.length; i++) { // Loop through the data & construct the options
options += '<option value="' + result[i].Value + '">' + result[i].Text + '</option>';
}
// Append to the html
$('#cityid').append(options);
},
error: function (ex) {
alert(ex)
}
});
});
});
</script>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
Edit:
@model VCS_MVC_project.Models.Student
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm("Edit", "Students", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Student</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.ID)
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Dob, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.Dob, new { @class = "form-control", type = "date" })
@Html.ValidationMessageFor(model => model.Dob, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.EmailID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.EmailID, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.EmailID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PhoneNO, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PhoneNO, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PhoneNO, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Image, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.Image, new { @class = "form-control", type = "file" })
@Html.ValidationMessageFor(model => model.Image, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Gender, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Gender, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Gender, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.City.State.Country, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@* @Html.DropDownList("CountryId",null, String.Empty, new { @class = "form-control" })*@
@Html.DropDownList("CountryId", new SelectList(ViewBag.Countries, "ID", "CountryName"), "---Select Country---", new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.City.State, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.DropDownList("StateId",null ,String.Empty, new { @class = "form-control" })*@
<select id="stateid" name="StateId" class="form-control">
<option value="StateId">---Select State---</option>
</select>
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.City, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<select id="cityid" name="CityId" class="form-control">
<option value="CityId">---Select City---</option>
</select>
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ZipCode, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ZipCode, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ZipCode, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
@section scripts {
<script>
$(function () {
$("#countryid").change(function () {
$.ajax({
url: "/Students/AjaxMethod",
type: "get",
contentType: 'application/json',
data: { type: "ddlCountries", value: $("#countryid").val() },
success: function (result) {
//$("#City_StateId").html(result);
var options = '';
for (var i = 0; i < result.length; i++) { // Loop through the data & construct the options
options += '<option value="' + result[i].Value + '">' + result[i].Text + '</option>';
}
// Append to the html
$('#stateid').append(options);
},
error: function (ex) {
alert(ex)
}
});
});
});
$(function () {
$("#stateid").change(function () {
$.ajax({
url: "/Students/AjaxMethod",
type: "get",
contentType: 'application/json',
data: { type: "ddlStates", value: $("#stateid").val() },
success: function (result) {
//$("#City_StateId").html(result);
var options = '';
for (var i = 0; i < result.length; i++) { // Loop through the data & construct the options
options += '<option value="' + result[i].Value + '">' + result[i].Text + '</option>';
}
// Append to the html
$('#cityid').append(options);
},
error: function (ex) {
alert(ex)
}
});
});
});
</script>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
Index:
@model IEnumerable<VCS_MVC_project.Models.Student>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Dob)
</th>
<th>
@Html.DisplayNameFor(model => model.EmailID)
</th>
@*<th>
@Html.DisplayNameFor(model => model.Password)
</th>*@
<th>
@Html.DisplayNameFor(model => model.PhoneNO)
</th>
<th>
@Html.DisplayNameFor(model => model.Image)
</th>
<th>
@Html.DisplayNameFor(model => model.Gender)
</th>
<th>
@Html.DisplayNameFor(model => model.Address)
</th>
<th>
@Html.DisplayNameFor(model => model.City.CityName)
</th>
<th>
@Html.DisplayNameFor(model => model.ZipCode)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Dob)
</td>
<td>
@Html.DisplayFor(modelItem => item.EmailID)
</td>
@*<td>
@Html.DisplayFor(modelItem => item.Password)
</td>*@
<td>
@Html.DisplayFor(modelItem => item.PhoneNO)
</td>
<td>
<img src="@Url.Content($"~/S_Images/{item.Image}")" alt="Image" width="100px" height="100px" />
</td>
<td>
@Html.DisplayFor(modelItem => item.Gender)
</td>
<td>
@Html.DisplayFor(modelItem => item.Address)
</td>
<td>
@Html.DisplayFor(modelItem => item.City.CityName)
</td>
<td>
@Html.DisplayFor(modelItem => item.ZipCode)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.ID }) |
@Html.ActionLink("Details", "Details", new { id = item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.ID })
</td>
</tr>
}
</table>
details:
@model VCS_MVC_project.Models.Student
@{
ViewBag.Title = "Details";
}
<h2>Details</h2>
<div>
<h4>Student</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.Name)
</dt>
<dd>
@Html.DisplayFor(model => model.Name)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Dob)
</dt>
<dd>
@Html.DisplayFor(model => model.Dob)
</dd>
<dt>
@Html.DisplayNameFor(model => model.EmailID)
</dt>
<dd>
@Html.DisplayFor(model => model.EmailID)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Password)
</dt>
<dd>
@Html.DisplayFor(model => model.Password)
</dd>
<dt>
@Html.DisplayNameFor(model => model.PhoneNO)
</dt>
<dd>
@Html.DisplayFor(model => model.PhoneNO)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Image)
</dt>
<dd>
<img src="@Url.Content($"~/S_Images/{Model.Image}")" alt="Image" width="100px" height="100px" />
</dd>
<dt>
@Html.DisplayNameFor(model => model.Gender)
</dt>
<dd>
@Html.DisplayFor(model => model.Gender)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Address)
</dt>
<dd>
@Html.DisplayFor(model => model.Address)
</dd>
<dt>
@Html.DisplayNameFor(model => model.City)
</dt>
<dd>
@Html.DisplayFor(model => model.CityId)
</dd>
<dt>
@Html.DisplayNameFor(model => model.ZipCode)
</dt>
<dd>
@Html.DisplayFor(model => model.ZipCode)
</dd>
</dl>
</div>
<p>
@Html.ActionLink("Edit", "Edit", new { id = Model.ID }) |
@Html.ActionLink("Back to List", "Index")
</p>
and how to edit uploaded image in this program