eCommerce Website Part 15 User Address
eCommerce Website Part 15 User Addresses
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 User Addresses for more details click here:
watch vedioIn UserController we add two method UserAddress and EditUserAddress show in below code:
> UserAddressMV Class Code :
public class UserAddressMV
{
public int UserAddressID { get; set; }
public int UserID { get; set; }
public int AddressTypeID { get; set; }
public string AddressType { get; set; }
public string FullAddress { get; set; }
public int CityID { get; set; }
public string City { get; set; }
public string PostalCode { get; set; }
public string ContactNo { get; set; }
public int StatusID { get; set; }
public string Status { get; set; }
}
> Reg_UserAddressMV Class Code :
public class Reg_UserAddressMV
{
public Reg_UserAddressMV()
{
AddressTypeList = new List<AddressTypeMV>();
foreach (var addresstype in new DatabaseLayer.Pro_EcommerceDbEntities().AddressTypeTables.ToList())
{
AddressTypeList.Add(new AddressTypeMV() { AddressTypeID = addresstype.AddressTypeID, AddressType = addresstype.AddressType });
}
CountriesList = new List<CountryMV>();
foreach (var country in new DatabaseLayer.Pro_EcommerceDbEntities().CountryTables.ToList())
{
CountriesList.Add(new CountryMV() { CountryID = country.CountryID, CountryTitle = country.CountryTitle });
}
CitiesList = new List<CityMV>();
StatusList = new List<StatusMV>();
foreach (var status in new DatabaseLayer.Pro_EcommerceDbEntities().StatusTables.ToList())
{
StatusList.Add(new StatusMV() { StatusID = status.StatusID, StatusTitle = status.StatusTitle });
}
}
public Reg_UserAddressMV(int? userid)
{
AddressTypeList = new List<AddressTypeMV>();
foreach (var addresstype in new DatabaseLayer.Pro_EcommerceDbEntities().AddressTypeTables.ToList())
{
AddressTypeList.Add(new AddressTypeMV() { AddressTypeID = addresstype.AddressTypeID, AddressType = addresstype.AddressType });
}
CountriesList = new List<CountryMV>();
foreach (var country in new DatabaseLayer.Pro_EcommerceDbEntities().CountryTables.ToList())
{
CountriesList.Add(new CountryMV() { CountryID = country.CountryID, CountryTitle = country.CountryTitle });
}
CitiesList = new List<CityMV>();
StatusList = new List<StatusMV>();
foreach (var status in new DatabaseLayer.Pro_EcommerceDbEntities().StatusTables.ToList())
{
StatusList.Add(new StatusMV() { StatusID = status.StatusID, StatusTitle = status.StatusTitle });
}
list = new List<UserAddressMV>();
foreach (var address in new DatabaseLayer.Pro_EcommerceDbEntities().UserAddressTables.Where(u => u.UserID == userid).ToList())
{
var addresstype = new DatabaseLayer.Pro_EcommerceDbEntities().AddressTypeTables.Find(address.AddressTypeID);
var city = new DatabaseLayer.Pro_EcommerceDbEntities().CityTables.Find(address.CityID);
var status = new DatabaseLayer.Pro_EcommerceDbEntities().StatusTables.Find(address.StatusID);
list.Add(new UserAddressMV()
{
UserAddressID = address.UserAddressID,
UserID = address.UserID,
AddressTypeID = address.AddressTypeID,
AddressType = addresstype.AddressType,
FullAddress = address.FullAddress,
CityID = address.CityID,
City = city.CityName,
PostalCode = address.PostalCode,
ContactNo = address.ContactNo,
StatusID = address.StatusID,
Status = status.StatusTitle
});
}
UserID = Convert.ToInt32(userid);
}
public int UserAddressID { get; set; }
public int UserID { get; set; }
[Required(ErrorMessage="Required*")]
[Display(Name ="Address Type")]
public int AddressTypeID { get; set; }
[Required(ErrorMessage = "Enter Full Address*")]
[Display(Name = "Full Address")]
[DataType(DataType.MultilineText)]
public string FullAddress { get; set; }
[Required(ErrorMessage = "Required*")]
[Display(Name = "Select Country")]
public int CountryID { get; set; }
[Required(ErrorMessage = "Required*")]
[Display(Name = "Select City")]
public int CityID { get; set; }
[Required(ErrorMessage = "Enter Postal Code*")]
[Display(Name = "Postal Code")]
[DataType(DataType.Text)]
public string PostalCode { get; set; }
[Required(ErrorMessage = "Enter Contact No*")]
[Display(Name = "Contact No")]
[RegularExpression("(^\\+?\\d{1,4}?[-.\\s]?\\(?\\d{1,3}?\\)?[-.\\s]?\\d{1,4}[-.\\s]?\\d{1,4}[-.\\s]?\\d{1,9}$)", ErrorMessage = "Only Number's Allowed.")]
public string ContactNo { get; set; }
[Required(ErrorMessage = "Required*")]
[Display(Name = "Select Status")]
public int StatusID { get; set; }
public List<UserAddressMV> list { get; set; }
public virtual List<AddressTypeMV> AddressTypeList { get; set; }
public virtual List<CountryMV> CountriesList { get; set; }
public virtual List<CityMV> CitiesList { get; set; }
public virtual List<StatusMV> StatusList { get; set; }
}
> UserAddress Action Code :
public ActionResult UserAddress()
{
if (string.IsNullOrEmpty(Convert.ToString(Session["UserID"])))
{
return RedirectToAction("Login","User");
}
int userid = 0;
int.TryParse(Convert.ToString(Session["UserID"]),out userid);
return View(new Reg_UserAddressMV(userid));
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult UserAddress(Reg_UserAddressMV reg_UserAddressMV)
{
if (ModelState.IsValid)
{
var useraddress = new UserAddressTable();
useraddress.UserID = reg_UserAddressMV.UserID;
useraddress.AddressTypeID = reg_UserAddressMV.AddressTypeID;
useraddress.FullAddress = reg_UserAddressMV.FullAddress;
useraddress.CityID = reg_UserAddressMV.CityID;
useraddress.PostalCode = reg_UserAddressMV.PostalCode;
useraddress.ContactNo = reg_UserAddressMV.ContactNo;
useraddress.StatusID = reg_UserAddressMV.StatusID;
DB.UserAddressTables.Add(useraddress);
DB.SaveChanges();
return RedirectToAction("UserAddress");
}
return View(reg_UserAddressMV);
}
> EditUserAddress Action Code :
public ActionResult EditUserAddress(int? id)
{
if (string.IsNullOrEmpty(Convert.ToString(Session["UserID"])))
{
return RedirectToAction("Login", "User");
}
int userid = 0;
int.TryParse(Convert.ToString(Session["UserID"]), out userid);
var address = DB.UserAddressTables.Find(id);
var edituseraddress = new Reg_UserAddressMV(userid);
edituseraddress.UserAddressID = address.UserAddressID;
edituseraddress.UserID = address.UserID;
edituseraddress.AddressTypeID = address.AddressTypeID;
edituseraddress.FullAddress = address.FullAddress;
edituseraddress.CityID = address.CityID;
edituseraddress.PostalCode = address.PostalCode;
edituseraddress.ContactNo = address.ContactNo;
edituseraddress.StatusID = address.StatusID;
return View(edituseraddress);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult EditUserAddress(Reg_UserAddressMV reg_UserAddressMV)
{
if (ModelState.IsValid)
{
var useraddress = DB.UserAddressTables.Find(reg_UserAddressMV.UserAddressID);
useraddress.AddressTypeID = reg_UserAddressMV.AddressTypeID;
useraddress.FullAddress = reg_UserAddressMV.FullAddress;
useraddress.CityID = reg_UserAddressMV.CityID;
useraddress.PostalCode = reg_UserAddressMV.PostalCode;
useraddress.ContactNo = reg_UserAddressMV.ContactNo;
useraddress.StatusID = reg_UserAddressMV.StatusID;
DB.Entry(useraddress).State = System.Data.Entity.EntityState.Modified;
DB.SaveChanges();
return RedirectToAction("UserAddress");
}
return View(reg_UserAddressMV);
}
> UserAddress View Code :
@model eCommerceUI.Models.Reg_UserAddressMV
@{
ViewBag.Title = "User Address";
}
<div class="container">
<div class="page-width">
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<hr />
<h3 class="docs-title text-center">New User Address</h3>
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.UserID)
<div class="row" style="margin-left:1px;">
<div class="form-group col-md-3">
@Html.LabelFor(model => model.AddressTypeID, "Select Address Type", htmlAttributes: new { @class = "control-label" })
@Html.DropDownList("AddressTypeID", new SelectList(Model.AddressTypeList, "AddressTypeID", "AddressType", Model.AddressTypeID), "--Choose--", htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.AddressTypeID, "", new { @class = "text-danger" })
</div>
<div class="form-group col-md-3">
@Html.LabelFor(model => model.StatusID, "Select Status", htmlAttributes: new { @class = "control-label" })
@Html.DropDownList("StatusID", new SelectList(Model.StatusList, "StatusID", "StatusTitle", Model.StatusID), "--Choose--", htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.StatusID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.FullAddress, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-6">
@Html.EditorFor(model => model.FullAddress, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FullAddress, "", new { @class = "text-danger" })
</div>
</div>
<div class="row" style="margin-left:1px;">
<div class="form-group col-md-3">
@Html.LabelFor(model => model.CountryID, "Select Country", htmlAttributes: new { @class = "control-label" })
@Html.DropDownList("CountryID", new SelectList(Model.CountriesList, "CountryID", "CountryTitle", Model.CountryID), "--Choose--", htmlAttributes: new { @class = "form-control", onchange = "myFunction()", required = "required" })
@Html.ValidationMessageFor(model => model.CountryID, "", new { @class = "text-danger" })
</div>
<div class="form-group col-md-3">
@Html.LabelFor(model => model.CityID, "Select City", htmlAttributes: new { @class = "control-label" })
@Html.DropDownList("CityID", new SelectList(Model.CitiesList, "CityID", "CityName", Model.CityID), "--Choose--", htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.CityID, "", new { @class = "text-danger" })
</div>
</div>
<div class="row" style="margin-left:1px;">
<div class="form-group col-md-3">
@Html.LabelFor(model => model.PostalCode, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.PostalCode, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PostalCode, "", new { @class = "text-danger" })
</div>
<div class="form-group col-md-3">
@Html.LabelFor(model => model.ContactNo, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.ContactNo, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ContactNo, "", 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" />
</div>
</div>
</div>
}
<hr />
<h3 class="docs-title">Address Types List</h3>
<p>
<div style="float:right;" class="form-group">
<input type="text"
style="background-image: url('@Url.Content("~/Content/Template/images/searchicon.png")'); background-position: 10px 5px; background-repeat: no-repeat; font-size: 16px; padding: 12px 20px 12px 40px; border: 1px solid #ddd; margin-bottom: 12px; "
class="search form-control"
id="myInput"
placeholder="What you looking for?">
</div>
</p>
<div class="table">
<table class="responsive-table">
<thead>
<tr>
<td>#Unique No</td>
<td>Address Type</td>
<td>Full Address</td>
<td>Contact No</td>
<td>City</td>
<td>Postal Code</td>
<td>Status</td>
<th>Action</th>
</tr>
</thead>
<tbody id="myTable">
@foreach (var item in Model.list)
{
<tr class="responsive-table-row">
<td>#@Html.DisplayFor(modelItem => item.UserAddressID)</td>
<td>@Html.DisplayFor(modelItem => item.AddressType)</td>
<td>@Html.DisplayFor(modelItem => item.FullAddress)</td>
<td>@Html.DisplayFor(modelItem => item.ContactNo)</td>
<td>@Html.DisplayFor(modelItem => item.City)</td>
<td>@Html.DisplayFor(modelItem => item.PostalCode)</td>
<td>
@if (item.Status == "Visible")
{
<span class="label label--new mb-3 mr-3 text-nowrap">
@Html.DisplayFor(modelItem => item.Status)
</span>
}
else
{
<span class="label label--sale mb-3 mr-3 text-nowrap">
@Html.DisplayFor(modelItem => item.Status)
</span>
}
</td>
<td>
@Html.ActionLink("Edit", "EditUserAddress", new { id = item.UserAddressID }, new { @class = "btn" })
</td>
</tr>
}
<tbody>
</table>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function () {
var countryid = $("#CountryID").val();
$.ajax({
type: "GET",
url: "/BasicConfiguration/GetCitiesByCountryID?countryid=" + countryid,
data: [],
success: function (data) {
var s = '<option value="-1">Select City</option>';
for (var i = 0; i < data.length; i++) {
s += '<option value="' + data[i].CityID + '">' + data[i].CityName + '</option>';
}
$("#CityID").html(s);
}
});
});
function myFunction() {
var countryid = $("#CountryID").val();
$.ajax({
type: "GET",
url: "/BasicConfiguration/GetCitiesByCountryID?countryid=" + countryid,
data: [],
success: function (data) {
var s = '<option value="-1">Select City</option>';
for (var i = 0; i < data.length; i++) {
s += '<option value="' + data[i].CityID + '">' + data[i].CityName + '</option>';
}
$("#CityID").html(s);
}
});
};
</script>
> Add GetCitiesByCountryID Method in BasicConfigurationController Code :
// Get Cities By Country ID
public JsonResult GetCitiesByCountryID(int? countryid)
{
var Cities = new List<CityMV>();
var cities = new DatabaseLayer.Pro_EcommerceDbEntities().CityTables.Where(c => c.CountryID == countryid).ToList();
foreach (var city in cities)
{
string countrytitle = city.CountryTable.CountryTitle;
Cities.Add(new CityMV { CityID = city.CityID, CityName = city.CityName, CountryID = city.CountryID, CountryName = countrytitle });
}
return Json(Cities, JsonRequestBehavior.AllowGet);
}
> EditUserAddress View Code :
@model eCommerceUI.Models.Reg_UserAddressMV
@{
ViewBag.Title = "Edit User Address";
}
<div class="container">
<div class="page-width">
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<hr />
<h3 class="docs-title text-center">Edit User Address</h3>
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.UserID)
@Html.HiddenFor(model => model.UserAddressID)
<div class="row" style="margin-left:1px;">
<div class="form-group col-md-3">
@Html.LabelFor(model => model.AddressTypeID, "Select Address Type", htmlAttributes: new { @class = "control-label" })
@Html.DropDownList("AddressTypeID", new SelectList(Model.AddressTypeList, "AddressTypeID", "AddressType", Model.AddressTypeID), "--Choose--", htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.AddressTypeID, "", new { @class = "text-danger" })
</div>
<div class="form-group col-md-3">
@Html.LabelFor(model => model.StatusID, "Select Status", htmlAttributes: new { @class = "control-label" })
@Html.DropDownList("StatusID", new SelectList(Model.StatusList, "StatusID", "StatusTitle", Model.StatusID), "--Choose--", htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.StatusID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.FullAddress, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-6">
@Html.EditorFor(model => model.FullAddress, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FullAddress, "", new { @class = "text-danger" })
</div>
</div>
<div class="row" style="margin-left:1px;">
<div class="form-group col-md-3">
@Html.LabelFor(model => model.CountryID, "Select Country", htmlAttributes: new { @class = "control-label" })
@Html.DropDownList("CountryID", new SelectList(Model.CountriesList, "CountryID", "CountryTitle", Model.CountryID), "--Choose--", htmlAttributes: new { @class = "form-control", onchange = "myFunction()", required = "required" })
@Html.ValidationMessageFor(model => model.CountryID, "", new { @class = "text-danger" })
</div>
<div class="form-group col-md-3">
@Html.LabelFor(model => model.CityID, "Select City", htmlAttributes: new { @class = "control-label" })
@Html.DropDownList("CityID", new SelectList(Model.CitiesList, "CityID", "CityName", Model.CityID), "--Choose--", htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.CityID, "", new { @class = "text-danger" })
</div>
</div>
<div class="row" style="margin-left:1px;">
<div class="form-group col-md-3">
@Html.LabelFor(model => model.PostalCode, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.PostalCode, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PostalCode, "", new { @class = "text-danger" })
</div>
<div class="form-group col-md-3">
@Html.LabelFor(model => model.ContactNo, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.ContactNo, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ContactNo, "", 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" />
</div>
</div>
</div>
}
<hr />
<h3 class="docs-title">Address Types List</h3>
<p>
<div style="float:right;" class="form-group">
<input type="text"
style="background-image: url('@Url.Content("~/Content/Template/images/searchicon.png")'); background-position: 10px 5px; background-repeat: no-repeat; font-size: 16px; padding: 12px 20px 12px 40px; border: 1px solid #ddd; margin-bottom: 12px; "
class="search form-control"
id="myInput"
placeholder="What you looking for?">
</div>
</p>
<div class="table">
<table class="responsive-table">
<thead>
<tr>
<td>#Unique No</td>
<td>Address Type</td>
<td>Full Address</td>
<td>Contact No</td>
<td>City</td>
<td>Postal Code</td>
<td>Status</td>
<th>Action</th>
</tr>
</thead>
<tbody id="myTable">
@foreach (var item in Model.list)
{
<tr class="responsive-table-row">
<td>#@Html.DisplayFor(modelItem => item.UserAddressID)</td>
<td>@Html.DisplayFor(modelItem => item.AddressType)</td>
<td>@Html.DisplayFor(modelItem => item.FullAddress)</td>
<td>@Html.DisplayFor(modelItem => item.ContactNo)</td>
<td>@Html.DisplayFor(modelItem => item.City)</td>
<td>@Html.DisplayFor(modelItem => item.PostalCode)</td>
<td>
@if (item.Status == "Visible")
{
<span class="label label--new mb-3 mr-3 text-nowrap">
@Html.DisplayFor(modelItem => item.Status)
</span>
}
else
{
<span class="label label--sale mb-3 mr-3 text-nowrap">
@Html.DisplayFor(modelItem => item.Status)
</span>
}
</td>
<td>
@Html.ActionLink("Edit", "EditUserAddress", new { id = item.UserAddressID }, new { @class = "btn" })
</td>
</tr>
}
<tbody>
</table>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function () {
var countryid = $("#CountryID").val();
$.ajax({
type: "GET",
url: "/BasicConfiguration/GetCitiesByCountryID?countryid=" + countryid,
data: [],
success: function (data) {
var s = '<option value="-1">Select City</option>';
for (var i = 0; i < data.length; i++) {
s += '<option value="' + data[i].CityID + '">' + data[i].CityName + '</option>';
}
$("#CityID").html(s);
}
});
});
function myFunction() {
var countryid = $("#CountryID").val();
$.ajax({
type: "GET",
url: "/BasicConfiguration/GetCitiesByCountryID?countryid=" + countryid,
data: [],
success: function (data) {
var s = '<option value="-1">Select City</option>';
for (var i = 0; i < data.length; i++) {
s += '<option value="' + data[i].CityID + '">' + data[i].CityName + '</option>';
}
$("#CityID").html(s);
}
});
};
</script>
Comments
Post a Comment