我正在传递ViewBag,
var forms = SqlMapper.Query(con, "getDetails", param, commandType: CommandType.StoredProcedure);
ViewBag.myForm = forms;
这种形式'包含VehicleType,VehicleName,VehiclePrice等值 我正在使用循环显示那些视图,
@foreach (var frm in ViewBag.myForm)
{
<div class="form-group">
<label for="exampleInputEmail1">@frm.VehicleType</label>
<label for="exampleInputEmail1">@frm.VehicleName</label>
<label for="exampleInputEmail1">@frm.VehiclePrice</label>
</div>
}
这很好用。现在我想在标题中显示VehicleType。而且我不想再次循环ViewBag来获取VehicleType。 VehicleType与列表相同。就好像我得到了汽车一样。作为VehicleType,那么对于那个ViewBag,&#39; Car&#39;是唯一的VehicleType。如何在不循环ViewBag的情况下阅读VehicleType?感谢
为了更多地了解数据,
Vehicle Type | Name | Price
Car | Ford | 1000000
Car | Audi | 1500000
Car | Chev.| 1250000
Car | Volks| 1246000
我想显示车辆类型
<div class="box-header with-border">
<h3 class="box-title">@ViewBag.myForm ----- VehicleType</h3>
</div>
答案 0 :(得分:2)
您应该使用模型而不是ViewBag。这样您就不必循环遍历列表以获得一个VehicleType
<强> Vehicle.cs 强>
public class Vehicle
{
public int VehicleID {get; set;}
public string VehicleType {get; set;}
public string VehicleName {get; set;}
public double VehiclePrice {get; set;}
}
<强>控制器强>
var forms = SqlMapper.Query(con, "getDetails", param, commandType: CommandType.StoredProcedure);
List<Vehicle> vehicleList = new List<Vehicle>();
vehicleList = forms;
查看强>
@model IEnumerable<ProjName.Models.Vehicle>
@Html.DisplayFor(model => model.ElementAt(0).VehicleType)
@foreach (var frm in Model)
{
<div class="form-group">
<label for="exampleInputEmail1">@frm.VehicleName</label>
<label for="exampleInputEmail1">@frm.VehiclePrice</label>
</div>
}