Solved

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

Posted on 2013-10-28
9
1,849 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

770 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