简体   繁体   English

ASP.NET MVC 5视图无法访问模型属性

[英]ASP.NET MVC 5 view can't access model properties

When I try to use @Model.property , none of the properties that are in my ViewModel show up, so I can't select or display anything. 当我尝试使用@Model.property ,ViewModel中的所有属性都不会显示,因此我无法选择或显示任何内容。 I've searched on Google, and read that a restart of Visual Studio would help but it didn't. 我在Google上进行了搜索,并阅读到重新启动Visual Studio会有所帮助,但没有帮助。 I've rebuilt the solution, etc. as well, but nothing seems to help. 我也重建了解决方案,等等,但是似乎没有任何帮助。 What gives? 是什么赋予了?

Controller 控制者

public ActionResult GebruikersList()
{
    List<Gebruiker> gebruikers = db.Gebruikers.ToList();
    //List<GebruikerModel> gebruikersVM = Mapper.Map<List<Gebruiker>, List<GebruikerModel>>(gebruikers);

    GebruikerModel singleUser = Mapper.Map<Gebruiker, GebruikerModel>(db.Gebruikers.FirstOrDefault());

    return View(singleUser);
}

View: 视图:

@model alina1617.Models.GebruikerModel

@{
    ViewBag.Title = "GebruikersListTest";
}

<h2>GebruikersList</h2>


<p>
    @Model.
</p>

ViewModel: ViewModel:

public class GebruikerModel
{
    String Gebruikersnaam { get; set; }
    String Wachtwoord { get; set; }

    // Om automapper automatisch te laten converteren simpelweg klassenaam (FaculteitModel) + property (Naam) gebruiken
    String FaculteitModelNaam { get; set; }
    String InstellingModelMNaam { get; set; }

}

public class FaculteitModel
{
    String Naam { get; set; }
}                                                                                     

public class InstellingModel
{
    String naam { get; set; }
}

If your model properties are public you are able to access them within the view. 如果模型属性是公共的,则可以在视图中访问它们。 Hope this helps. 希望这可以帮助。

public class GebruikerModel
{
    public String Gebruikersnaam { get; set; }
    public String Wachtwoord { get; set; }

    // Om automapper automatisch te laten converteren simpelweg klassenaam (FaculteitModel) + property (Naam) gebruiken
    public String FaculteitModelNaam { get; set; }
    public String InstellingModelMNaam { get; set; }

}

    public class FaculteitModel
    {
        public String Naam { get; set; }
    }                                                                                     

    public class InstellingModel
    {
        public String naam { get; set; }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM