?
Solved

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

Posted on 2013-10-28
9
Medium Priority
?
2,010 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
Use Filtering Commands to Process Files in Linux

Learn how to manipulate data with the help of various filtering commands such as `cat`, `fmt`, `pr`, and others in Linux.

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

765 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