?
Solved

@Html.DisplayFor -- fails, ASP.net, MVC

Posted on 2013-10-28
9
Medium Priority
?
2,087 Views
Last Modified: 2013-11-01
How can I fix the below "item.LastName" so it displays ?
Everything else works.
-----------------------------------------------------------------------------------------------------------------------

@model IEnumerable<MWR.Models.UserProfile>
@using MWR.Data;
@using MWR.Models;

@{
    ViewBag.Title = "Delete";
}

<h2>Create / Delete Users</h2>
<h6>@Html.ActionLink("Admin Home", "Index", new {})</h6>
<h6>@Html.ActionLink("Create User", "Register", "Account") </h6>

@using (Html.BeginForm("Users", "UserRole"))
{
   
    <table>
        <tr>

            <th></th>
            <th>User Name</th>


        </tr>

        @foreach (var item in Model)
        {
            <tr>
                <td>
                    <input type="checkbox"  name="ids" value="@item.UserId"  />
                </td>
                <td>
                    @Html.HiddenFor(userId => item.UserId)
                    @Html.ActionLink(@item.UserName, "GetRolesbyUser", new { id=item.UserId })
                </td>
                <td>
                    @Html.DisplayFor(userId => item.UserId)
                    @Html.DisplayFor(modelItem => item.LastName) -- this line displays BLANK, but has database values
                </td>
            </tr>
        }

    </table>
    <input type="submit" id="btnUpdate" value="Delete" />
}
0
Comment
Question by:finance_teacher
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 16

Expert Comment

by:Stephan
ID: 39607936
Is the "UserProfile" model an entity and when you debug the line (yes you can debug a view), do you see a value? Maybe it is not set in your viewmodel?
0
 

Author Comment

by:finance_teacher
ID: 39608412
How can I fix since the userProfile table is NOT in
Model1.edmx and should display all FIELDS correctly ?

"@foreach (var item in Model)" shows that all 28 "Model" records have BLANK FirstName/LastName.
------------------------------------------------------------------------------------------------------------
AccountModels.cs has the fields below, which are in my database.

    [Table("UserProfile")]
    public class UserProfile
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int UserId { get; set; }
        public string UserName { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public Int64 Badge { get; set; }
    }
0
 
LVL 16

Expert Comment

by:Stephan
ID: 39608422
Is the UserProfile class filled by a certain method or should be done by the EF code-first?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:finance_teacher
ID: 39608499
UserProfile class is the STANDARD method that gets automatically created when you do a new MVC4 ASP.net app
0
 
LVL 16

Expert Comment

by:Stephan
ID: 39608536
I'm trying to get some things straight.
You say you have created an MVC app with EF code-first AND you have a "Model1.edmx"?

When I create a new MVC 4 project, I don't have FirstName and LastName.
[Table("UserProfile")]
    public class UserProfile
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int UserId { get; set; }
        public string UserName { get; set; }
    }

Open in new window


So you probably added those later in the UserProfile table.
Is it using the same database?
And what's inside Model1.edmx?
0
 

Author Comment

by:finance_teacher
ID: 39608558
Correct, I added these two fields to my UserProfile table and classes later.

Model1.edmx only has my non-USER-related tables (i.e. CUSTOMER ORDERS, etc)
0
 

Author Comment

by:finance_teacher
ID: 39608874
Any ideas on how I can fix the BLANK results ?
0
 
LVL 16

Accepted Solution

by:
Stephan earned 2000 total points
ID: 39612601
Sorry for my late response, I was trying to reproduce the issue on my work but I don't have the correct sql version. So now from home I took a look at it.

But I am unable to reproduce it.

I did the following:
- Create a new MVC4 internet project in VS2012
- Register an account
- Add column "FirstName" in the table UserProfile using SQL.
- Fill the column with information so we have data for "FirstName"
- Add a property "FirstName" and added this to the "UserProfile" class
- Finally modify the Index.cshtml view for displaying the "First Name".  
      Html.DisplayFor(modelItem => item.FirstName)

Can you check if you did thesame or maybe more/less?
0
 

Author Comment

by:finance_teacher
ID: 39616674
I needed to change my below http://localhost/UserRole/Users 
"dataDB.getAllUserProfiles();" method to return FirstName/etc
and it works now.
------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MWR.Models;
using System.Configuration;
using System.Data.SqlClient;
using MWR.Data;

namespace MWR.Controllers
{
    public class UserRoleController : Controller
    {

        string result = string.Empty;
        UserProfile userProfile = null;
        UserRoleModel userRoleModel = null;
        List<UserRoleModel> listUserRole = new List<UserRoleModel>();

        List<UserProfile> listUserProfile = new List<UserProfile>();
        dataDB dataDB = new dataDB();


        public ActionResult Users()
        {
            listUserProfile = dataDB.getAllUserProfiles();
            if (listUserProfile.Count() > 0)
                return View(listUserProfile);
            else
                return View();

        }



    }
}
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question