使用id在mvc4中搜索 [英] Search in mvc4 by using id

查看:56
本文介绍了使用id在mvc4中搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的MVC应用程序中,

i想要显示结果(当我在Id文本框中输入应用程序时,它将在各自的文本框中显示客户FirstName,LastName,Address

而不是在webgrid中显示,我想使用EF在同一页面中显示记录。

i我正在使用mvc4,visual studio 2012和EF.Thanks提前。任何人都可以帮助我。 />


我尝试过:



从数据库表tblCustomer创建实体框架。治疗了一个ActionResult。但没有按照我的期望工作。

i搜索谷歌,我发现搜索结果显示在网格中。



但我想,当我在Id文本框中输入应用程序时,它将在相应的文本框中显示客户FirstName,LastName,Address

而不是在webgrid中显示它,在同一页面中使用EF。

Sq;表:

CREATE TABLE [dbo]。[tblCustomer](

[Id] [int] IDENTITY(1 ,1)NOT NULL,

[FirstName] [varchar](50)NULL,

[LastName] [varchar](50)NULL,

[地址] [varchar](50)空,

CONSTRAINT [PK_tblCustomer] PRIMARY KEY CLUSTERED



[Id] ASC

)WITH(PAD_INDEX = OFF,STATISTICS_NORECOMPUTE = OFF,IGNORE_DUP_KEY = OFF,ALLOW_ROW_LOCKS = ON,ALLOW_PAGE_LOCKS = ON)ON [PRIMARY]

)ON [PRIMARY]



指数查看:

指数



@using(@ Html.BeginForm(索引,主页,FormMethod.Post))

{

输入Id:@ Html.TextBox(Id)

< ;输入类型=提交名称=搜索值=搜索/>

}



家庭控制器



[HttpGet]

公共ActionResult索引()

{

返回查看() ;

}



[HttpPost]

公共ActionResult索引(int Id)

{

var qry = se.tblCustomers.Single(m => m.Id == Id);

返回查看(_详情,qry);

}



详细信息查看:





tblCustomer







@ Html.DisplayNameFor(model => model.FirstName)



@ Html.DisplayFor(model => model .FirstName)





@ Html.DisplayNameFor(model => model.LastName)



@ Html.DisplayFor(model => model.LastName)





@ Html.DisplayNameFor(model = > model.Address)



@ Html.DisplayFor(model => model.Address)

解决方案

< blockquote>为了在各个文本框中显示结果,请使用TextBoxFor而不是DisplayFor。



详细信息视图中需要更改PFB。



详情查看:





@model CodePrjctQues.Models.tblCustomer



@ {

ViewBag.Title = 详情;

}



详细信息







tblCustomer





@ Html.DisplayNameFor (model => model.FirstName)





@ Html.TextBoxFor(model => model.FirstName)







@ Html.DisplayNameFor(model => model.LastName)





@ Html.TextBoxFor(model => model.LastName )







@Html。 DisplayNameFor(model => model.Address)





@ Html.TextBoxFor(model => model.Address)









我希望这将解决您的问题。


In my MVC application,
i want display result (when i enter the application no in Id textbox,it will display the customer FirstName,LastName,Address in respective textboxes
instead of displaying in webgrid,i want to display record in the same page by using EF.
i am using mvc4,visual studio 2012 and EF.Thanks in advance.Can anyone please help me.

What I have tried:

created Entity framework from database table "tblCustomer".Created one ActionResult.but not working as per my expectation.
i searched in google,i found after search result displayed in the webgrid.

but i want,when i enter the application no in Id textbox,it will display the customer FirstName,LastName,Address in respective textboxes
instead of displaying it in webgrid,in the same page by using EF.
Sq; Table:
CREATE TABLE [dbo].[tblCustomer](
[Id] [int] IDENTITY(1,1) NOT NULL,
[FirstName] [varchar](50) NULL,
[LastName] [varchar](50) NULL,
[Address] [varchar](50) NULL,
CONSTRAINT [PK_tblCustomer] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

Index View:
Index

@using (@Html.BeginForm("Index","Home",FormMethod.Post))
{
Enter Id :@Html.TextBox("Id")
<input type="submit" name="Search" value="Search" />
}

Home controller

[HttpGet]
public ActionResult Index()
{
return View();
}

[HttpPost]
public ActionResult Index(int Id)
{
var qry = se.tblCustomers.Single(m => m.Id == Id);
return View("_Details", qry);
}

Details View:


tblCustomer



@Html.DisplayNameFor(model => model.FirstName)

@Html.DisplayFor(model => model.FirstName)


@Html.DisplayNameFor(model => model.LastName)

@Html.DisplayFor(model => model.LastName)


@Html.DisplayNameFor(model => model.Address)

@Html.DisplayFor(model => model.Address)

解决方案

In order to display the result in respective textboxes, kindly use TextBoxFor instead of DisplayFor.

PFB changes required in Details View.

Details View:


@model CodePrjctQues.Models.tblCustomer

@{
ViewBag.Title = "Details";
}

Details




tblCustomer


@Html.DisplayNameFor(model => model.FirstName)



@Html.TextBoxFor(model => model.FirstName)




@Html.DisplayNameFor(model => model.LastName)



@Html.TextBoxFor(model => model.LastName)




@Html.DisplayNameFor(model => model.Address)



@Html.TextBoxFor(model => model.Address)





I hope this will solve your problem.


这篇关于使用id在mvc4中搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆