2

How to multiply in the View? Or have best Practices? Project - car rental. My controller takes parameter int numberDays. I need to calculate the total price on View for all days . Need multiply the price in one day the number of days. And then this parameter must be passed in the form of a controller.

public ActionResult DayView(int numberDays)
{
         //
}

In View in foreach I display numbercar,colorcar and other...

@model IEnumerable<Car>
foreach (Car car in Model)
{
    //other parameters
    <p>Full Price</p>:Model.CostOneDay      //need multiply numberDays
}

How <p>Full Price</p>:@(car.CostOneDay * Model.NumberOfDays) this parameter add in hiddenfor , I need pass this form to controller

andrey1567
  • 97
  • 1
  • 13
  • 1
    Instead of your model being an `IEnumerable` you can define a custom class which extends that to have the additional properties. At that point, you just access the property. – Eric Hotinger May 14 '15 at 13:59
  • Yes, what @EricHotinger said. The new class would then be a [ViewModel](http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) – Liam May 14 '15 at 14:00
  • You could use a [calculated property](http://stackoverflow.com/questions/2030636/c-sharp-read-only-calculated-properties-should-they-be-methods) in your model but I find that these only end up causing more problems than they solve in the long run. Just get your controller to calculate it into a VM – Liam May 14 '15 at 14:02

2 Answers2

1

To expand on the comment I posted, right now your controller looks like this:

public ActionResult DayView(int numberDays)
{
  // returns View with IEnumerable
}

You want to change this to be something like this, utilizing a new class, CalculatedDayViewModel (name this better) -- and have that class hold the calculated fields as properties. Be sure to set up a constructor which allows you to pass in numberDays and do the calculation.

public ActionResult DayView(int numberDays)
{
  return View(new CalculatedDayViewModel(numberDays));
}

Then in your Razor, change @model IEnumerable<Car> to be @model CalculatedDayViewModel and you'll have access to the calculated properties.

Eric Hotinger
  • 8,957
  • 5
  • 36
  • 43
1

You can create a view model class that contains Cars and NumberOfDays property

public class MyViewModel
{
    public IEnumerable<Car> Cars { get; set; }
    public int NumberOfDays { get; set; }
}

then use it in your controller and pass it to the view

public ActionResult DayView(int numberDays)
{
    MyViewModel model = new MyViewModel();
    model.NumberOfDays = numberDays;
    model.Cars = ...; // get the car records here

    return View(model);
}

and change your view as below

@model MyViewModel
foreach (Car car in Model.Cars)
{
    //other parameters
    <p>Full Price</p>:@(car.CostOneDay * Model.NumberOfDays)
}

EDIT

If you want to use the multiplied price in @Html.HiddenFor, then you need to add an additional property to your Car class. Assuming the type of CostOneDay is decimal, add FullPrice property with decimal type

public class Car
{
    // other properties

    public decimal FullPrice { get; set; }
}

then set the FullPrice property for each car record in the controller

public ActionResult DayView(int numberDays)
{
    MyViewModel model = new MyViewModel();
    model.NumberOfDays = numberDays;
    model.Cars = ...; // get the car records here

    foreach (var car in model.Cars)
    {
        car.FullPrice = car.CostOneDay * model.NumberOfDays;
    }

    return View(model);
}

and use @Html.HiddenFor like below

@model MyViewModel
foreach (Car car in Model.Cars)
{
    //other parameters
    <p>Full Price</p>:@car.FullPrice
    @Html.HiddenFor(m => car.FullPrice)
}
ekad
  • 14,436
  • 26
  • 44
  • 46
  • Great!!Thanks! and how now add @(car.CostOneDay * Model.NumberOfDays) in hiddenFor? I need to pass this form from controler – andrey1567 May 14 '15 at 14:21