Click here to Skip to main content
15,884,900 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 it in webgrid,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))
{
<text>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)

Posted
Updated 10-Aug-16 4:28am
v5

1 solution

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.
 
Share this answer
 
Comments
bubai banerjee 10-Aug-16 11:53am    
Thanks Tanvi.

as i am using "_Details" partial view,it is re-direct me into next page,
but, i want, when i enter id,it will display result in the same page,not in 2nd page.
please suggest how to proceed.
Member 11114915 - Tanvi 10-Aug-16 12:54pm    
Instead of using return View("_Details", qry) , kindly use return View(qry); and also add below on Index view.

<fieldset>
tblCustomer

<div class="display-label">
@Html.DisplayNameFor(model => model.FirstName)
</div>
<div class="display-field">
@Html.TextBoxFor(model => model.FirstName)
</div>

<div class="display-label">
@Html.DisplayNameFor(model => model.LastName)
</div>
<div class="display-field">
@Html.TextBoxFor(model => model.LastName)
</div>

<div class="display-label">
@Html.DisplayNameFor(model => model.Address)
</div>
<div class="display-field">
@Html.TextBoxFor(model => model.Address)
</div>
</fieldset>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900