namespace STD.Controllers
{
public class StudentController: Controller
{
StudentEntities stud;
public StudentController()
{
stud= new StudentEntities ();
}
public ActionResult Index()
{
ViewData.Model = stud.StudentTable.ToList();
return View();
}
public ActionResult Student(int id)
{
Student s = stud.StudentTable.Find(id);
return View(s);
}
}
Then right-click anywhere in the Student action and select "Add View". In the Add View dialog create a strongly-typed detail view on the Student class.
Isnt "Student" a method? I get an error "STD.Controllers.StudentController.Student(int) is a method but used like a type,
when I use it like this:
chohmann
public ActionResult Student(int id) { Student s = stud.StudentTable.Find(id); return View(s); }
Also, I get the following error when I use the "Find" method:
Error205'System.Data.Objects.ObjectQuery<STD.Models.StudentTable>' does not contain a definition for 'Find' and no extension method 'Find' accepting a first argument of type 'System.Data.Objects.ObjectQuery<STD.Models.StudentTable>'
could be found (are you missing a using directive or an assembly reference?)
Am I missing some steps before using the Student method you gave?
Got it. The id field for student table is "StudentId"
I get the following error at "var s" :
Error205A local variable named 's' cannot be declared in this scope because it would give a different meaning to 's', which is already used in a 'child' scope to denote something else
Now, I did what you asked earlier: I am adding the strongly typed view, selecting the View data class (Database view: Entity Framework) and selecting "Details" from View content dropdown. This creates a Student.aspx view under Student folder in Views.
When I start debugging, I see the entire Students Table at URL: http://localhost:12121/Students
But, when I use URL: http://localhost:12121/Students/Student?StudentId=Jd112 I get the following error:
Sequence contains no elements
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: Sequence contains no elements
Source Error:
Line 31: public ActionResult Student(string id)
Line 32: {
Line 33: var s = stud.StudentTable.First(x => x.StudentId == id);
Line 34:
Line 35: return View(s);
Do I need to make any changes to the Student.aspx page to display the data I want for a particular student?
sam.pat
Member
195 Points
145 Posts
HELP: Creating an ActionResult method to display a particular database record using Entity Framew...
May 17, 2011 11:01 PM|LINK
Hi All,
I have an ActionResult method that displays records from a database table (below):
namespace STD.Controllers { public class StudentController: Controller { StudentEntities stud; public StudentController() { stud= new StudentEntities (); } public ActionResult Index() { ViewData.Model = stud.StudentTable.ToList(); return View(); } }I want to design a method "ActionResult Student()" that accepts "Student ID" as a parameter and displays record for that particular student only.
What do I need to do to make this happen? Could anyone please help me with this?
Thanks in advance
chohmann
Star
9385 Points
1644 Posts
Re: HELP: Creating an ActionResult method to display a particular database record using Entity Fr...
May 18, 2011 05:19 AM|LINK
namespace STD.Controllers { public class StudentController: Controller { StudentEntities stud; public StudentController() { stud= new StudentEntities (); } public ActionResult Index() { ViewData.Model = stud.StudentTable.ToList(); return View(); } public ActionResult Student(int id) { Student s = stud.StudentTable.Find(id); return View(s); } }Then right-click anywhere in the Student action and select "Add View". In the Add View dialog create a strongly-typed detail view on the Student class.
tdykstra
Contributor
4460 Points
624 Posts
Microsoft
Moderator
Re: HELP: Creating an ActionResult method to display a particular database record using Entity Fr...
May 18, 2011 11:15 AM|LINK
See the EF/MVC tutorial series at this location:
http://asp.net/entity-framework/tutorials
sam.pat
Member
195 Points
145 Posts
Re: HELP: Creating an ActionResult method to display a particular database record using Entity Fr...
May 18, 2011 04:10 PM|LINK
Thanks for helping chohmann,
Isnt "Student" a method? I get an error "STD.Controllers.StudentController.Student(int) is a method but used like a type, when I use it like this:
Also, I get the following error when I use the "Find" method:
Error 205 'System.Data.Objects.ObjectQuery<STD.Models.StudentTable>' does not contain a definition for 'Find' and no extension method 'Find' accepting a first argument of type 'System.Data.Objects.ObjectQuery<STD.Models.StudentTable>' could be found (are you missing a using directive or an assembly reference?)
Am I missing some steps before using the Student method you gave?
<div></div>
chohmann
Star
9385 Points
1644 Posts
Re: HELP: Creating an ActionResult method to display a particular database record using Entity Fr...
May 18, 2011 05:22 PM|LINK
Sorry, I assumed you were using a DbContext. It sounds like your using an ObjectContext. See if this works:
public ActionResult Student(int id) { var s = stud.StudentTable.First(s => s.Id == id); return View(s); }I'm assuming the id field for the student table is "Id".sam.pat
Member
195 Points
145 Posts
Re: HELP: Creating an ActionResult method to display a particular database record using Entity Fr...
May 18, 2011 05:55 PM|LINK
Got it. The id field for student table is "StudentId"
I get the following error at "var s" :
Error 205 A local variable named 's' cannot be declared in this scope because it would give a different meaning to 's', which is already used in a 'child' scope to denote something else
<div></div>chohmann
Star
9385 Points
1644 Posts
Re: HELP: Creating an ActionResult method to display a particular database record using Entity Fr...
May 18, 2011 05:59 PM|LINK
Lol. Sorry, try this:
public ActionResult Student(int id) { var s = stud.StudentTable.First(x => x.StudentId == id); return View(s); }I make this mistake at least once a day. You would think it would eventually sink in.
sam.pat
Member
195 Points
145 Posts
Re: HELP: Creating an ActionResult method to display a particular database record using Entity Fr...
May 18, 2011 06:23 PM|LINK
Thanks!
But, I still get an error "Error 204 Operator '==' cannot be applied to operands of type 'string' and 'int' " <div></div>
When I get it to just "=" I get the error "Error 204 Cannot implicitly convert type 'int' to 'string' "
Do I declare it a different way?
chohmann
Star
9385 Points
1644 Posts
Re: HELP: Creating an ActionResult method to display a particular database record using Entity Fr...
May 18, 2011 06:26 PM|LINK
Is StudentId a string? If so, try this:
public ActionResult Student(string id) { var s = stud.StudentTable.First(x => x.StudentId == id); return View(s); }sam.pat
Member
195 Points
145 Posts
Re: HELP: Creating an ActionResult method to display a particular database record using Entity Fr...
May 18, 2011 06:53 PM|LINK
Got that to work.
Now, I did what you asked earlier: I am adding the strongly typed view, selecting the View data class (Database view: Entity Framework) and selecting "Details" from View content dropdown. This creates a Student.aspx view under Student folder in Views.
When I start debugging, I see the entire Students Table at URL: http://localhost:12121/Students
But, when I use URL: http://localhost:12121/Students/Student?StudentId=Jd112 I get the following error:
Sequence contains no elements
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: Sequence contains no elements
Source Error:
Line 31: public ActionResult Student(string id)
Line 32: {
Line 33: var s = stud.StudentTable.First(x => x.StudentId == id);
Line 34:
Line 35: return View(s);
Do I need to make any changes to the Student.aspx page to display the data I want for a particular student?