Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 访问传递到视图中的对象_C#_Asp.net Mvc_View - Fatal编程技术网

C# 访问传递到视图中的对象

C# 访问传递到视图中的对象,c#,asp.net-mvc,view,C#,Asp.net Mvc,View,我将对象(列表)传递到视图中,如下所示: private readonly CarsContext _db = new CarsContext(); public ActionResult Index() { ViewBag.Message = "Foobar"; var result = from cars in _db.CarProfiles where cars.CarOfTheWeek select

我将对象(列表)传递到视图中,如下所示:

private readonly CarsContext _db = new CarsContext();

public ActionResult Index()
{
    ViewBag.Message = "Foobar";

    var result = from cars in _db.CarProfiles
                 where cars.CarOfTheWeek
                 select cars;

    return View(result.ToList());
}
如何从索引视图中访问此对象?我想我需要将它添加到我的动态viewbag中,并以这种方式访问它,而不是将其作为参数传递,这是正确的吗?或者如果我保持控制器代码不变,我可以从视图中访问此对象吗


谢谢

在Index.cshtml视图中,您的第一行是:

@model YOUR_OBJECT_TYPE
@model String
@model List<CarProfile>
例如,如果在行中传递字符串:

return View("hello world");
那么Index.cshtml中的第一行是:

@model YOUR_OBJECT_TYPE
@model String
@model List<CarProfile>
在您的情况下,Index.cshtml中的第一行是:

@model YOUR_OBJECT_TYPE
@model String
@model List<CarProfile>
@型号列表
这样,如果在Index.cshtml视图中键入@Model,就可以访问CarProfile列表的所有属性@模型[0]将返回列表中的第一个CarProfile


提示一下,ViewBag大部分时间不应该被使用。相反,您应该创建ViewModel并将其返回到视图中。如果您想了解ViewModels,这里有一个链接:

在Index.cshtml视图中,第一行是:

@model YOUR_OBJECT_TYPE
@model String
@model List<CarProfile>
例如,如果在行中传递字符串:

return View("hello world");
那么Index.cshtml中的第一行是:

@model YOUR_OBJECT_TYPE
@model String
@model List<CarProfile>
在您的情况下,Index.cshtml中的第一行是:

@model YOUR_OBJECT_TYPE
@model String
@model List<CarProfile>
@型号列表
这样,如果在Index.cshtml视图中键入@Model,就可以访问CarProfile列表的所有属性@模型[0]将返回列表中的第一个CarProfile


提示一下,ViewBag大部分时间不应该被使用。相反,您应该创建ViewModel并将其返回到视图中。如果您想阅读有关ViewModels的内容,这里有一个链接:

从操作方法将视图强类型化为要返回的类/类型

在本例中,您将返回
CarProfile
类对象的列表。因此,在您的视图(index.cshtml)中,将其作为第一行删除

@model List<CarProfile>

假设
Name
CarProfile
类的属性。

从操作方法将视图强类型化为要返回的类/类型

在本例中,您将返回
CarProfile
类对象的列表。因此,在您的视图(index.cshtml)中,将其作为第一行删除

@model List<CarProfile>

假设
Name
CarProfile
类的属性。

您可以通过在ViewBag中传递对象(您编写控制器的方式)来访问视图中的对象。但我认为,通常最好有一个ViewModel,并通过该ViewModel将数据传递给view。 将业务对象直接传递给模型(正如您所做的那样),使模型依赖于业务对象,这在大多数情况下不是最优的。使用ViewModel代替ViewBag还有使用strong类型的优点

    private readonly CarsContext _db = new CarsContext();

    public ActionResult Index()
    {
        var result = from cars in _db.CarProfiles
                     where cars.CarOfTheWeek
                     select cars;

        MyViewModel myViewModel = new MyViewModel( );
        myViewModel.Message = "Foobar";
        myViewModel.ResultList = result.ToList();

        return View( myViewModel );
    }

您可以通过在ViewBag中传递对象(您编写控制器的方式)来访问视图中的对象。但我认为,通常最好有一个ViewModel,并通过该ViewModel将数据传递给view。 将业务对象直接传递给模型(正如您所做的那样),使模型依赖于业务对象,这在大多数情况下不是最优的。使用ViewModel代替ViewBag还有使用strong类型的优点

    private readonly CarsContext _db = new CarsContext();

    public ActionResult Index()
    {
        var result = from cars in _db.CarProfiles
                     where cars.CarOfTheWeek
                     select cars;

        MyViewModel myViewModel = new MyViewModel( );
        myViewModel.Message = "Foobar";
        myViewModel.ResultList = result.ToList();

        return View( myViewModel );
    }