You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过浮点数实现数据库价格检索功能

解决Repairs数据表价格检索失效的问题

我帮你梳理下代码里的问题,这几个点是导致检索不起作用的核心原因:

核心问题分析

  • 转换后的数值未被使用:你调用了float.Parse但没把结果存进变量,等于白做了转换,后面还是用原始字符串和数据库的数值字段对比,类型不匹配肯定搜不到结果
  • 类型匹配逻辑错误:数据库里的价格字段应该是数值类型(比如float/decimal),你用字符串的Equals方法去匹配数值,逻辑上完全不对
  • 缺少异常处理:如果用户输入了非数字字符,float.Parse会直接抛异常,导致页面崩溃

修复后的代码示例

AccountantController 修改

using System.Globalization;
using Microsoft.EntityFrameworkCore;

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 repairsQuery = _context.Repairs.AsQueryable();

            // 处理服务价格检索
            if (!string.IsNullOrEmpty(searchServicePrice) 
                && float.TryParse(searchServicePrice, CultureInfo.InvariantCulture, out float servicePrice))
            {
                // 请根据你的Repair实体实际字段名调整ServicePrice
                repairsQuery = repairsQuery.Where(s => s.ServicePrice == servicePrice);
            }

            // 处理配件价格检索
            if (!string.IsNullOrEmpty(searchPartsPrice) 
                && float.TryParse(searchPartsPrice, CultureInfo.InvariantCulture, out float partsPrice))
            {
                // 请根据你的Repair实体实际字段名调整PartsPrice
                repairsQuery = repairsQuery.Where(s => s.PartsPrice == partsPrice);
            }

            // 处理总价格检索
            if (!string.IsNullOrEmpty(searchSummaryPrice) 
                && float.TryParse(searchSummaryPrice, CultureInfo.InvariantCulture, out float summaryPrice))
            {
                // 请根据你的Repair实体实际字段名调整SummaryPrice
                repairsQuery = repairsQuery.Where(s => s.SummaryPrice == summaryPrice);
            }

            var repairs = await repairsQuery.ToListAsync();
            return View(repairs);
        }
    }
}

View 优化(可选,提升用户体验)

把输入框改成数字类型,浏览器会自动限制输入内容,还支持小数输入:

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

<h2>Repairs Search</h2>
<form asp-controller="Accountant" asp-action="Index" method="get">
    <p>
        Service Price : <input type="number" step="0.01" name="searchServicePrice" placeholder="e.g. 125.50">
        Parts Price : <input type="number" step="0.01" name="searchPartsPrice" placeholder="e.g. 80.00">
        Summary Price: <input type="number" step="0.01" name="searchSummaryPrice" placeholder="e.g. 205.50">
        <input type="submit" value="Filter" />
    </p>
</form>

@if (Model.Any())
{
    <table class="table">
        <!-- 这里添加你的表格内容,展示检索结果 -->
    </table>
}
else
{
    <p>No repairs matched your search criteria.</p>
}

关键修改说明

  1. 用float.TryParse替代float.Parse:避免输入非法字符时抛出异常,同时返回转换是否成功的结果
  2. 使用转换后的数值进行查询:把转换后的float变量和数据库的数值字段直接对比,确保类型匹配
  3. 区分三个价格字段:根据你的三个搜索框,对应到Repair实体的三个不同价格字段(请根据实际数据库字段名调整代码里的字段名称)
  4. 异步查询优化:用ToListAsync()执行异步查询,符合ASP.NET MVC的最佳实践

内容的提问来源于stack exchange,提问作者Jacek Duraj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 03:27:53