首页 > 解决方案 > 如何在 MVC 中按 ID 搜索

问题描述

我想知道是否有办法在 MVC 中按 ID 搜索记录我看过一些例子,但大多数使用字符串,我的字段是十进制,这就是我要找的。基本上我需要使用 TextBox 中的 ID 过滤我的记录

localhost/Search?req_no=1

我有以下代码,但我不知道它是否正确,谢谢。

控制器

    namespace MvcApplication31.Controllers
{
    public class SearchController : Controller
    {
        //
        // GET: /Search/

        public ActionResult Search()
        {
            Entities db = new Entities();
            return View(db.TB_CS_TEST.ToList());
        }
        [HttpPost]
        public ActionResult Search(decimal? reqid)
        {
            Entities db = new Entities();
            var req = from r in db.TB_CS_TEST
                      select r;
            if (reqid.HasValue)
            {
                req = req.Where(s => s.REQ_NO.Equals(reqid));
            }
            return View(req);
        }

    }
}

看法

@model IEnumerable<MvcApplication31.TB_CS_TEST>

@{
    ViewBag.Title = "Search";
}

<h2>Search</h2>

<p> 
    @Html.ActionLink("Create New", "Create") 

     @using (Html.BeginForm("Search","Search", FormMethod.Post)){    
         <p> Request Number: @Html.TextBox("reqid") <br />   
         <input type="submit" value="Search" /></p> 
        } 
</p> 

班级

    namespace MvcApplication31
{
    using System;
    using System.Collections.Generic;

    public partial class TB_CS_TEST
    {
        public decimal REQ_NO { get; set; }
        public Nullable<decimal> SEQ_NO { get; set; }
        public Nullable<decimal> ITEM_ID { get; set; }
        public Nullable<decimal> QUANTITY { get; set; }
        public string UOM { get; set; }
        public Nullable<decimal> UNIT_PRICE { get; set; }
        public Nullable<decimal> EXTENDED_AMT { get; set; }
        public Nullable<System.DateTime> CRT_DATE { get; set; }
        public decimal REQDTL_ID { get; set; }

        public virtual TB_CS_TEST2 TB_CS_TEST2 { get; set; }
    }
}

标签: c#asp.net-mvcasp.net-mvc-4visual-studio-2012

解决方案


尝试

localhost/Search?reqid=1 

代替

localhost/Search?req_no=1

推荐阅读