asp.net - 如何将数据从一个视图发送到另一个视图,它是品牌、型号等。它不是一种形式
问题描述
这是汽车模型:
public partial class Car
{
public int CarID { get; set; }
public string Make { get; set; }
public string Model { get; set; }
public string Colour { get; set; }
public bool Availability { get; set; }
public double Mileage { get; set; }
public string RegNumber { get; set; }
public string EngineSize { get; set; }
public Nullable<decimal> LateFeeHourly { get; set; }
public int CarCategoryID { get; set; }
public string OutOnRent { get; set; }
public string ImagePath { get; set; }
public string ImagePath2 { get; set; }
public string ImagePath3 { get; set; }
public string ImagePath4 { get; set; }
public string ImagePath5 { get; set; }
public string ImagePath6 { get; set; }
public Nullable<decimal> DailyFee { get; set; }
public string Transmission { get; set; }
}
这是汽车类别模型:
public partial class CarCategory
{
public int CarCategoryID { get; set; }
public string CarCategoryName { get; set; }
public int NoOfSeat { get; set; }
}
视图模型:
public class CarVM
{
public List<Car> Cars { get; set; }
public List<CarCategory> CarCats { get; set; }
}
看法:
@foreach (var item in Model.Cars)
{
<div class="col-lg-4">
<div class="carGalleryWrapper">
<div class="carGallery" style="background:url('@Html.DisplayFor(modelItem=>item.ImagePath)') no-repeat 0 0 ">
<div class="carDetails">
<h4>@Html.DisplayFor(modelItem => item.DailyFee) / Per Day</h4>
<h3>@Html.DisplayFor(modelItem => item.Make) @Html.DisplayFor(modelItem => item.Model), Navi, Leather, ABS</h3>
<button type="submit" class="driveMore">Drive Now</button>
</div>
</div>
</div>
</div>
}
当用户点击立即驾驶按钮时,它会转到详细信息视图,其中包含汽车的具体详细信息。但我似乎无法弄清楚该怎么做。
解决方案
您可以通过 url 参数发送数据并在控制器操作上使用 GET。
传递多个参数
@Html.ActionLink("Drive Now","actionName","controllerName", new{ @make=item.Make, @model = item.Model }, new { });
或者只是汽车ID
@Html.ActionLink("Drive Now","actionName","controllerName", new{ @id = item.CarID }, new { });
完整代码;
<div class="carDetails">
<h4>@Html.DisplayFor(modelItem => item.DailyFee) / Per Day</h4>
<h3>@Html.DisplayFor(modelItem => item.Make) @Html.DisplayFor(modelItem => item.Model), Navi, Leather, ABS</h3>
@Html.ActionLink("Drive Now","actionName","controllerName", new{ @make=item.Make, @model = item.Model }, new { });
</div>
然后在您的目标控制器操作中,只需通过添加参数来捕获那些传递的值。
public ActionResult actionName(string make, string model){
}
或者
public ActionResult actionName(int id){
var car = db.Cars.FirstOrDefault(c=>c.CarId == id);
// car.Make
// car.Model
}
推荐阅读
- javascript - 如何为表格的每一行创建展开/折叠功能?角6
- javascript - JS。我想将函数存储到对象,然后将其用作匿名对象
- orientdb - 使用 Orientdb 3 和 tinkerpop 3 识别 2 个顶点是否与边缘连接
- python - 是否可以使用间隔查询 Pandas IntervalIndex
- visual-studio - Visual Studio 2017(专业版)脱机更新布局缺少包
- tensorflow - 权重矩阵提供什么信息?
- django - Django 后台任务间隔
- python - Django ManyToMany 字段返回 None 但它有相关记录
- c# - 定义两个可为空的隐式运算符时无法将 null 转换为 struct
- angular - Angular6:过滤与属性匹配的列表元素