如何在 MVC 中按 ID 搜索

How to search by ID in MVC

我想知道在 MVC 中是否有通过 ID 搜索记录的方法 我看过一些例子,但大多数使用 String,而我的字段是 Decimal,这就是我要找的。 基本上我需要使用 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> 

Class

    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; }
    }
}

尝试

localhost/Search?reqid=1 

而不是

localhost/Search?req_no=1