简体   繁体   中英

How to show other model column in Razor view?

Currently i'm able to show all column in razer view from userRole model. Just curious if i like to show the SiteName column in UserRole Razor view, instead of showing SiteID, is it possible? I know it can be done via custom view model, but is it a must? Please correct me if i'm wrong!

UserRole Model:

    public int UserID { get; set; }
    public string UserName { get; set; }
    public int SiteID { get; set; }
    *No SiteName column here....so i only can show SiteID in razor..

Site Model :

public int SiteID { get; set; }
public string SiteName { get; set; } <<-- the column i want..

Controller:

public ActionResult Index()

    {
         //need join table here perhaps?

         return View(db.User_Role.ToList());
    }

Razor View:

@model IEnumerable<FAB_Portal_POC.Models.UserRole>

<table class="table table-striped table-hover">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.UserName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Role)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.SiteID)
        </th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.UserName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Role)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.SiteID) <<-- i want site name.
        </td>      
    </tr>
}
</table>

The first problem is that your UserRole entity model doesn't seem to have a navigation property.

You don't need to, but then querying becomes awkward:

var rolesWithSite = from r in db.User_Role
                    join s in db.Sites on s.ID equals r.Site_ID
                    select new
                    {
                        Role = r,
                        Site = s
                    }
                    .ToList();

While when you add a navigation property (perhaps even instead of the foreign key property):

public class User_Role
{
    public int UserID { get; set; }
    public string Role { get; set; }
    public string UserName { get; set; }
    public virtual Site Site { get; set; }
}

Querying will become a whole lot easier:

var rolesWithSite = db.User_Role.Include(r => r.Site).ToList();

You can then introduce a viewmodel:

public class UserRoleViewModel
{
    public int UserID { get; set; }
    public string UserName { get; set; }
    public string Role { get; set; }
    public string Site { get; set; }
}

And map the query results to that:

var viewModels = rolesWithSite.Select(r => new UserRoleViewModel
{
    UserID = r.UserID,
    UserName = r.UserName,
    Role = r.Role,
    Site = r.Site.SiteName,

}).ToList();

return View(viewModels);

Entities and Models are very different.

You have to create a specific model for your page.

something like this :

PageModel

public string UserName { get; set; }
public string SiteName { get; set; }

In your controller your have to prepare all the data you want to send to the page. It s here that you will "associate" your entities to your pagemodel

public ActionResult Index()

{
     var roles = db.User_Role.ToList();
     var sites = db.Sites.ToList(); // i don t know how you get this information in your exemple.

     //define here every property of the PageModel you want to use there
     var model = new PageModel();
     model.UserName = roles.UserName;
     model.SiteName = sites.Select(...).SiteName;

     return View(model);
}

then in razor view

@model IEnumerable<FAB_Portal_POC.Models.PageModel>

Hope it will help

You can store the sites list in the ViewBag and show the site name by extracting it from the ViewBag:

public ActionResult Index()
{
     ViewBag.Sites = db.Sites.ToList();
     return View(db.User_Role.ToList());
}

And the View:

<td>
    @Html.DisplayFor(modelItem => ViewBag.Sites.Single(s => s.SiteID == item.SiteID).SiteName)
</td>

Another, and to me a better approach, would be to create a ViewModel for your view which contains all of the data that you need. And return that view model to your view.

public class UserSiteViewModel
{
    public int UserID { get; set; }
    public string UserName { get; set; }
    public string Role { get; set; }
    public int SiteID { get; set; }
    public string SiteName { get; set; }
}

You have multiple ways to do that the simple one is you have to create new class name UserRoleViewData and mention these fields and pass the class object to view. just like that.

New Class UserRoleViewData Look like this

public int UserID { get; set; }
public string UserName { get; set; }
public int SiteID { get; set; }
public string SiteName { get; set; }

and your Controller

public ActionResult Index()

{
      List<className> dto = (from a in _db.User_Role
                 join b in db.Site on a.SiteID equals b.SiteID 
                  select (new classname { UserID =a.UserID , UserName =a.UserName , SiteID =a.SiteID , SiteName =b.SiteName })).ToList();
     return View(dto);
}

In View You have to change @model IEnumerable<FAB_Portal_POC.folderName.ClassName> and map other property on view like others.I hope u'll understand and this will help you.

No need to use ViewBag , join between 2 tables and store result in another viewModel it's vary bad idea. Please check out reference link, It will really help you to do Code in Very good direction. Click Here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM