首页 > 解决方案 > 如何将数据从一个视图发送到另一个视图,它是品牌、型号等。它不是一种形式

问题描述

这是汽车模型:

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>

}

当用户点击立即驾驶按钮时,它会转到详细信息视图,其中包含汽车的具体详细信息。但我似乎无法弄清楚该怎么做。

标签: asp.netasp.net-mvcrazor

解决方案


您可以通过 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
}

推荐阅读