Как отправить данные из одного представления в другое, это марка, модель и т. Д. c. Это не форма - PullRequest
1 голос
/ 07 апреля 2020

Это модель автомобиля:

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; }
}

Viewmodel:

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>

}

Когда пользователь нажимает на кнопку «Двигаться сейчас», он переходит к просмотру деталей, в котором указаны c подробности автомобиля. Но я не могу понять, как это сделать.

1 Ответ

0 голосов
/ 07 апреля 2020

Вы можете отправить свои данные через параметры URL и использовать GET для действия вашего контроллера.

Документы

Передать несколько параметров

@Html.ActionLink("Drive Now","actionName","controllerName", new{ @make=item.Make, @model = item.Model }, new { });

или просто идентификатор автомобиля

@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
}
...