eCommerce Website Part 11 Country Table
eCommerce Website Part 11 Country Table
Hi, Dear's here we learn how to implement eCommerce Website in Visual Studio using C# ASP.NET MVC. Ilyasoft software company provide full project step by step training on our YouTube Channel ilyasoft software company so now subscribe, share and like for more project base tutorials
In this video we are going to implement CountryTable form for more details click here:
watch vedio First Download Materials Download Materials.
Create BasicConfigurationController and create one by one below action's:
> Country Table Action Code
Action Code :
public ActionResult Countries_List()
{
var list = new List<CountryMV>();
foreach (var country in DB.CountryTables.ToList())
{
list.Add(new CountryMV()
{
CountryID = country.CountryID,
CountryTitle = country.CountryTitle
});
}
return View(list);
}
public ActionResult NewCountry()
{
return View(new CountryMV());
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult NewCountry(CountryMV countryMV)
{
if (ModelState.IsValid)
{
var check_exist = DB.CountryTables.Where(a => a.CountryTitle == countryMV.CountryTitle.Trim()).FirstOrDefault();
if (check_exist == null)
{
var country = new CountryTable();
country.CountryTitle = countryMV.CountryTitle;
DB.CountryTables.Add(country);
DB.SaveChanges();
return RedirectToAction("Countries_List");
}
else
{
ModelState.AddModelError("CountryTitle", "Already Registered.");
}
}
ModelState.AddModelError("CountryTitle", "Required Field");
return View(countryMV);
}
public ActionResult EditCountry(int? id)
{
var getcountry = DB.CountryTables.Find(id);
var editcountry = new CountryMV();
editcountry.CountryID = getcountry.CountryID;
editcountry.CountryTitle = getcountry.CountryTitle;
return View(editcountry);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult EditCountry(CountryMV countryMV)
{
if (ModelState.IsValid)
{
var check_exist = DB.CountryTables.Where(a => a.CountryTitle == countryMV.CountryTitle.Trim() && a.CountryID != countryMV.CountryID).FirstOrDefault();
if (check_exist == null)
{
var country = DB.CountryTables.Find(countryMV.CountryID);
country.CountryTitle = countryMV.CountryTitle;
DB.Entry(country).State = System.Data.Entity.EntityState.Modified;
DB.SaveChanges();
return RedirectToAction("Countries_List");
}
else
{
ModelState.AddModelError("CountryTitle", "Already Registered.");
}
}
ModelState.AddModelError("CountryTitle", "Required Field");
return View(countryMV);
}
> CountryTable All Views Code
Country List View Code:
@model IEnumerable<eCommerceUI.Models.CountryMV>
@{
ViewBag.Title = "List";
}
<div class="container">
<div class="page-width">
<h3 class="docs-title">Country List</h3>
<p>
@Html.ActionLink("New Country", "NewCountry", null, new { @class = "btn btn--secondary" })
</p>
<div class="table-wrap">
<table class="responsive-table">
<thead>
<tr>
<th>@Html.DisplayNameFor(model => model.CountryID)</th>
<th>@Html.DisplayNameFor(model => model.CountryTitle)</th>
<th>Action</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr class="responsive-table-row">
<td>#@Html.DisplayFor(modelItem => item.CountryID)</td>
<td>@Html.DisplayFor(modelItem => item.CountryTitle)</td>
<td>
@Html.ActionLink("Edit", "EditCountry", new { id = item.CountryID }, new { @class = "btn" })
</td>
</tr>
}
<tbody>
</table>
</div>
</div>
</div>
New Country View Code :
@model eCommerceUI.Models.CountryMV
@{
ViewBag.Title = "Add";
}
<div class="container">
<div class="page-width">
@using (Html.BeginForm("NewCountry", "BasicConfiguration"))
{
@Html.AntiForgeryToken()
<h3 class="docs-title">New Country</h3>
<hr />
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.CountryTitle, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CountryTitle, new { htmlAttributes = new { @class = "form-control", placeholder = "Enter Country Title" } })
@Html.ValidationMessageFor(model => model.CountryTitle, "", 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--secondary" />
@Html.ActionLink("Back to List", "Countries_List", null, new { @class = "btn" })
</div>
</div>
</div>
}
</div>
</div>
Edit Country View Code :
@model eCommerceUI.Models.CountryMV
@{
ViewBag.Title = "Edit";
}
<div class="container">
<div class="page-width">
@using (Html.BeginForm("EditCountry", "BasicConfiguration"))
{
@Html.AntiForgeryToken()
<h3 class="docs-title">Edit Country</h3>
<hr />
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.CountryID)
<div class="form-group">
@Html.LabelFor(model => model.CountryTitle, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CountryTitle, new { htmlAttributes = new { @class = "form-control", placeholder = "Enter Country Title" } })
@Html.ValidationMessageFor(model => model.CountryTitle, "", 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--secondary" />
@Html.ActionLink("Back to List", "Countries_List", null, new { @class = "btn" })
</div>
</div>
</div>
}
</div>
</div>
Comments
Post a Comment