C#-如何打印视图数据

本文关键字:视图 数据 打印 何打印 C#- | 更新日期: 2023-09-27 18:28:34

我想知道如何在视图中打印出视图数据。

以下是我如何沿传递视图数据

 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"?我尝试了很多方法,但到目前为止似乎都不起作用。

C#-如何打印视图数据

例如,我如何打印视图中的用户?

哦,努力一点。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,所以你不应该依赖于一些动态类型转换和类似于滴答作响的炸弹机制的东西,等待在运行时在你的脸上(或你的用户脸上)爆炸。

因此,本文的结论是,您永远不应该在ASP.NET MVC应用程序中使用ViewDataViewBag。这两样东西就像癌症一样应该被根除。在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>