locked
list of users RRS feed

  • Question

  • hi

    i use code first in mvc5

    i have feild id_city in ApplicationUser class of IdentityModels.

    i have Index Action for show list of users:

    public ActionResult Index()
            {
                var Db = new ApplicationDbContext();
                var users = Db.Users;
                //ViewModel will be posted at the end of the answer
                var model = new List<EditUserViewModel>();
                foreach (var user in users)
                {
                    var u = new EditUserViewModel(user);
                    model.Add(u);
                }
                return View(model);
            }

    model :

    public class ApplicationUser : IdentityUser
        {
    public string Name { get; set; }
    
            public string family{ get; set; }
    
    public Guid city_Id { get; set; }
    
            public string Address { get; set; }
    
    [ForeignKey("city_Id")]
            public virtual city city{ get; set; }
    }

    public class EditUserViewModel { public EditUserViewModel() { } // Allow Initialization with an instance of ApplicationUser: public EditUserViewModel(ApplicationUser user) { this.UserName = user.UserName; this.Name = user.Nam; this.family = user.Namkh; this.city_Id = user.city_Id; this.Password = user.PasswordHash; this.Address = user.Address; } [Key] public string Id { get; set; } public string UserName { get; set; } public string Password { get; set; } public string ConfirmPassword { get; set; } public string Name { get; set; } public string family{ get; set; } public Guid city_Id { get; set; } public string Address { get; set; } [ForeignKey("city_Id")] public virtual city city{ get; set; } }

    public class city
        {
            
            public Guid Id { get; set; }
            
            
            
            public string namecity{ get; set; }
            
            
            public ICollection<ApplicationUser> ApplicationUser { get; set; }
    
        }

    @model IEnumerable<Project.Models.EditUserViewModel>
    
    @{
        ViewBag.Title = "Index";
    }
    
    <h2>Index</h2>
    
    <p>
        @Html.ActionLink("Create New", "Create")
    </p>
    <table class="table">
        <tr>
            
            <th>
                @Html.DisplayNameFor(model => model.Name)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.family)
            </th>
            
            
            <th>
                @Html.DisplayNameFor(model => model.UserName)
            </th>
            
            
            
            <td>
                @Html.DisplayFor(model => model.city_Id)
            </td>
            <th></th>
        </tr>
    
    @foreach (var item in Model) {
        <tr>
            
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.family)
            </td>
            
            
            <td>
                @Html.DisplayFor(modelItem => item.UserName)
            </td>
            
            
            
            <td>
                @Html.DisplayFor(modelItem => item.city.namecity)
            </td>
            
            <td>
                @Html.ActionLink("Edit", "Edit", new { id=item.UserName }) |
                @Html.ActionLink("Details", "Details", new { id=item.UserName }) |
                @Html.ActionLink("Delete", "Delete", new { id=item.UserName })
            </td>
        </tr>
    }
    
    </table>
    but name of city is empty.

    please help me for edit my code.

    thanks


    • Edited by Neda.IT Monday, May 26, 2014 3:42 AM
    • Moved by Anne Jing Tuesday, May 27, 2014 7:24 AM off-topic
    Monday, May 26, 2014 3:37 AM

Answers

  • Ask on forums.asp.net. this forum is for language issues, not for class libraries.


    Visual C++ MVP

    • Proposed as answer by Just Karl Wednesday, May 28, 2014 2:40 PM
    • Marked as answer by Just Karl Wednesday, June 4, 2014 6:52 PM
    Monday, May 26, 2014 3:58 AM

All replies

  • Ask on forums.asp.net. this forum is for language issues, not for class libraries.


    Visual C++ MVP

    • Proposed as answer by Just Karl Wednesday, May 28, 2014 2:40 PM
    • Marked as answer by Just Karl Wednesday, June 4, 2014 6:52 PM
    Monday, May 26, 2014 3:58 AM
  • Hello,

    Specifically, this should be asked in the ASP.Net MVC forum on forums.asp.net.

    Karl


    When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer.
    My Blog: Unlock PowerShell
    My Book: Windows PowerShell 2.0 Bible
    My E-mail: -join ('6F6C646B61726C40686F746D61696C2E636F6D'-split'(?&lt;=\G.{2})'|%{if($_){[char][int]&quot;0x$_&quot;}})

    Wednesday, May 28, 2014 2:41 PM