Just another tech site

MVC Tutorial PART 1: CRUD


A simple reminder of the diferent steps.
nothhing fancy here juste the CRUD features

The project: Simple Admin Site with DataEntity

Step 1: Build the model

The dataEntity from the database for example. Let’s focus on one table the “TestPlan”

The “TestPlan” entity will refer to the user table. This will show just the part that will interest us in this first part

Step 2: Add the Controler

Select the option to create the default CRUD functions
Let’s call the controler “TestPlanController ”

    public class TestPlanController : Controller
    {

Step 3: Add the basic Operations

Using the strongly-typed view

You should have the diferent views

Step 4: Fill the Controler

Step 4.1: Initialise the model

private WatchTestEntities _db;
public TestPlanController()
{
_db = new WatchTestEntities();
}

Step 4.2: the list

The output

The View:
just the list

<% foreach (var item in Model) { %>

    <tr>
        <td>
            <%= Html.ActionLink("Edit", "Edit", new { id=item.id }) %> |
            <%= Html.ActionLink("Details", "Details", new { id=item.id })%> |
            <%= Html.ActionLink("Delete", "Delete", new { id=item.id })%>
        </td>
        <td>
            <%= Html.Encode(item.id) %>
        </td>
        <td>
            <%= Html.Encode(item.name) %>
        </td>
        <td>
            <%= Html.Encode(item.description) %>
        </td>
        <td>
            <%= Html.Encode(item.enable) %>
        </td>
    </tr>

<% } %>

The controler:

public ActionResult Index()
{
var model = _db.TST_TestPlan.ToList();
return View(model);
}

or

        public ActionResult Index()
        {
            ViewData.Model = _db.TST_TestPlan.ToList();
            return View();
        }

Step 4.3: the edit

From the autogenerated form you just need to change two controls:

  • Hide the ID (remember, this is the IDENTITY field in the DB)
  • Use the CHECKBOX

The view code:

The controler

public ActionResult Edit(int id)
{
    ViewData.Model = _db.TST_TestPlan.First(m => m.id == id);
    return View();
}

[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
    try
    {
        var tempObject = _db.TST_TestPlan.First(m => m.id == id);
        UpdateModel(tempObject, new[] { "name", "description", "enable" });
        //UpdateModel(tempObject, collection.ToValueProvider());
        _db.SaveChanges();

        return RedirectToAction("Index");
    }
    catch
    {
        return View();
    }
}

Step 4.4: Delete the item

public ActionResult Delete(int id)
{
     ViewData.Model = _db.TST_TestPlan.First(m => m.id == id);
     return View();
}

[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
    try
    {
        var tempObject = _db.TST_TestPlan.First(m => m.id == id);
        tempObject.enable = false;
        _db.SaveChanges();

        return RedirectToAction("Index");
    }
    catch
    {
        return View();
    }
}

Step 4.5: Create new item

The view will look like the Edit view
The controler:

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

[HttpPost]
public ActionResult Create(FormCollection collection)
{
    try
    {
        var TestPlan = new TST_TestPlan();
        TestPlan.enable = true;
        TestPlan.name = collection["name"];
        TestPlan.description = collection["description"];
        TestPlan.TST_User = _db.TST_User.Where(m => m.username == "admin").First();

        _db.AddToTST_TestPlan(TestPlan);
        _db.SaveChanges();

        return RedirectToAction("Index");
    }
    catch(Exception ex)
    {
        return View();
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Tag Cloud

%d bloggers like this: