Previous and Next result display in textboxes using Asp.net MVC

Posted by Bubaibanerjee under ASP.NET MVC on 1/6/2014 | Points: 10 | Views : 5900 | Status : [Member] | Replies : 8
Dear Sir/Madam,
I am very new in the MVC coding. i have a below problem.please help me.Thanks in advance.
 
I have Three text box in asp.net MVC form. Author Id,Author Name and Author address.
I have two button(Previous and Next).when I click on the next button it will show one by one Author records from Sql server database.i used entity framework in MVC 4.
 
Design view in mvc
 

Author
 

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

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

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

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

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

@Html.DisplayFor(model => model.Location)
 
<input type="submit" value="Previous" />
<input type="submit" value="Next" />

 
Database Details:
 
CREATE TABLE Author1
(
AuthorId INT PRIMARY KEY IDENTITY(1,1) NOT NULL,
Name VARCHAR(100) NULL,
Location VARCHAR(50) NULL,
)
 
INSERT INTO Author1 VALUES('bubai','Kulti')
INSERT INTO Author1 VALUES('Ajay','Malad')
INSERT INTO Author1 VALUES('Rajesh','Kharghar')
INSERT INTO Author1 VALUES('Smita','Thane')
INSERT INTO Author1 VALUES('Amit','Burdwan')
INSERT INTO Author1 VALUES('Sumit','Golf Green')
 

select * from Author1
 
Please send me complete and simple code as i am very new in mvc coding.
 
Thanks for kind help.
Bubai Banerjee




Responses

Posted by: kgovindarao523-21772 on: 1/6/2014 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi,

Here, I provide a solution for my table Employee. u can change it as you like

In Controller(Home)

[HttpGet]
public ActionResult Nextchange()
{
Repositary res = new Repositary();
IEnumerable<Employee> lst = res.GetAllEmployees();
Employee lst1 = lst.FirstOrDefault();
return View(lst1);
}
[HttpPost]
public ActionResult Nextchange(Employee emp,string Nxt)
{
Repositary res = new Repositary();
IEnumerable<Employee> lst = res.GetAllEmployees();
Employee lst1;
if (Nxt == "Nxt" && lst.Where(x => x.EmpId == (emp.EmpId + 1)).Count() > 0)
lst1 = lst.Where(x => x.EmpId == (emp.EmpId + 1)).FirstOrDefault();
else if (Nxt == "Pre" && lst.Where(x => x.EmpId == (emp.EmpId - 1)).Count() > 0)
lst1 = lst.Where(x => x.EmpId == (emp.EmpId - 1)).FirstOrDefault();
else
return View(emp);
return View(lst1);
}


In View
@model EFDemo.Models.Employee
<form action="@Url.Action("Nextchange","Home")" method="post">

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

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


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

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


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

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

<input type="submit" value="Previous" />
<input type="submit" value="Next" />
</form>


Thank you,
Govind

Bubaibanerjee, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bubaibanerjee on: 1/6/2014 [Member] Starter | Points: 25

Up
0
Down
Hi Govind Sir,
if you don't mind can you please send me the full code(including model and Database) as i am completely fresher in this field.
Aditionally, please send me GetAllEmployees() method also.

Really thanks for helping me.

Thanks
Bubai Banerjee

Bubaibanerjee, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: kgovindarao523-21772 on: 1/6/2014 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi,

Please find the attachment.
if this helps you, Please mark as answer

 Download source file

Thank you,
Govind

Bubaibanerjee, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bubaibanerjee on: 1/7/2014 [Member] Starter | Points: 25

Up
0
Down
Hi Govind Sir,

Thank you so much for sending the code.If you not mind can you please send me the Repositary class details, as i am not getting anything for this repositary class.

Please sir, send me the below details please
1) Repositary class details
2) GetAllEmployees() method

Thanks
Bubai

Bubaibanerjee, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: kgovindarao523-21772 on: 1/7/2014 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Sure,

Sorry i Forgot to attach the class
Here i am attached Repository class
 Download source file

Thank you,
Govind

Bubaibanerjee, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bubaibanerjee on: 1/8/2014 [Member] Starter | Points: 25

Up
0
Down
Hi Govind Sir,
Thank you so much for your kind support and help. It is really working fine.
But when i want to use Store procedure it is not working properly.

I may be doing some mistake.
The below steps i am following:

1) Creating sp and merge it with entity framework.
2) Creating one Complex type function(Including sp with this function).because i have 5 tables.
3) Complex type function name is "HapaFunction()"

your code:
[HttpGet]
public ActionResult Nextchange()
{
var qresult = te.Author1.ToList();
IEnumerable<Author1> lst = qresult;
Author1 lst1 = lst.FirstOrDefault();
return View(lst1);
}

My Code:

[HttpGet]
public ActionResult Nextchange()
{
var qresult = te.HapaFunction();
IEnumerable<Author1> lst = qresult.ToList();
Author1 lst1 = lst.FirstOrDefault();
return View(lst1);
}
Sir your code working properly, but when i change it(means My code with function) showing err.
I am not able to find my mistake.

Please help Sir.

Thanks
Bubai

Bubaibanerjee, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: kgovindarao523-21772 on: 1/8/2014 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi,

May I know what error you are getting?
I think, you have to recheck the process of adding your Stored procedure(and passing parameters too)

Thank you,
Govind

Bubaibanerjee, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: kgovindarao523-21772 on: 1/9/2014 [Member] [MVP] Bronze | Points: 25

Login to post response