Solved

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

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

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now