C# 如何在MVC中按ID进行搜索
我想知道是否有办法在MVC中按ID搜索记录 我看过一些例子,但大多数使用字符串,我的字段是十进制,这就是我要找的。 基本上,我需要使用文本框中的ID过滤我的记录C# 如何在MVC中按ID进行搜索,c#,asp.net-mvc,asp.net-mvc-4,visual-studio-2012,C#,Asp.net Mvc,Asp.net Mvc 4,Visual Studio 2012,我想知道是否有办法在MVC中按ID搜索记录 我看过一些例子,但大多数使用字符串,我的字段是十进制,这就是我要找的。 基本上,我需要使用文本框中的ID过滤我的记录 localhost/Search?req_no=1 我有以下代码,但我不知道它是否正确,谢谢 控制器 namespace MvcApplication31.Controllers { public class SearchController : Controller { //
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
@{
ViewBag.Title=“搜索”;
}
搜寻
@ActionLink(“新建”、“创建”)
@使用(Html.BeginForm(“Search”,“Search”,FormMethod.Post)){
请求编号:@Html.TextBox(“请求ID”)
}
阶级
namespace mvcapapplication31
{
使用制度;
使用System.Collections.Generic;
公共部分类TB\U CS\U测试
{
公共十进制请求号{get;set;}
公共可空序列号{get;set;}
公共可空项_ID{get;set;}
公共可空数量{get;set;}
公共字符串UOM{get;set;}
公共可空单位价格{get;set;}
公共可空扩展_AMT{get;set;}
公共可为空的CRT_日期{get;set;}
公共十进制请求ID{get;set;}
公共虚拟TB_CS_TEST2 TB_CS_TEST2{get;set;}
}
}
试试看
而不是
localhost/Search?req_no=1
您遇到了什么问题?当我输入ID时,它不会抛出任何行
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