MODEL
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Data;
namespace MyMvcApplication.Models
{
public class Mobiledata
{
public int MobileID { get; set; }
[Required(ErrorMessage = "Please Enter Mobile Name")]
[Display(Name = "Enter Mobile Name")]
[StringLength(50, MinimumLength = 3, ErrorMessage = "Mobile Name must be between 3 and 50 characters!")]
public string MobileName { get; set; }
[Required(ErrorMessage = "Please Enter MobileIMEno")]
[Display(Name = "Enter MobileIMEno")]
[MaxLength(100, ErrorMessage = "Exceeding Limit")]
public string MobileIMEno { get; set; }
[Required(ErrorMessage = "Please Enter Mobile Price")]
[Display(Name = "Enter Mobile Price")]
[DataType(DataType.Currency)]
public string mobileprice { get; set; }
[Required(ErrorMessage = "Please Enter Mobile Manufacured")]
[Display(Name = "Enter Mobile Manufacured")]
[DataType(DataType.Text)]
public string mobileManufacured { get; set; }
public DataSet StoreAllData { get; set; }
}
}
CONTROLLER
using MyMvcApplication.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MyMvcApplication.Controllers
{
public class MobileStoreController : Controller
{
//
// GET: /MobileStore/
public ActionResult Index()
{
return View();
}
public ActionResult InsertMobile() // Calling when we first hit controller.
{
return View();
}
[HttpPost]
public ActionResult InsertMobile(Mobiledata MB) // Calling on http post (on Submit)
{
if (ModelState.IsValid) //checking model is valid or not
{
DataAccessLayer.DBData objDB = new DataAccessLayer.DBData(); //calling class DBdata
string result = objDB.InsertData(MB); // passing Value to DBClass from model
ViewData["result"] = result;
ModelState.Clear(); //clearing model
return View();
}
else
{
ModelState.AddModelError("", "Error in saving data");
return View();
}
}
//(StoreAlldata)
public ActionResult ShowAllMobileDetails(Mobiledata MB)
{
DataAccessLayer.DBData objDB = new DataAccessLayer.DBData(); //calling class DBdata
MB.StoreAllData = objDB.SelectAllData();
return View(MB);
}
public ActionResult EDITMOBILEDATA(string id)
{
DataAccessLayer.DBData objDB = new DataAccessLayer.DBData(); //calling class DBdata
DataSet ds = objDB.SelectAllDatabyID(id);
Mobiledata MB = new Mobiledata();
MB.MobileID = Convert.ToInt32(ds.Tables[0].Rows[0]["MobileID"].ToString());
MB.MobileName = ds.Tables[0].Rows[0]["MobileName"].ToString();
MB.MobileIMEno = ds.Tables[0].Rows[0]["MobileIMEno"].ToString();
MB.mobileprice = ds.Tables[0].Rows[0]["mobileprice"].ToString();
MB.mobileManufacured = ds.Tables[0].Rows[0]["mobileManufacured"].ToString();
return View(MB);
}
[HttpPost]
public ActionResult EDITMOBILEDATA(Mobiledata MD)
{
DataAccessLayer.DBData objDB = new DataAccessLayer.DBData(); //calling class DBdata
string result = objDB.UpdateData(MD); // passing Value to DBClass from model
ViewData["resultUpdate"] = result; // for dislaying message after updating data.
return RedirectToAction("ShowAllMobileDetails", "Mobilestore");
}
public ActionResult DELETEMOBILEDATA(string id)
{
DataAccessLayer.DBData objDB = new DataAccessLayer.DBData(); //calling class DBdata
DataSet ds = objDB.SelectAllDatabyID(id);
Mobiledata MB = new Mobiledata();
MB.MobileID = Convert.ToInt32(ds.Tables[0].Rows[0]["MobileID"].ToString());
MB.MobileName = ds.Tables[0].Rows[0]["MobileName"].ToString();
MB.MobileIMEno = ds.Tables[0].Rows[0]["MobileIMEno"].ToString();
MB.mobileprice = ds.Tables[0].Rows[0]["mobileprice"].ToString();
MB.mobileManufacured = ds.Tables[0].Rows[0]["mobileManufacured"].ToString();
return View(MB);
}
[HttpPost]
public ActionResult DELETEMOBILEDATA(Mobiledata MD)
{
DataAccessLayer.DBData objDB = new DataAccessLayer.DBData(); //calling class DBdata
string result = objDB.DeleteData(MD);
return RedirectToAction("ShowAllMobileDetails", "Mobilestore");
}
}
}
VIEW
INSERT
@model MyMvcApplication.Models.Mobiledata
@{
ViewBag.Title = "InsertMobile";
}
<h2>InsertMobile</h2>
<table>
<tr>
<td>
@Html.ActionLink("Show All Mobile List", "ShowAllMobileDetails")
</td>
</tr>
</table>
@using (Html.BeginForm())
{
<table width="100%">
<tr>
<td>
@Html.LabelFor(a => a.MobileName)
</td>
</tr>
<tr>
<td>
@Html.TextBoxFor(a => a.MobileName)
@Html.ValidationMessageFor(a => a.MobileName)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(a => a.MobileIMEno)
</td>
</tr>
<tr>
<td>
@Html.TextBoxFor(a => a.MobileIMEno)
@Html.ValidationMessageFor(a => a.MobileIMEno)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(a => a.mobileManufacured)
</td>
</tr>
<tr>
<td>
@Html.TextBoxFor(a => a.mobileManufacured)
@Html.ValidationMessageFor(a => a.mobileManufacured)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(a => a.mobileprice)
</td>
</tr>
<tr>
<td>
@Html.TextBoxFor(a => a.mobileprice)
@Html.ValidationMessageFor(a => a.mobileprice)
</td>
</tr>
<tr>
<td colspan="2">
<input id="Submit1" type="submit" value="submit" />
</td>
</tr>
</table>
}
@{
if (ViewData["result"] != "" && ViewData["result"] != null)
{
ViewData["result"] = null;
<script type="text/javascript" language="javascript">
alert("Data saved Successfully");
</script>
}
}
EDIT
@model MyMvcApplication.Models.Mobiledata
@{
ViewBag.Title = "EDITMOBILEDATA";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>EDITMOBILEDATA</h2>
<table>
<tr>
<td>
@Html.ActionLink("Show All Mobile List", "ShowAllMobileDetails")
</td>
</tr>
</table>
<br />
@using (Html.BeginForm())
{
<table width="100%">
<tr>
<td colspan="2">
@Html.HiddenFor(a => a.MobileID)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(a => a.MobileName)
</td>
</tr>
<tr>
<td>
@Html.TextBoxFor(a => a.MobileName)
@Html.ValidationMessageFor(a => a.MobileName)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(a => a.MobileIMEno)
</td>
</tr>
<tr>
<td>
@Html.TextBoxFor(a => a.MobileIMEno)
@Html.ValidationMessageFor(a => a.MobileIMEno)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(a => a.mobileManufacured)
</td>
</tr>
<tr>
<td>
@Html.TextBoxFor(a => a.mobileManufacured)
@Html.ValidationMessageFor(a => a.mobileManufacured)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(a => a.mobileprice)
</td>
</tr>
<tr>
<td>
@Html.TextBoxFor(a => a.mobileprice)
@Html.ValidationMessageFor(a => a.mobileprice)
</td>
</tr>
<tr>
<td colspan="2">
<input id="Submit1" type="submit" value="Update" />
</td>
</tr>
</table>
}
@{
if (ViewData["resultUpdate"] != "" && ViewData["resultUpdate"] != null)
{
ViewData["resultUpdate"] = null;
<script type="text/javascript" language="javascript">
alert("Data Updated Successfully");
</script>
}
}
DELETE
@model MyMvcApplication.Models.Mobiledata
@{
ViewBag.Title = "DELETEMOBILEDATA";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>DELETEMOBILEDATA</h2>
<table>
<tr>
<td>
@Html.ActionLink("Add New Mobiles", "InsertMobile")
</td>
<td>
@Html.ActionLink("Show All Mobile List", "ShowAllMobileDetails")
</td>
</tr>
</table>
<br />
@using (Html.BeginForm())
{
<table width="100%">
<tr>
<td colspan="2">
@Html.HiddenFor(a => a.MobileID)
</td>
</tr>
<tr>
<td>
MobileName :-
@Html.DisplayFor(a => a.MobileName)
</td>
</tr>
<tr>
<td>
MobileIMEI Number:-
@Html.DisplayFor(a => a.MobileIMEno)
</td>
</tr>
<tr>
<td>
Mobile Manufacured :-
@Html.DisplayFor(a => a.mobileManufacured)
</td>
</tr>
<tr>
<td>
Mobileprice :-
@Html.DisplayFor(a => a.mobileprice)
</td>
</tr>
<tr>
<td colspan="2">
<input id="Submit1" onclick="return confirm('Are you sure you want delete');" type="submit"
value="Delete" />
</td>
</tr>
</table>
}
SHOW
@model MyMvcApplication.Models.Mobiledata
@{
ViewBag.Title = "ShowAllMobileDetails";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>ShowAllMobileDetails</h2>
<table>
<tr>
<td>
@Html.ActionLink("Add New Mobiles", "InsertMobile")
</td>
</tr>
</table>
@{
for (int i = 0; i < Model.StoreAllData.Tables[0].Rows.Count; i++)
{
var MobileID = Model.StoreAllData.Tables[0].Rows[i]["MobileID"].ToString();
var MobileName = Model.StoreAllData.Tables[0].Rows[i]["MobileName"].ToString();
var MobileIMEno = Model.StoreAllData.Tables[0].Rows[i]["MobileIMEno"].ToString();
var Mobileprice = Model.StoreAllData.Tables[0].Rows[i]["mobileprice"].ToString();
var MobileManufacured = Model.StoreAllData.Tables[0].Rows[i]["mobileManufacured"].ToString();
<table width="100%">
<tr>
<td>
MobileID
</td>
<td>
MobileName
</td>
<td>
Mobile IMEI No
</td>
<td>
Mobileprice
</td>
<td>
Mobile Manufactured
</td>
<td>
EDIT
</td>
<td>
DELETE
</td>
</tr>
<tr>
<td>
@MobileID
</td>
<td>
@MobileName
</td>
<td>
@MobileIMEno
</td>
<td>
@Mobileprice
</td>
<td>
@MobileManufacured
</td>
<td>
@Html.ActionLink("EDIT", "EDITMOBILEDATA", new { id = Model.StoreAllData.Tables[0].Rows[i]["MobileID"].ToString() })
</td>
<td>
@Html.ActionLink("Delete", "DELETEMOBILEDATA", new { id = Model.StoreAllData.Tables[0].Rows[i]["MobileID"].ToString() })
</td>
</tr>
<tr>
<td>
@Html.ActionLink("Add New Mobiles", "InsertMobile")
</td>
</tr>
</table>
}
}