Question

I have created a form with a dropdown list for 'Region'. Once the form has been submitted I want to be able to view the details from the form. In particular I want the name of the 'Region' to be displayed but I am currently getting the name, ID and labels.

The dropdown list Controller:

public ActionResult Add()
{
    var db = new AssociateDBEntities();

    var region = db.Regions.Select(r => new { r.RegionId, r.RegionName });
    ViewBag.Regions = new SelectList(region.OrderBy(r => r.RegionName).AsEnumerable(), "RegionId", "RegionName");

    return View();
}

'Details' Controller:

public ActionResult Details(int id)
{
    using (var db = new AssociateDBEntities())
    {
        ViewData["Region"] = new SelectList(db.Regions.ToList(),"RegionName");

        return View(db.Associates.Find(id));
    }
}

'Details' View:

<div class="form-grid-1">
    <div class="display-label">@Html.LabelFor(model => model.Region)</div>
    <div class="display-field">@Html.DisplayFor(model => model.Region)</div>
</div>

How can I just get the name (RegionName) to display?

Was it helpful?

Solution

Just use

@Html.LabelFor(model => model.Region.RegionName)

Or create a DisplayTemplate for Region. Check out http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top