Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hello,

I am using ASPP MVC and Entity Framework. I want to include a Filter by Price in my Project. I get alwaya an Error of
Quote:
Nullable object must have a value.


What I have tried:

This is the code in my View :
@{
    var fromPrice = ViewBag.fromPrice as Decimal?;
    var toPrice = ViewBag.toPrice as Decimal?;
}

@using (Html.BeginForm("FiltredPrice", "Home", FormMethod.Get))
{
    <div>From Price: @Html.TextBox("fromPrice", fromPrice.Value))</div>
    <div>To Price: @Html.TextBox("toPrice", toPrice.Value))</div>
    <input type="submit" value="Search" />
}
HTML



and this is what I tried in my Controller :
public ActionResult FiltredPrice(Decimal? fromPrice, Decimal? toPrice)
        {

            if (!fromPrice.HasValue) fromPrice = Decimal.Zero;
            if (!toPrice.HasValue) toPrice = fromPrice.GetValueOrDefault(Decimal.Zero+1);
            if (toPrice < fromPrice) toPrice = fromPrice.GetValueOrDefault(Decimal.Zero + 1);
            ViewBag.fromDate = fromPrice;
            ViewBag.toDate = toPrice;

            var voitures = db.Voitures
              .Where(c => c.Price >= fromPrice && c.Price < toPrice)
              .ToList();
            return View(voitures);
        }
C#

Posted
Comments
F-ES Sitecore 14-Aug-17 6:16am    
What line gives the error?
Anouar2002 14-Aug-17 6:17am    
From Price: @Html.TextBox("fromPrice", fromPrice.Value))
F-ES Sitecore 14-Aug-17 6:28am    
Does fromPrice have a value? You might need to code for when it doesn't by giving it a default

@Html.TextBox("fromPrice", fromPrice.HasValue ? fromPrice.Value.ToString() : ""))
Anouar2002 14-Aug-17 6:37am    
Thanks alot the error is corrected !but I didnt success to filter between the Prices

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900