How to Create Mvc Db

Only available on StudyMode
  • Download(s) : 46
  • Published : February 3, 2013
Open Document
Text Preview
MVC DATABASE APPLICATION USING C# CODE

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Pharmacy.Models;

namespace Pharmacy.Controllers
{
public class HomeController : Controller
{
private MedicineEntities _datamodel = new MedicineEntities(); public ActionResult Index()
{
return View(_datamodel.MDs.ToList());
}

//
// GET: /Home/Details/5

public ActionResult Details(int id)
{
var prod = _datamodel.MDs.Where(item => item.id == id).First(); return View(prod);
}

//
// GET: /Home/Create

public ActionResult Create()
{
return View();
}

//
// POST: /Home/Create

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Exclude = "ID")] MD productToCreate) {
if (!ModelState.IsValid)
return View();
try
{
_datamodel.AddToMDs(productToCreate);
_datamodel.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}

//
// GET: /Home/Edit/5

public ActionResult Edit(int id)
{
return View(_datamodel.MDs.FirstOrDefault(item => item.id == id)); }

//
// POST: /Home/Edit/5

[HttpPost]
public ActionResult Edit(MD productToCreate)
{
try
{
var prod = _datamodel.MDs.Where(item => item.id == productToCreate.id).First(); prod.medicine_name = productToCreate.medicine_name; prod.description = productToCreate.description; prod.quantity = productToCreate.quantity;

prod.price = productToCreate.price;
_datamodel.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}

//
// GET: /Home/Delete/5

public ActionResult Delete(int id)
{
return View(_datamodel.MDs.FirstOrDefault(item => item.id == id)); }

//
// POST: /Home/Delete/5

[HttpPost,ActionName("Delete")]
public ActionResult Delete(MD productToDelete)
{
try
{
var prod = _datamodel.MDs.Where(item => item.id == productToDelete.id).First(); _datamodel.MDs.DeleteObject(prod);
_datamodel.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}

MVC DATABASE APPLICATION USING VB CODE

Namespace PharmacyVB
Public Class HomeController
Inherits System.Web.Mvc.Controller
Private _dataModel As New MedicineEntities()

'
' GET: /Home

Function Index() As ActionResult
Return View(_dataModel.MDs.ToList)
End Function

'
' GET: /Home/Details/5

Function Details(ByVal id As Integer) As ActionResult Dim originalMedicineInfo = (From p In _dataModel.MDs Where p.id = id Select p).FirstOrDefault() Return View(originalMedicineInfo)

End Function

'
' GET: /Home/Create

Function Create() As ActionResult
Return View()
End Function

'
' POST: /Home/Create

<HttpPost()> _
Function Create(<Bind(Exclude:="Id")> ByVal productToCreate As MD) As ActionResult If Not ModelState.IsValid Then...
tracking img