How to pass entire rows data of a table from one view to another different view MVC3 [Resolved]

Posted by ramuvalmiki07-22001 under ASP.NET on 5/1/2013 | Points: 10 | Views : 3824 | Status : [Member] | Replies : 8
Hii to all,
I have a requirement that i need to get complete rows data of a table from view to controller and then display in another different view, The actual scenario is i have a table where i need to display the employees data like Empid,Ename,Task,in addition to this columns i have placed one more field called Edit(Link Field),so that with the help of Edit link field we can update employee details, here what i need is whenever i clicked on EditLink i need to redirect to another page say editable page,in this editable page i need to get the respective rows(the row editlink is fired)data and dispaly in the textbox's in an editable mode, so that he can modify the employee details and click on submit buttton then changes should reflect to the table. Here i iam not using any database for displaying table data, in the view itself i designed the table & populated the data to the table fields,please guide me how can i pass complete rows data from one View to another different view..thanks in advance

Thanks
Ramu




Responses

Posted by: Kmandapalli on: 5/2/2013 [Member] Silver | Points: 50

Up
0
Down

Resolved
Hi,

whenever you click on edit link, redirect it to [HttpPost] ActionMethod of that View.
ie., for the link button, write onclick="edit();"
Write javascript so that when you click on that link it goes to the post ActionMethod.
Javasctript :
function edit() {
document.addedit.submit()
}
@using (Html.BeginForm("PresentViewName", "ControllerName", FormMethod.Post, new { id = "addedit", name = "addedit" }))
{
// write the code
}

Write a stored procedure to insert the columns that you want.
//First create a table
create table editable
(
EmpID int primary key,
EmpName nvarchar(100),
Task nvarchar(500)
)

//Next create a stored procedure
create proc usp_editable
(
@EmpID int,
@EmpName nvarchar(100),
@Task nvarchar(500)
)
as
begin
insert into editable(EmpID, EmpName, Task) values(@EmpID, @EmpName, @Task)
end

Now, goto models folder, right click on it and select data tab and add entity data model.
select table editable from tables and usp_editable from storedProcedures and click on next.
After that goto to function Imports and select your stored proc.

Next, open your controller and write the Post ActionMethod.
[HttpPost]
public ActionResult PresentViewName(string EmpID, string EmpName, string Task etc....(include the parameters that you want from your view table))
{
// Put EmpID in TempData
TempData["EmpID"]=EmpID;
TempData.Keep();
db.usp_editable(EmpID, EmpName, Task);
db.SaveChanges();
return RedirectToAction("EditableView");
}

Now, retreive the column values from the table in this actionMethod

public ActionMethod Editable()
{
int EmpId = (string)TempData["EmpID"];
var query = from d in db.editable
where d.EmpID==EmpId
select new {
EmpID=d.EmpID,
EmpName=d.EmpName,
Task=d.Task
};
return View(query.ToList());
}
Declare the properties in the Modelclass Employee.
In this editable view, retreive the values.
<table>
<tr>
@foreach (ApplicationName.Models.Employee item in Model)
{
<td align="right" width="20%">
@Html.Label("Company Name :")
</td>
<td>
@Html.TextBox("EmpID", @item.EmpID)
</td>
</tr>
//repeat the code for EmpName and Task
</table>

Run the appliaction and you can see the values displayed in the text boxes.

You can message me for any quiries....

Regards,
Shree M.

Kavya Shree Mandapalli

ramuvalmiki07-22001, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kmandapalli on: 5/2/2013 [Member] Silver | Points: 50

Up
0
Down

Resolved
Hi,

var itemFromLINQ2 = (from s in objemp where s.Empid == 101 select s.Ename).FirstOrDefault();
In this query you wrote select s.Ename and so your getting only the Employee Name.
If you want all the columns to be displayed, then write as,

var itemFromLINQ2 = from s in objemp
where s.Empid == 101
select new {
EmpID = s.EmpID,
EmpName=s.EmpName,
Task=s.Task
}.FirstOrDefault();

Please mark as answer...

Regards,
Shree M.

Kavya Shree Mandapalli

ramuvalmiki07-22001, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kmandapalli on: 5/2/2013 [Member] Silver | Points: 50

Up
0
Down

Resolved
Hi,

<table>
<tr>
@foreach (ApplicationName.Models.Employee item in Model)
{
<td align="right" width="20%">
@Html.Label("Employee ID :")
</td>
<td>
@Html.TextBox("EmpID", @item.EmpID)
</td>
}
</tr>
<tr>
@foreach (ApplicationName.Models.Employee item in Model)
{
<td align="right" width="20%">
@Html.Label("Employee Name :")
</td>
<td>
@Html.TextBox("EmpName", @item.EmpName)
</td>
}
</tr>
<tr>
@foreach (ApplicationName.Models.Employee item in Model)
{
<td align="right" width="20%">
@Html.Label("Task :")
</td>
<td>
@Html.TextBox("Task", @item.Task)
</td>
}
</tr>
</table>

Mark as Answer if satisfied...

Regards,
Shree M.

Kavya Shree Mandapalli

ramuvalmiki07-22001, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kmandapalli on: 5/6/2013 [Member] Silver | Points: 50

Up
0
Down

Resolved
Hi,

I have attached the source code in .zip format.
You can download it.

Mark as Answer if satisfied....

Regards,
Shree M.
 Download source file

Kavya Shree Mandapalli

ramuvalmiki07-22001, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: ramuvalmiki07-22001 on: 5/2/2013 [Member] Starter | Points: 25

Up
0
Down
Hii Shree,
Thanks for ur responce, the code u provided is working fine,and i have got a very good idea on writing the code in mvc,once again thanks for ur detailed explanation.Here i have one more requirement please try to help me, i added my table data to List,by writing LINQ how can i get all the tables data & display in textboxes.
Here i wrote the LINQ like following:-
var itemFromLINQ2 = (from s in objemp where s.Empid == 101 select s.Ename).FirstOrDefault();
ViewData["Ename"] = itemFromLINQ2;
but here iam getting only one record i.e Ename,i want to display total data related to 101 id in textboxes

Thanks
Ramu

ramuvalmiki07-22001, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kmandapalli on: 5/2/2013 [Member] Silver | Points: 25

Up
0
Down
Hi,
So can you please Mark as Answer.

Regards,
Shree M.

Kavya Shree Mandapalli

ramuvalmiki07-22001, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: ramuvalmiki07-22001 on: 5/2/2013 [Member] Starter | Points: 25

Up
0
Down
Hii shree,
Now i got to know my mistake,its working fine,sorry for disturbing u again though iam not new to .Net but new to this MVC,EF& LINQ, hear i have one more problem, how can i bind this to textboxes,i.e now all the three values are displaying in one textbox only,i want Empid in one textbox,Ename in another and similarly task in another textbox,

Thanks
Ramu

ramuvalmiki07-22001, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 5/4/2013 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi

Kmandapalli

Can You post this Source Code Zip Format.

Mark as Answer if its helpful to you

ramuvalmiki07-22001, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response