C# - 如何打印出viewdata?

时间:2015-12-29 20:23:34

标签: 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.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 "视图中用户的所有位置?我尝试了很多东西,但到目前为止似乎没有任何工作。

2 个答案:

答案 0 :(得分:2)

  

例如,如何打印" locname"所有的位置   用户在视图上?

哦,尝试一下。 C#中的匿名类型仅限于当前方法。因此,在每个ASP.NET MVC应用程序中,请首先定义一些视图模型模型,该模型将描述您将在视图中使用的数据:

public class MyModel
{
    public string LocationName { get; set; }
}

您将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 };

好的,既然你知道你投射的是什么,你可以在你的视图中使用这种类型:

@foreach (var model in (IEnumerable<MyViewModel>)ViewData["locations"])
{
    <div>@model.LocationName</div>
}

到目前为止,我所展示的代码当然只是在您的情况下可以完成的第一步改进。它只适合初学者。显然,永远不应该写这样的代码,因为这是对人类的犯罪。执行此操作的正确方法是使用实​​际视图模型进行投影:

public class Location
{
    public string Name { get; set; }
}

public class Competence
{
    public string Name { get; set; }
}

public class MyViewModel
{
    public User User { get; set; }
    public IEnumerable<Location> Locations { get; set; }
    public IEnumerable<Competence> Competences { get; set; }
}

然后在针对此模型的控制器操作项目中:

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

    User user = db.user.Find(id);

    if (user == null)
    {
        return HttpNotFound();
    }

    var competences = 
        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 Competence { Name = comp.competentie };

    var 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 Location { Name = loc.loc_name };

    var model = new MyViewModel
    {
        User = user,
        Locations = locations.ToList(), // eagerly fetch the data that will be needed in the view
        Competences = competences.ToList(), // eagerly fetch the data that will be needed in the view
    }

    return View(model);
}

并且显然最后一步是将您的视图强烈输入到我们刚刚定义的视图模型中:

@model MyViewModel

好的,现在,您可以轻松访问所需的任何信息

@foreach (var location in Model.Locations)
{
    <div>@location.Name</div>
}

或其他:

@foreach (var competence in Model.Competences)
{
    <div>@competence.Name</div>
}

或者如果你想问候你的用户或其他什么:

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

当您在ASP.NET MVC视图中使用强类型视图模型时,您甚至会发现Visual Studio有一些相当不错的IntelliSense,因此您不应该依赖于某些动态转换和类似刻度的内容-tacking炸弹机制等待在运行时(或你的用户面孔)爆炸。

所以这篇文章的结论是你不应该在ASP.NET MVC应用程序中使用ViewDataViewBag。这两件事就像癌症应该被根除。在ASP.NET MVC解决方案中搜索这些关键字( Ctrl + Shift + F ),如果存在,请操作它们。

答案 1 :(得分:0)

如果由于某种原因你不想使用ViewModel,你可以这样做:

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

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

要显示该属性的数据,请执行以下操作:

rm -rf app/cache/* && rm app/logs/prod.log
相关问题