문제

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?

도움이 되었습니까?

해결책

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>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top