Can i get help with connecting to database through MVC method/ All of my internal code seems fine, model class, Persons controller, view! But yet and still cant figure why i get an error. I am using EF ado model.
PersonsController
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AndrewProject.Models;
using System.Reflection;
namespace AndrewProject.Controllers
{
public class PersonController : Controller
{
// GET: Person
//List<Person> _db = new List<Person>();
private Person _db = new Person();
//mvcappdb _db = new mvcappdb();
public ActionResult Index()
{
// List<Person> lstper = new List<Person>()
//// var person = new List<Person>
//{
// new Person () { PersonId= 1001, Age = 28, FirstName = "Bob", LastName = "Chris" , City = "Houston"},
// new Person () { PersonId= 1002, Age = 35, FirstName = "Meech", LastName = "Gavins", City = "Boston"},
// new Person () { Age = 35, FirstName = "Shannon", LastName = "Moore", City = "Denver"}
// };
// //return View("Index", person);
// return View(lstper);
namespace AndrewProject.Models
{
using System;
using System.Collections.Generic;
public partial class Person
{
public int PersonId { get; set; }
public string Age { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string City { get; set; }
}
}
Okay i corrected the name of the connection string to Persons from Person!
privatePersons _db =newPersons();
The error is in the return view of the ActionResult Index
public ActionResult Index()
{
return View(_db.Person.ToList()); // cannot correct this piece of code.(Person)
}
I still receive following error!
Severity Code Description Project File Line Suppression State
Error CS1061 'Persons' does not contain a definition for 'Person' and no extension method 'Person' accepting a first argument of type 'Persons' could be found (are you missing a using directive or an assembly reference?) AndrewProject c:\users\andrew\documents\visual
studio 2015\Projects\AndrewProject\AndrewProject\Controllers\PersonController.cs 24 Active
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
251 Points
423 Posts
Action Method Error!
Oct 18, 2018 08:21 PM|Markus33|LINK
Can i get help with connecting to database through MVC method/ All of my internal code seems fine, model class, Persons controller, view! But yet and still cant figure why i get an error. I am using EF ado model.
PersonsController
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AndrewProject.Models;
using System.Reflection;
namespace AndrewProject.Controllers
{
public class PersonController : Controller
{
// GET: Person
//List<Person> _db = new List<Person>();
private Person _db = new Person();
//mvcappdb _db = new mvcappdb();
public ActionResult Index()
{
return View(_db.Person.ToList()); //error code..."
}
//Get:Student
// List<Person> lstper = new List<Person>()
//// var person = new List<Person>
//{
// new Person () { PersonId= 1001, Age = 28, FirstName = "Bob", LastName = "Chris" , City = "Houston"},
// new Person () { PersonId= 1002, Age = 35, FirstName = "Meech", LastName = "Gavins", City = "Boston"},
// new Person () { Age = 35, FirstName = "Shannon", LastName = "Moore", City = "Denver"}
// };
// //return View("Index", person);
// return View(lstper);
}
}
Index view
@model IEnumerable<AndrewProject.Models.Person>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Age)
</th>
<th>
@Html.DisplayNameFor(model => model.FirstName)
</th>
<th>
@Html.DisplayNameFor(model => model.LastName)
</th>
<th>
@Html.DisplayNameFor(model => model.City)
</th>
<th></th>
</tr>
@foreach (AndrewProject.Models.Person personid in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => personid.Age)
</td>
<td>
@Html.DisplayFor(modelItem => personid.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => personid.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => personid.City)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=personid.PersonId }) |
@Html.ActionLink("Details", "Details", new { id=personid.PersonId }) |
@Html.ActionLink("Delete", "Delete", new { id=personid.PersonId })
</td>
</tr>
}
</table>
Person Model
//------------------------------------------------------------------------------
namespace AndrewProject.Models
{
using System;
using System.Collections.Generic;
public partial class Person
{
public int PersonId { get; set; }
public string Age { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string City { get; set; }
}
}
All-Star
52201 Points
23274 Posts
Re: Action Method Error!
Oct 18, 2018 08:42 PM|mgebhard|LINK
What is the error?
Is Person really the name of your DbContext?
I recommend going through this Getting Started Tutorial to make sure you have EF setup correctly.
https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application
Member
251 Points
423 Posts
Re: Action Method Error!
Oct 18, 2018 08:52 PM|Markus33|LINK
Okay i corrected the name of the connection string to Persons from Person!
The error is in the return view of the ActionResult Index
public ActionResult Index()
{
return View(_db.Person.ToList()); // cannot correct this piece of code.(Person)
}
I still receive following error!
Severity Code Description Project File Line Suppression State
Error CS1061 'Persons' does not contain a definition for 'Person' and no extension method 'Person' accepting a first argument of type 'Persons' could be found (are you missing a using directive or an assembly reference?) AndrewProject c:\users\andrew\documents\visual studio 2015\Projects\AndrewProject\AndrewProject\Controllers\PersonController.cs 24 Active
All-Star
52201 Points
23274 Posts
Re: Action Method Error!
Oct 18, 2018 10:28 PM|mgebhard|LINK
Contributor
3710 Points
1431 Posts
Re: Action Method Error!
Oct 19, 2018 08:19 AM|Yuki Tao|LINK
Hi Markus33,
this line just create a instance of Person not a context.
For example,you should code like below:
Then Create the database context named WebApplication1Context and add a line,like:
to:
Best Regards.
Yuki Tao
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
251 Points
423 Posts
Re: Action Method Error!
Oct 19, 2018 02:33 PM|Markus33|LINK
Thanks Yuki Tao. I managed to fix the problem so much appreciative! Markus.