Question

I've created a DB application in MVC 4 using EntityFramework. It works okay, however I couldn't find the way of highlighting new added item to my table(html). Is it possible to highlight new rows in DB applications in MVC? Like in C#:

DataGrid.SelectedItem 

Any advice or help would be very clarifying. How to highlight a row in MVC?

Was it helpful?

Solution

How are you rendering the table?

One option is to use TempData. Store the identifier of the added item in TempData and check for the identifier when rendering the table.

ItemController:

// POST: /Item/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include="Id,Name")] Item item)
{
    if (ModelState.IsValid)
    {
        db.Items.Add(item);
        db.SaveChanges();

        TempData["AddedItemId"] = item.Id;
        return RedirectToAction("Index");
    }

    return View(item);
}

Item view:

<!-- Views\Item\Index.cshtml -->
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        var className = "";

        if (TempData["AddedItemId"] != null && (int)TempData["AddedItemId"] == item.Id)
        {
            className = "alert-info";
        }
        <tr class="@className">
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <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>
        </tr>
    }
</table>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top