In ApplicationHelper :
ApplicationEnum
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DoctApplicationHelper
{
public class ApplicationEnum
{
public enum AlertType
{
Error = 0,
Success = 1
}
}
}
CommonFuntion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using DoctOnlineModel;
using DoctApplicationHelper;
namespace DoctApplicationHelper
{
public class CommonFunction
{
public static List<SelectListItem> GetProfessions()
{
using (DBEntities db = new DBEntities())
{
var listOfProfession = db.ProfessionMasterModel.Distinct().Select(p => new SelectListItem()
{
Text = p.ProfessionName,
Value= p.ProfessionId.ToString()
}).ToList();
listOfProfession.Insert(0, new SelectListItem { Text="Please Select", Value=string.Empty });
return listOfProfession;
}
}
}
}
In DBEntities :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using DoctOnlineModel;
namespace DoctApplicationHelper
{
public class DBEntities : DbContext
{
//public DbSet<UserInfoModel> Login { get; set; }
public DbSet<ProfessionMasterModel> ProfessionMasterModel { get; set; }
public DbSet<UserInfoModel> UserInfoModel { get; set; }
}
}
In Model :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace DoctOnlineModel
{
[Table("UserMaster")]
public class UserInfoModel
{
[Key]
public Int64 UserID { get; set; }
public string Username { get; set; }
public string EmailId { get; set; }
public string MobileNo { get; set; }
public string Name { get; set; }
public DateTime? DOB { get; set; }
public string City { get; set; }
public int? ProfessionId { get; set; }
public DateTime? ProfileCreated { get; set; }
public DateTime? ProfileActivated { get; set; }
public DateTime? ProfileDeactivated { get; set; }
public bool? IsProfileDeactivated { get; set; }
public byte? LoginAttempted { get; set; }
public bool? Status { get; set; }
public bool? IsDeleted { get; set; }
[NotMapped]
public string ProfessionName { get; set; }
[NotMapped]
public string Password { get; set; }
[NotMapped]
public string ReturnURL { get; set; }
[NotMapped]
public bool isRemember { get; set; }
public string HASH { get; set; }
public byte[] SALT { get; set; }
}
}
In Base Controller :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
namespace DoctOnline.Controllers
{
public class BaseController : Controller
{
public void AlertMessage(string AlertMessage, string AlertType)
{
Message = Regex.Replace(AlertMessage, @"[^0-9a-zA-Z :,]+", "");
MessageType = AlertType;
}
private string Message
{
get { return TempData["AlertMessage"] == null ? string.Empty : TempData["AlertMessage"].ToString(); }
set { TempData["AlertMessage"] = value; }
}
private string MessageType
{
get { return TempData["AlertType"] == null ? string.Empty : TempData["AlertType"].ToString(); }
set { TempData["AlertType"] = value; }
}
}
}
In Child Controller :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DoctApplicationHelper;
using DoctOnlineModel;
using System.Text.RegularExpressions;
namespace DoctOnline.Controllers
{
public class DashboardController : BaseController
{
private DBEntities db = new DBEntities();
public ActionResult UserProfile()
{
ViewBag.Message = "User Profile";
ViewBag.Profession = CommonFunction.GetProfessions();
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult UserProfile(UserInfoModel objUserInfo, string btnSumbit)
{
ViewBag.Profession = new SelectList(CommonFunction.GetProfessions(), "Value", "Text", objUserInfo.ProfessionName);
switch (btnSumbit)
{
case "Save":
// call another controller action
return (Save(objUserInfo));
case "Cancel":
// call another action to perform the cancellation
return (Cancel());
default:
// If they've submitted the form without a submitButton,
// just return the view again.
return (View());
}
}
private ActionResult Cancel()
{
// process the cancellation request here.
return (View("Cancelled"));
}
private ActionResult Save(UserInfoModel objUserInfo)
{
try
{
if (string.IsNullOrEmpty(objUserInfo.Name))
{
ModelState.AddModelError("Name", "Name is required");
}
if (!string.IsNullOrEmpty(objUserInfo.EmailId))
{
string emailRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
@"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
@".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
Regex re = new Regex(emailRegex);
if (!re.IsMatch(objUserInfo.EmailId))
{
ModelState.AddModelError("EmailId", "Email is not valid");
}
}
else
{
ModelState.AddModelError("EmailId", "Email is required");
}
if (!string.IsNullOrEmpty(objUserInfo.MobileNo))
{
string mobileRegex = @"(?<!\d)\d{10}(?!\d)";
Regex re = new Regex(mobileRegex);
if (!re.IsMatch(objUserInfo.MobileNo))
{
ModelState.AddModelError("MobileNo", "Mobile No is not valid");
}
}
else
{
ModelState.AddModelError("MobileNo", "Mobile No is required");
}
if (ModelState.IsValid)
{
using (db = new DBEntities())
{
var userInfo = db.UserInfoModel.Where(u => u.EmailId == objUserInfo.EmailId
|| u.MobileNo == objUserInfo.MobileNo).FirstOrDefault();
if (userInfo == null)
{
objUserInfo.IsProfileDeactivated = false;
objUserInfo.IsDeleted = false;
objUserInfo.Status = true;
db.UserInfoModel.Add(objUserInfo);
db.SaveChanges();
AlertMessage("Profile saved successfully.", Convert.ToString(ApplicationEnum.AlertType.Success));
}
else
{
AlertMessage("Profile saved successfully.", Convert.ToString(ApplicationEnum.AlertType.Error));
}
}
}
return View();
}
catch
{
throw;
}
}
}
}
In View :
@model DoctOnlineModel.UserInfoModel
@{
Layout = "~/Views/Shared/_LayoutDashboard.cshtml";
}
<div class="outter-wp">
<!--/sub-heard-part-->
<div class="sub-heard-part">
<ol class="breadcrumb m-b-0">
<li><a href="index.html">Dashboard</a></li>
<li class="active">Personal Details</li>
</ol>
</div>
<!--/forms-->
<div class="forms-main">
<div class="forms-inner">
<!--/set-2-->
<div class="set-1">
<div class="graph-2 general">
<h3 class="inner-tittle two">Personal Details </h3>
<div class="grid-1">
<div class="form-body">
@using (Html.BeginForm("UserProfile", "Dashboard", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<div class="form-group">
<label for="focusedinput" class="col-sm-2 control-label">Name<span class="text-danger">*</span> : </label>
<div class="col-sm-6">
@Html.TextBoxFor(s => s.Name, new { @placeholder = "Enter Your Name!", @class="form-control1", id="txtName" })
</div>
<div class="col-sm-4">
<p class="help-block">Your Name!</p>
</div>
</div>
<div class="form-group">
<label for="focusedinput" class="col-sm-2 control-label">Email<span class="text-danger">*</span> : </label>
<div class="col-sm-6">
@Html.TextBoxFor(s => s.EmailId, new { @placeholder = "Enter Your Email!", @class="form-control1", id="txtName" })
</div>
<div class="col-sm-4">
<p class="help-block">Enter Your Email!</p>
</div>
</div>
<div class="form-group">
<label for="focusedinput" class="col-sm-2 control-label">Mobile No<span class="text-danger">*</span> : </label>
<div class="col-sm-6">
@Html.TextBoxFor(s => s.MobileNo, new { @placeholder = "Enter Your Mobile No!", @class="form-control1", id="txtName" })
</div>
<div class="col-sm-4">
<p class="help-block">Enter Your Mobile No!</p>
</div>
</div>
<div class="form-group">
<label for="focusedinput" class="col-sm-2 control-label">DOB<span class="text-danger">*</span> : </label>
<div class="col-sm-6">
@Html.TextBoxFor(s => s.DOB, new { @placeholder = "DOB", @class="form-control1 ng-invalid ng-invalid-required", type="date", id="txtName" })
</div>
<div class="col-sm-4">
<p class="help-block">Select Your Date Of Birth!</p>
</div>
</div>
<div class="form-group">
<label for="focusedinput" class="col-sm-2 control-label">Current Location : </label>
<div class="col-sm-6">
@Html.TextBoxFor(s => s.City, new { @placeholder = "Enter Your Current City!", @class="form-control1", id="txtName" })
</div>
<div class="col-sm-4">
<p class="help-block">Enter Your Current City!</p>
</div>
</div>
<div class="form-group">
<label for="focusedinput" class="col-sm-2 control-label">Occupation<span class="text-danger">*</span> : </label>
<div class="col-sm-6">
@Html.DropDownListFor(s => s.ProfessionId, ViewBag.Profession as IEnumerable<SelectListItem>, new { @class="form-control1", id="ddlProfession" })
</div>
<div class="col-sm-4">
<p class="help-block">Select Your Occupation!</p>
</div>
</div>
<div class="form-group">
<label for="focusedinput" class="col-sm-2 control-label"> </label>
<div class="col-sm-6 form-group button-2">
<input type="submit" class="btn btn-primary" name="btnSumbit" value="Save"/>
<input type="submit" class="btn btn-default" name="btnSumbit" value="Reset"/>
</div>
<div class="col-sm-4">
<p class="help-block"> </p>
</div>
</div>
</div>
}
</div>
</div>
</div>
</div>
<!--/set-2-->
</div>
</div>
</div>
In Layout
<script src="~/Include/SweetAlert/sweetalert2.min.js"></script>
<link href="~/Include/SweetAlert/sweetalert2.css" rel="stylesheet" />
@*<script src="https://unpkg.com/sweetalert2@7.24.1/dist/sweetalert2.all.js"></script>*@
@{
var strAlertMessage = Convert.ToString(TempData["AlertMessage"]) ?? string.Empty;
var strAlertType = Convert.ToString(TempData["AlertType"]) ?? string.Empty;
TempData["AlertMessage"]=string.Empty;
TempData["AlertType"]=string.Empty;
}
<script>
$(document).ready(function () {
var Message = '@strAlertMessage';
var MessageType = '@strAlertType';
if (Message != '') {
if (MessageType == 'Error') {
swal({
title: 'Error!',
text: Message,
type: 'error',
confirmButtonText: 'Ok'
})
}
else (messageType == 1)
{
swal({
title: 'Success!',
text: Message,
type: 'success',
confirmButtonText: 'Ok'
})
}
}
});
</script>
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)
ApplicationEnum
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DoctApplicationHelper
{
public class ApplicationEnum
{
public enum AlertType
{
Error = 0,
Success = 1
}
}
}
CommonFuntion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using DoctOnlineModel;
using DoctApplicationHelper;
namespace DoctApplicationHelper
{
public class CommonFunction
{
public static List<SelectListItem> GetProfessions()
{
using (DBEntities db = new DBEntities())
{
var listOfProfession = db.ProfessionMasterModel.Distinct().Select(p => new SelectListItem()
{
Text = p.ProfessionName,
Value= p.ProfessionId.ToString()
}).ToList();
listOfProfession.Insert(0, new SelectListItem { Text="Please Select", Value=string.Empty });
return listOfProfession;
}
}
}
}
In DBEntities :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using DoctOnlineModel;
namespace DoctApplicationHelper
{
public class DBEntities : DbContext
{
//public DbSet<UserInfoModel> Login { get; set; }
public DbSet<ProfessionMasterModel> ProfessionMasterModel { get; set; }
public DbSet<UserInfoModel> UserInfoModel { get; set; }
}
}
In Model :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace DoctOnlineModel
{
[Table("UserMaster")]
public class UserInfoModel
{
[Key]
public Int64 UserID { get; set; }
public string Username { get; set; }
public string EmailId { get; set; }
public string MobileNo { get; set; }
public string Name { get; set; }
public DateTime? DOB { get; set; }
public string City { get; set; }
public int? ProfessionId { get; set; }
public DateTime? ProfileCreated { get; set; }
public DateTime? ProfileActivated { get; set; }
public DateTime? ProfileDeactivated { get; set; }
public bool? IsProfileDeactivated { get; set; }
public byte? LoginAttempted { get; set; }
public bool? Status { get; set; }
public bool? IsDeleted { get; set; }
[NotMapped]
public string ProfessionName { get; set; }
[NotMapped]
public string Password { get; set; }
[NotMapped]
public string ReturnURL { get; set; }
[NotMapped]
public bool isRemember { get; set; }
public string HASH { get; set; }
public byte[] SALT { get; set; }
}
}
In Base Controller :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
namespace DoctOnline.Controllers
{
public class BaseController : Controller
{
public void AlertMessage(string AlertMessage, string AlertType)
{
Message = Regex.Replace(AlertMessage, @"[^0-9a-zA-Z :,]+", "");
MessageType = AlertType;
}
private string Message
{
get { return TempData["AlertMessage"] == null ? string.Empty : TempData["AlertMessage"].ToString(); }
set { TempData["AlertMessage"] = value; }
}
private string MessageType
{
get { return TempData["AlertType"] == null ? string.Empty : TempData["AlertType"].ToString(); }
set { TempData["AlertType"] = value; }
}
}
}
In Child Controller :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DoctApplicationHelper;
using DoctOnlineModel;
using System.Text.RegularExpressions;
namespace DoctOnline.Controllers
{
public class DashboardController : BaseController
{
private DBEntities db = new DBEntities();
public ActionResult UserProfile()
{
ViewBag.Message = "User Profile";
ViewBag.Profession = CommonFunction.GetProfessions();
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult UserProfile(UserInfoModel objUserInfo, string btnSumbit)
{
ViewBag.Profession = new SelectList(CommonFunction.GetProfessions(), "Value", "Text", objUserInfo.ProfessionName);
switch (btnSumbit)
{
case "Save":
// call another controller action
return (Save(objUserInfo));
case "Cancel":
// call another action to perform the cancellation
return (Cancel());
default:
// If they've submitted the form without a submitButton,
// just return the view again.
return (View());
}
}
private ActionResult Cancel()
{
// process the cancellation request here.
return (View("Cancelled"));
}
private ActionResult Save(UserInfoModel objUserInfo)
{
try
{
if (string.IsNullOrEmpty(objUserInfo.Name))
{
ModelState.AddModelError("Name", "Name is required");
}
if (!string.IsNullOrEmpty(objUserInfo.EmailId))
{
string emailRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
@"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
@".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
Regex re = new Regex(emailRegex);
if (!re.IsMatch(objUserInfo.EmailId))
{
ModelState.AddModelError("EmailId", "Email is not valid");
}
}
else
{
ModelState.AddModelError("EmailId", "Email is required");
}
if (!string.IsNullOrEmpty(objUserInfo.MobileNo))
{
string mobileRegex = @"(?<!\d)\d{10}(?!\d)";
Regex re = new Regex(mobileRegex);
if (!re.IsMatch(objUserInfo.MobileNo))
{
ModelState.AddModelError("MobileNo", "Mobile No is not valid");
}
}
else
{
ModelState.AddModelError("MobileNo", "Mobile No is required");
}
if (ModelState.IsValid)
{
using (db = new DBEntities())
{
var userInfo = db.UserInfoModel.Where(u => u.EmailId == objUserInfo.EmailId
|| u.MobileNo == objUserInfo.MobileNo).FirstOrDefault();
if (userInfo == null)
{
objUserInfo.IsProfileDeactivated = false;
objUserInfo.IsDeleted = false;
objUserInfo.Status = true;
db.UserInfoModel.Add(objUserInfo);
db.SaveChanges();
AlertMessage("Profile saved successfully.", Convert.ToString(ApplicationEnum.AlertType.Success));
}
else
{
AlertMessage("Profile saved successfully.", Convert.ToString(ApplicationEnum.AlertType.Error));
}
}
}
return View();
}
catch
{
throw;
}
}
}
}
In View :
@model DoctOnlineModel.UserInfoModel
@{
Layout = "~/Views/Shared/_LayoutDashboard.cshtml";
}
<div class="outter-wp">
<!--/sub-heard-part-->
<div class="sub-heard-part">
<ol class="breadcrumb m-b-0">
<li><a href="index.html">Dashboard</a></li>
<li class="active">Personal Details</li>
</ol>
</div>
<!--/forms-->
<div class="forms-main">
<div class="forms-inner">
<!--/set-2-->
<div class="set-1">
<div class="graph-2 general">
<h3 class="inner-tittle two">Personal Details </h3>
<div class="grid-1">
<div class="form-body">
@using (Html.BeginForm("UserProfile", "Dashboard", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<div class="form-group">
<label for="focusedinput" class="col-sm-2 control-label">Name<span class="text-danger">*</span> : </label>
<div class="col-sm-6">
@Html.TextBoxFor(s => s.Name, new { @placeholder = "Enter Your Name!", @class="form-control1", id="txtName" })
</div>
<div class="col-sm-4">
<p class="help-block">Your Name!</p>
</div>
</div>
<div class="form-group">
<label for="focusedinput" class="col-sm-2 control-label">Email<span class="text-danger">*</span> : </label>
<div class="col-sm-6">
@Html.TextBoxFor(s => s.EmailId, new { @placeholder = "Enter Your Email!", @class="form-control1", id="txtName" })
</div>
<div class="col-sm-4">
<p class="help-block">Enter Your Email!</p>
</div>
</div>
<div class="form-group">
<label for="focusedinput" class="col-sm-2 control-label">Mobile No<span class="text-danger">*</span> : </label>
<div class="col-sm-6">
@Html.TextBoxFor(s => s.MobileNo, new { @placeholder = "Enter Your Mobile No!", @class="form-control1", id="txtName" })
</div>
<div class="col-sm-4">
<p class="help-block">Enter Your Mobile No!</p>
</div>
</div>
<div class="form-group">
<label for="focusedinput" class="col-sm-2 control-label">DOB<span class="text-danger">*</span> : </label>
<div class="col-sm-6">
@Html.TextBoxFor(s => s.DOB, new { @placeholder = "DOB", @class="form-control1 ng-invalid ng-invalid-required", type="date", id="txtName" })
</div>
<div class="col-sm-4">
<p class="help-block">Select Your Date Of Birth!</p>
</div>
</div>
<div class="form-group">
<label for="focusedinput" class="col-sm-2 control-label">Current Location : </label>
<div class="col-sm-6">
@Html.TextBoxFor(s => s.City, new { @placeholder = "Enter Your Current City!", @class="form-control1", id="txtName" })
</div>
<div class="col-sm-4">
<p class="help-block">Enter Your Current City!</p>
</div>
</div>
<div class="form-group">
<label for="focusedinput" class="col-sm-2 control-label">Occupation<span class="text-danger">*</span> : </label>
<div class="col-sm-6">
@Html.DropDownListFor(s => s.ProfessionId, ViewBag.Profession as IEnumerable<SelectListItem>, new { @class="form-control1", id="ddlProfession" })
</div>
<div class="col-sm-4">
<p class="help-block">Select Your Occupation!</p>
</div>
</div>
<div class="form-group">
<label for="focusedinput" class="col-sm-2 control-label"> </label>
<div class="col-sm-6 form-group button-2">
<input type="submit" class="btn btn-primary" name="btnSumbit" value="Save"/>
<input type="submit" class="btn btn-default" name="btnSumbit" value="Reset"/>
</div>
<div class="col-sm-4">
<p class="help-block"> </p>
</div>
</div>
</div>
}
</div>
</div>
</div>
</div>
<!--/set-2-->
</div>
</div>
</div>
In Layout
<script src="~/Include/SweetAlert/sweetalert2.min.js"></script>
<link href="~/Include/SweetAlert/sweetalert2.css" rel="stylesheet" />
@*<script src="https://unpkg.com/sweetalert2@7.24.1/dist/sweetalert2.all.js"></script>*@
@{
var strAlertMessage = Convert.ToString(TempData["AlertMessage"]) ?? string.Empty;
var strAlertType = Convert.ToString(TempData["AlertType"]) ?? string.Empty;
TempData["AlertMessage"]=string.Empty;
TempData["AlertType"]=string.Empty;
}
<script>
$(document).ready(function () {
var Message = '@strAlertMessage';
var MessageType = '@strAlertType';
if (Message != '') {
if (MessageType == 'Error') {
swal({
title: 'Error!',
text: Message,
type: 'error',
confirmButtonText: 'Ok'
})
}
else (messageType == 1)
{
swal({
title: 'Success!',
text: Message,
type: 'success',
confirmButtonText: 'Ok'
})
}
}
});
</script>
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)
No comments:
Post a Comment