public ActionResult Index()
{
var Grp = context.ContactGroups;
return View(Grp);
}
//
// GET: /Home/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Home/Create
[HttpPost]
public ActionResult Create(ContactGroup Grp)
{
if (ModelState.IsValid)
{
try
{
context.AddToContactGroups(Grp);
context.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View(Grp);
}
}
else
{
return View(Grp);
}
}
//
// GET: /Home/Edit/5
public ActionResult Edit(int id)
{
var Grp = context.ContactGroups.SingleOrDefault(x => x.GroupId == id);
return View(Grp);
}
//
// POST: /Home/Edit/5
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
var Grp = context.ContactGroups.SingleOrDefault(x => x.GroupId == id);
try
{
UpdateModel(Grp, collection.ToValueProvider());
context.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
//
// GET: /Home/Delete/5
public ActionResult Delete(int id)
{
var Grp = context.ContactGroups.SingleOrDefault(x => x.GroupId == id);
return View(Grp);
}
//
// POST: /Home/Delete/5
[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
var Grp = context.ContactGroups.SingleOrDefault(x => x.GroupId == id);
try
{
context.DeleteObject(Grp);
context.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
Saturday, January 15, 2011
Asp .Net Mvc with Entity Framework
Friday, April 30, 2010
Asp .Net MVC 2 with Visual Studio 2010 or Visual Web Developer 2010
Asp .net Mvc is the Web Development framework from Microsoft based on
the MVC architecture.
Asp .Net MVC is getting installed as a part of Visual Studio 2010 and
Visual Web Developer 2010.
Asp .Net 2 MVC Empty Web Application Project
Asp .Net MVC 2 Web Application comes with predefined
Membership and other frills which makes it easy
to get a head start, but abstracts away the inner working of MVC.
But I am taking another route. There is another
Asp .Net MVC 2 Empty Web Application Project.
Lets start with Controllers. Controllers are responsible for handling
incoming request. It's a class that inherits from System.Web.Mvc.Controller.
I am naming the controller HomeController.
I am replacing the existing code of HomeController which expects a View (which will be explained late) with a function that returns a string.
If you run the application you will see a web page with the value of function.
Views
Now Let's turn our attention to Views. A view is typically what you see in an Asp .Net MVC Application.
Preferably start a new new Asp .Net MVC 2 Empty Web Application so that we can start afresh.
The following diagram shows the default code within the HomeController class.
If you run the application you will receive a detailed error message stating that the view is missing.
So right click the Index method and select Add View.
Make sure you check off the Select MasterPage option because we haven't introduced Master Pages.
A view template called Index.aspx will created for you under ~/Views/Home/ directory.
Which comprises of plain HTML.
Now if you run the application you will receive a blank form.
Lets replace the existing code with the code below. The code will return a
ViewData structure
public ViewResult Index()
{
ViewData["Blog"] = "ShalvinPD.blogspot.com";
return View();
}
Now lets show the ViewData in the View.
Asp .Net MVC Views
Easiest way to develop Asp .Net MVC views are by using Html Helpers.
Name <%: Html.TextBox("Name") %>
<br />
<%: Html.CheckBox(".Net", true) %>.Net
<br />
<%: Html.RadioButton("Male", true) %>Male
<br />
<%: Html.RadioButton("Female", false) %>Female
<div>
Password <%: Html.Password("myPassword")%>
</div>
<div>
Address <%: Html.TextArea("Address", "Shalvin P D", 5, 20, null)%>
</div>
Kmug<%: Html.DropDownList("Kmug", new SelectList(new[] { "Shalvin", "Mathew" }), "Shalvin") %>
<br />
Technologies<%: Html.ListBox("myList", new SelectList(new[] { "Asp .Net", "Asp .Net Mvc" }), "Shalvin") %>
Friday, August 28, 2009
Asp .Net Mvc Resources
A list of Asp .Net Mvc Resources which I found very userful.
http://weblogs.asp.net/shijuvarghese/
ASP.NET MVC Tutorials
http://code-inside.de/blog-in/2008/11/25/howto-basics-of-aspnet-mvc-or-why-mvc/
ASP.NET MVC View Overview (C#)
DevConnections - The ASP.NET MVC Framework - Scott Hanselman
ASP.NET MVC using Visual Basic XML Literals
http://www.asp.net/learn/
ASP.NET MVC Preview 4 - Using Ajax and Ajax.Form
Free ASP.NET MVC eBook Tutorial
http://en.wikipedia.org/wiki/Model_View_Controller
http://en.wikipedia.org/wiki/Front_Controller_pattern
http://martinfowler.com/eaaCatalog/pageController.html
http://www.codeasp.net/articles/asp.net/30/aspnet-mvc-framework-tutorial
http://msdn.microsoft.com/en-us/magazine/cc337884.aspx (Old version)