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
Solved

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

Posted on 2013-10-28
9
1,872 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
  • 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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 500 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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used.

789 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