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

ASP.NET MVC新手求助:根据ITEMCODE获取并显示ITEMNAME

ASP.NET MVC 根据ITEMCODE获取ITEMNAME实现步骤

1. 配置实体类与DbContext

先确保项目中有对应tbl_inventorycontrol表的实体类,并在DbContext中注册数据集:

// tbl_inventorycontrol实体类
public class tbl_inventorycontrol
{
    public string ITEMCODE { get; set; }
    public string ITEMNAME { get; set; }
    // 其他表字段按需添加
}

// 你的DbContext类
public class YourDbContext : DbContext
{
    public DbSet<tbl_inventorycontrol> tbl_inventorycontrol { get; set; }
    
    // 构造函数配置连接字符串等
    public YourDbContext(DbContextOptions<YourDbContext> options) : base(options) { }
}

2. 实现Stockcard控制器的SearchItemCode方法

在StockcardController中添加查询方法,接收ITEMCODE参数并返回对应的ITEMNAME:

public class StockcardController : Controller
{
    private readonly YourDbContext _dbContext;

    // 依赖注入DbContext
    public StockcardController(YourDbContext dbContext)
    {
        _dbContext = dbContext;
    }

    [HttpPost]
    public JsonResult SearchItemCode(string itemCode)
    {
        if (string.IsNullOrWhiteSpace(itemCode))
        {
            return Json(new { success = false, msg = "请输入ITEMCODE" });
        }

        var itemName = _dbContext.tbl_inventorycontrol
            .Where(x => x.ITEMCODE == itemCode)
            .Select(x => x.ITEMNAME)
            .FirstOrDefault();

        if (itemName == null)
        {
            return Json(new { success = false, msg = "未找到匹配的ITEMNAME" });
        }

        return Json(new { success = true, itemName = itemName });
    }
}

注意替换YourDbContext为你实际的DbContext类名。

3. 调整视图实现按钮交互

假设视图已绑定tbl_STOCKCARD模型,添加AJAX逻辑实现点击按钮后异步查询并显示结果:

@model YourNamespace.tbl_STOCKCARD

<div>
    @Html.LabelFor(m => m.ITEMCODE)
    @Html.TextBoxFor(m => m.ITEMCODE, new { id = "itemCodeInput" })
    <button id="searchBtn">Search Item</button>
    <!-- 用于展示查询结果的容器 -->
    <div id="itemNameDisplay" style="margin-top:10px;"></div>
</div>

@section Scripts {
    <script>
        $(function() {
            $("#searchBtn").click(function() {
                var itemCode = $("#itemCodeInput").val().trim();
                if (!itemCode) {
                    $("#itemNameDisplay").text("请输入ITEMCODE");
                    return;
                }

                $.ajax({
                    url: '@Url.Action("SearchItemCode", "Stockcard")',
                    type: 'POST',
                    data: { itemCode: itemCode },
                    success: function(res) {
                        if (res.success) {
                            $("#itemNameDisplay").text("匹配的ITEMNAME:" + res.itemName);
                        } else {
                            $("#itemNameDisplay").text(res.msg);
                        }
                    },
                    error: function() {
                        $("#itemNameDisplay").text("查询失败,请重试");
                    }
                });
            });
        });
    </script>
}

替换YourNamespace为tbl_STOCKCARD模型所在的命名空间即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 15:27:14