How to use ViewModel to the detail View in Mvc c#

Viewed 36

This is a detail page in the view and it was created by default using the scaffolder controller with a view. Now need to use view model to pass data to the view, and on detail page, it has two information need to display are expiationOffenceCode and ExpiationOffenceDescription:

@model SAExpiations.Models.ExpiationOffence

@{
    ViewData["Title"] = "Details";
}

<h1>ExpiationOffence Details</h1>
<select name="select" class="form-selected" id="dropdownYear" style="width: 200px;" onchange="showList()">
</select>

<div>
    <hr />
    <dl class="row">
        <dt class="col-sm-3">
            @Html.DisplayNameFor(model => model.ExpiationOffenceCode)
        </dt>
        <dd class="col-sm-9">
            @Html.DisplayFor(model => model.ExpiationOffenceCode)
        </dd>
        <dt class = "col-sm-3">
            @Html.DisplayNameFor(model => model.ExpiationOffenceDescription)
        </dt>
        <dd class = "col-sm-9">
            @Html.DisplayFor(model => model.ExpiationOffenceDescription)
        </dd>
    </dl>
</div>
<div>
    <a asp-action="Edit" asp-route-id="@Model?.ExpiationOffenceCode" class="btn btn-primary">Edit</a> |
    <a asp-action="Index" class="btn btn-dark">Back to List</a>
</div>

The detail action in the controller

public async Task<IActionResult> Details(string id)
{
     if (id == null || _context.ExpiationOffences == null)
     {
            return NotFound();
     }

     var expiationOffence = await _context.ExpiationOffences
                                          .FirstOrDefaultAsync(m => m.ExpiationOffenceCode == id);

     if (expiationOffence == null)
     {
          return NotFound();
     }

     return View(expiationOffence);
}
0 Answers
Related