0

I am trying to make searcher. It should search through prices in table Repairs. I don't know how to resolve this problem. My only idea was to parse string to float using float.Parse method but it doesn't work.

Model

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace CallCenterService.Controllers
{
    public class AccountantController : Controller
    {
        private readonly DatabaseContext _context;

        public AccountantController(DatabaseContext context)
        {
            _context = context;
        }
        // GET: /<controller>/


        public async Task<IActionResult> Index(string searchServicePrice, string searchPartsPrice, string searchSummaryPrice)
        {
            var name = from m in _context.Repairs
                       select m;

            if (!String.IsNullOrEmpty(searchServicePrice))
            {
                float.Parse(searchServicePrice, CultureInfo.InvariantCulture.NumberFormat);
                name = name.Where(s => s.Price.Equals(searchServicePrice));
            }

View

@model IEnumerable<CallCenterService.Models.Repair>
@{
    ViewData["Title"] = "Index";
}

<h2>Index</h2>
<form asp-controller="Accountant" asp-action="Index" method="get">
    <p>
        Service Price : <input type="text" name="searchServicePrice">
        Parts Price : <input type="text"  name="searchPartsPrice">
        Summary Price: <input type="text"  name="searchSummaryPrice">
        <input type="submit" value="Filter" />
    </p>
</form>
  • never use floating point for money. neither float or double. Use decimal. – Keith Nicholas May 13 '18 at 23:15
  • 1
    Hello Jacek, your post doesn't really explain what problem you are having, what you expect your code to do or what result you are actually getting. Asking effective questions can be quite hard (at least I find it hard), The [ask] and [mcve] pages offer some helpful guidance on how to ask an effective question. – Frank Boyne May 14 '18 at 01:13
  • If you're wondering why @KeithNicholas recommends not using floating point for currency see this post: [Why not use Double or Float to represent currency?](https://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency) – Frank Boyne May 14 '18 at 01:20

1 Answers1

1

You need to pass the parsed item because the Parse method returns a float. Like this:

float parsedNum = float.Parse(searchServicePrice, CultureInfo.InvariantCulture.NumberFormat);
name = name.Where(s => s.Price.Equals(parsedNum));     

Also look into decimal.Parse, decimal.TryParse and float.TryParse because if Parse fails, it will throw an exception. TryParse will not throw an exception.

CodingYoshi
  • 25,467
  • 4
  • 62
  • 64