C#-如何打印视图数据?

C#-如何打印视图数据?,c#,asp.net,asp.net-mvc,view,C#,Asp.net,Asp.net Mvc,View,我想知道如何在视图上打印viewdata 下面是我如何传递视图数据的 public ActionResult Details(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } User user = db.user.Fin

我想知道如何在视图上打印viewdata

下面是我如何传递视图数据的

 public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            User user = db.user.Find(id);

            if (user == null)
            {
                return HttpNotFound();
            }
            ViewData["competenties"] = from usercomp in dbE.UserComp
                                       join comp in dbE.Competenties on usercomp.fk_Comp_id equals comp.comp_id
                                       where usercomp.fk_user_id == id
                                       select new { compname = comp.competentie };

            ViewData["locations"] = from userloc in dbE.UserLoc
                                    join loc in dbE.Locations on userloc.fk_location_id equals loc.loc_id
                                    where userloc.fk_user_id == id
                                    select new { locname = loc.loc_name };
            return View(user);
        }
例如,如何在视图上打印用户所有位置的“locname”?我尝试了很多方法,但到目前为止似乎没有任何效果

例如,我如何打印一个文件的所有位置的“locname” 视图上的用户

哦,再努力一点。C#中的匿名类型的作用域仅限于当前方法。因此,与在每个ASP.NET MVC应用程序中一样,请首先定义一些视图模型来描述您将在视图中使用的数据:

public class MyModel
{
    public string LocationName { get; set; }
}
@foreach (var model in (IEnumerable<MyViewModel>)ViewData["locations"])
{
    <div>@model.LocationName</div>
}
您将LINQ查询投影到:

 ViewData["locations"] = 
     from userloc in dbE.UserLoc
     join loc in dbE.Locations on userloc.fk_location_id equals loc.loc_id
     where userloc.fk_user_id == id
     select new MyViewModel { LocationName = loc.loc_name };
好的,现在您已经知道了您所针对的是什么,您可以在视图中使用这种类型:

public class MyModel
{
    public string LocationName { get; set; }
}
@foreach (var model in (IEnumerable<MyViewModel>)ViewData["locations"])
{
    <div>@model.LocationName</div>
}
显然,最后一步是将视图强类型化到我们刚刚定义的视图模型:

@model MyViewModel
好的,现在访问您需要的任何信息都非常容易

@foreach (var location in Model.Locations)
{
    <div>@location.Name</div>
}
@foreach(Model.Locations中的变量位置)
{
@地点。姓名
}
或其他:

@foreach (var competence in Model.Competences)
{
    <div>@competence.Name</div>
}
@foreach(模型能力中的var能力)
{
@能力.姓名
}
或者,如果您想向您的用户或其他人打招呼:

<div>
    Hello @Model.User.FirstName and welcome back to my super duper website
</div>

您好@Model.User.FirstName,欢迎回到我的super duper网站
当您在ASP.NET MVC视图中使用强类型视图模型时,您甚至会发现Visual Studio具有相当不错的智能感,因此您不应该依赖于某些动态强制转换和类似于等待在运行时在您的脸(或您的用户脸)上爆炸的打勾炸弹机制的东西


因此,本文得出的结论是,在ASP.NET MVC应用程序中,绝对不能使用
ViewData
ViewBag
。这两件事就像癌症,应该根除。在ASP.NET MVC解决方案(Ctrl+Shift+F)中搜索这些关键字,如果出现,请对其进行操作。

如果出于某种原因不想使用ViewModel,可以执行以下操作:

在视图的开头声明了一些对象

@{
    Layout = null;
    var myObject = ViewData["locations"];
    var properties = myObject.GetType().GetProperties();
}
要显示房地产的数据,请执行以下操作:

<label>@myObject.GetType().GetProperties().FirstOrDefault(x => x.Name == "locname").GetValue(myObject)</label>
@myObject.GetType().GetProperties().FirstOrDefault(x=>x.Name==“locname”).GetValue(myObject)

创建视图模型并将其传递给视图不是更容易,而不是执行ViewData吗?感谢您的回答,这非常有帮助嗨,我听从了您的建议,我开始理解结构,尽管我有错误“实体或复杂类型'P104.Models.Locations'不能在LINQ to Entities查询中构造”,有什么建议吗?我基本上遵循了您的代码。