How to Create the Edit/Delete/Create Views With a Code first database
up vote
-1
down vote
favorite
This Is my model for my ClientController
namespace CV_Website.Models
{
public class Clients
{
[Key]
public int ID { get; set; }
public int Age { get; set; }
public string Gender { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
.......
public List<CV> cVs;
}
public class CV
{
public string ID { get; set; }
public string Name { get; set; }
public string Job { get; set; }
public bool Public { get; set; }
}
}
And my DbContext
namespace CV_Website.Models
{
public class ClientsContext : DbContext
{
public ClientsContext() : base("name=ClientsContext")
{
}
public DbSet<Clients> Clients { get; set; }
}
I've been able to Populate the View With hard coded users (made for testing) but not from the model
this is my hard coded users
public List<Clients> GenerateCV()
{
data.AllClients.Clear()
List<Clients> result = new List<Clients>();
List<ClientsContext> contexts = new List<ClientsContext>()
Clients test = new Clients
{
Name = "John",
Age = 18,
Gender = "Male",
...
};
Clients test2 = new Clients
{
Name = "Sam",
Age = 18,
Public = true
...
};
result.Add(test);
result.Add(test2);
return result;
}`
I'm not certain how to add the Create/Edit/View ,views using the MVC Scaffolding tool and i would mostly get a System.NullReferenceException
I'm still new at coding and not sure what I'm doing wrong
Thanks in Advance
c# asp.net-mvc database code-first
add a comment |
up vote
-1
down vote
favorite
This Is my model for my ClientController
namespace CV_Website.Models
{
public class Clients
{
[Key]
public int ID { get; set; }
public int Age { get; set; }
public string Gender { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
.......
public List<CV> cVs;
}
public class CV
{
public string ID { get; set; }
public string Name { get; set; }
public string Job { get; set; }
public bool Public { get; set; }
}
}
And my DbContext
namespace CV_Website.Models
{
public class ClientsContext : DbContext
{
public ClientsContext() : base("name=ClientsContext")
{
}
public DbSet<Clients> Clients { get; set; }
}
I've been able to Populate the View With hard coded users (made for testing) but not from the model
this is my hard coded users
public List<Clients> GenerateCV()
{
data.AllClients.Clear()
List<Clients> result = new List<Clients>();
List<ClientsContext> contexts = new List<ClientsContext>()
Clients test = new Clients
{
Name = "John",
Age = 18,
Gender = "Male",
...
};
Clients test2 = new Clients
{
Name = "Sam",
Age = 18,
Public = true
...
};
result.Add(test);
result.Add(test2);
return result;
}`
I'm not certain how to add the Create/Edit/View ,views using the MVC Scaffolding tool and i would mostly get a System.NullReferenceException
I'm still new at coding and not sure what I'm doing wrong
Thanks in Advance
c# asp.net-mvc database code-first
i think following a basic on-line tutorial would be more time effective for increasing your knowledge around this technology.
– JohnB
Nov 20 at 3:59
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
This Is my model for my ClientController
namespace CV_Website.Models
{
public class Clients
{
[Key]
public int ID { get; set; }
public int Age { get; set; }
public string Gender { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
.......
public List<CV> cVs;
}
public class CV
{
public string ID { get; set; }
public string Name { get; set; }
public string Job { get; set; }
public bool Public { get; set; }
}
}
And my DbContext
namespace CV_Website.Models
{
public class ClientsContext : DbContext
{
public ClientsContext() : base("name=ClientsContext")
{
}
public DbSet<Clients> Clients { get; set; }
}
I've been able to Populate the View With hard coded users (made for testing) but not from the model
this is my hard coded users
public List<Clients> GenerateCV()
{
data.AllClients.Clear()
List<Clients> result = new List<Clients>();
List<ClientsContext> contexts = new List<ClientsContext>()
Clients test = new Clients
{
Name = "John",
Age = 18,
Gender = "Male",
...
};
Clients test2 = new Clients
{
Name = "Sam",
Age = 18,
Public = true
...
};
result.Add(test);
result.Add(test2);
return result;
}`
I'm not certain how to add the Create/Edit/View ,views using the MVC Scaffolding tool and i would mostly get a System.NullReferenceException
I'm still new at coding and not sure what I'm doing wrong
Thanks in Advance
c# asp.net-mvc database code-first
This Is my model for my ClientController
namespace CV_Website.Models
{
public class Clients
{
[Key]
public int ID { get; set; }
public int Age { get; set; }
public string Gender { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
.......
public List<CV> cVs;
}
public class CV
{
public string ID { get; set; }
public string Name { get; set; }
public string Job { get; set; }
public bool Public { get; set; }
}
}
And my DbContext
namespace CV_Website.Models
{
public class ClientsContext : DbContext
{
public ClientsContext() : base("name=ClientsContext")
{
}
public DbSet<Clients> Clients { get; set; }
}
I've been able to Populate the View With hard coded users (made for testing) but not from the model
this is my hard coded users
public List<Clients> GenerateCV()
{
data.AllClients.Clear()
List<Clients> result = new List<Clients>();
List<ClientsContext> contexts = new List<ClientsContext>()
Clients test = new Clients
{
Name = "John",
Age = 18,
Gender = "Male",
...
};
Clients test2 = new Clients
{
Name = "Sam",
Age = 18,
Public = true
...
};
result.Add(test);
result.Add(test2);
return result;
}`
I'm not certain how to add the Create/Edit/View ,views using the MVC Scaffolding tool and i would mostly get a System.NullReferenceException
I'm still new at coding and not sure what I'm doing wrong
Thanks in Advance
c# asp.net-mvc database code-first
c# asp.net-mvc database code-first
edited Nov 20 at 3:48
user3559349
asked Nov 20 at 3:37
Raymond Riekert
32
32
i think following a basic on-line tutorial would be more time effective for increasing your knowledge around this technology.
– JohnB
Nov 20 at 3:59
add a comment |
i think following a basic on-line tutorial would be more time effective for increasing your knowledge around this technology.
– JohnB
Nov 20 at 3:59
i think following a basic on-line tutorial would be more time effective for increasing your knowledge around this technology.
– JohnB
Nov 20 at 3:59
i think following a basic on-line tutorial would be more time effective for increasing your knowledge around this technology.
– JohnB
Nov 20 at 3:59
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
Firstly, classes should be nouns according to Microsoft's naming conventions, so I would rename Clients to Client.
As you said you don't have issues with the database, I will focus on controllers and views.
List Clients
The following action in your client controller would return a list of clients from your database to a View:
// GET: CV_Website/Clients/ClientList
[HttpGet]
public ActionResult ClientList()
{
//using statement disposes the connection to the database once query has completed
using (var context = new ClientContext())
{
//.ToList runs the query and maps the result to List<Client>
var clients = context.Clients.ToList();
}
//Return view with list of clients as the model
return View("ClientList", clients);
}
Simply right-click anywhere within this method and select Add View to create the view.
If you select 'List' as the template and 'Client (CV_Website.Models)' as the Model class, it will create a View that lists the details for each client in the list.
In the View, you can see the following lines of code:
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ })
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
These are the URL's that point to actions within your controller.
Edit Client
As an example, here is the implementation for Edit:
//Added third parameter to clearly point to 'Client' controller
@Html.ActionLink("Edit", "Edit", "Client", new { id=item.ID })
This URL points to an action named Edit in the client controller that will read another View named 'Edit'. The implementation would look similar to this:
// GET: CV_Website/Clients/Edit/1
[HttpGet]
public ActionResult Edit(int id)
{
using (var context = new ClientContext())
{
//Using Linq, select the client with the matching ID or return null
var client = context.Clients.SingleOrDefault(c => c.Id == id);
}
return View("ClientList", client);
}
Once again, right-click and select Add View. This time choose the Edit template and the client model again.
This will create a View with a form that can be submitted to the controller. To improve readability, I would write the using statement like this:
@using (Html.BeginForm("Edit", "Client", FormMethod.Post))
The implementation of the Edit action would be similar to this:
// POST: CV_Website/Clients/Edit/{Client}
[HttpPost]
public ActionResult Edit(Client client)
{
using (var context = new ClientContext())
{
//Get client from database
var clientInDb = context.Clients.SingleOrDefault(c => c.Id == client.ID);
//Update client using properties from the client parameter
clientInDb.Age = client.Age;
clientInDb.Gender = client.Gender;
clientInDb.Name = client.Name;
clientInDb.Surname = client.Surname;
//Commit changes to the database
context.SaveChanges();
}
return View("ClientList", client);
}
This updates the client in the database and saves the changes.
I hope this helps to get you started.
More information on DbContext here
Thanks managed to get it to work with something similar :)
– Raymond Riekert
Nov 22 at 17:32
add a comment |
up vote
0
down vote
Code first creates a database for us based on our classes, Please refer the following links for your answer:
Basics for code first approach
Code first demo
Creating the Database is not the problem.To add the control the data with an MVC application is where I'm struggling
– Raymond Riekert
Nov 20 at 11:17
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Firstly, classes should be nouns according to Microsoft's naming conventions, so I would rename Clients to Client.
As you said you don't have issues with the database, I will focus on controllers and views.
List Clients
The following action in your client controller would return a list of clients from your database to a View:
// GET: CV_Website/Clients/ClientList
[HttpGet]
public ActionResult ClientList()
{
//using statement disposes the connection to the database once query has completed
using (var context = new ClientContext())
{
//.ToList runs the query and maps the result to List<Client>
var clients = context.Clients.ToList();
}
//Return view with list of clients as the model
return View("ClientList", clients);
}
Simply right-click anywhere within this method and select Add View to create the view.
If you select 'List' as the template and 'Client (CV_Website.Models)' as the Model class, it will create a View that lists the details for each client in the list.
In the View, you can see the following lines of code:
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ })
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
These are the URL's that point to actions within your controller.
Edit Client
As an example, here is the implementation for Edit:
//Added third parameter to clearly point to 'Client' controller
@Html.ActionLink("Edit", "Edit", "Client", new { id=item.ID })
This URL points to an action named Edit in the client controller that will read another View named 'Edit'. The implementation would look similar to this:
// GET: CV_Website/Clients/Edit/1
[HttpGet]
public ActionResult Edit(int id)
{
using (var context = new ClientContext())
{
//Using Linq, select the client with the matching ID or return null
var client = context.Clients.SingleOrDefault(c => c.Id == id);
}
return View("ClientList", client);
}
Once again, right-click and select Add View. This time choose the Edit template and the client model again.
This will create a View with a form that can be submitted to the controller. To improve readability, I would write the using statement like this:
@using (Html.BeginForm("Edit", "Client", FormMethod.Post))
The implementation of the Edit action would be similar to this:
// POST: CV_Website/Clients/Edit/{Client}
[HttpPost]
public ActionResult Edit(Client client)
{
using (var context = new ClientContext())
{
//Get client from database
var clientInDb = context.Clients.SingleOrDefault(c => c.Id == client.ID);
//Update client using properties from the client parameter
clientInDb.Age = client.Age;
clientInDb.Gender = client.Gender;
clientInDb.Name = client.Name;
clientInDb.Surname = client.Surname;
//Commit changes to the database
context.SaveChanges();
}
return View("ClientList", client);
}
This updates the client in the database and saves the changes.
I hope this helps to get you started.
More information on DbContext here
Thanks managed to get it to work with something similar :)
– Raymond Riekert
Nov 22 at 17:32
add a comment |
up vote
0
down vote
accepted
Firstly, classes should be nouns according to Microsoft's naming conventions, so I would rename Clients to Client.
As you said you don't have issues with the database, I will focus on controllers and views.
List Clients
The following action in your client controller would return a list of clients from your database to a View:
// GET: CV_Website/Clients/ClientList
[HttpGet]
public ActionResult ClientList()
{
//using statement disposes the connection to the database once query has completed
using (var context = new ClientContext())
{
//.ToList runs the query and maps the result to List<Client>
var clients = context.Clients.ToList();
}
//Return view with list of clients as the model
return View("ClientList", clients);
}
Simply right-click anywhere within this method and select Add View to create the view.
If you select 'List' as the template and 'Client (CV_Website.Models)' as the Model class, it will create a View that lists the details for each client in the list.
In the View, you can see the following lines of code:
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ })
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
These are the URL's that point to actions within your controller.
Edit Client
As an example, here is the implementation for Edit:
//Added third parameter to clearly point to 'Client' controller
@Html.ActionLink("Edit", "Edit", "Client", new { id=item.ID })
This URL points to an action named Edit in the client controller that will read another View named 'Edit'. The implementation would look similar to this:
// GET: CV_Website/Clients/Edit/1
[HttpGet]
public ActionResult Edit(int id)
{
using (var context = new ClientContext())
{
//Using Linq, select the client with the matching ID or return null
var client = context.Clients.SingleOrDefault(c => c.Id == id);
}
return View("ClientList", client);
}
Once again, right-click and select Add View. This time choose the Edit template and the client model again.
This will create a View with a form that can be submitted to the controller. To improve readability, I would write the using statement like this:
@using (Html.BeginForm("Edit", "Client", FormMethod.Post))
The implementation of the Edit action would be similar to this:
// POST: CV_Website/Clients/Edit/{Client}
[HttpPost]
public ActionResult Edit(Client client)
{
using (var context = new ClientContext())
{
//Get client from database
var clientInDb = context.Clients.SingleOrDefault(c => c.Id == client.ID);
//Update client using properties from the client parameter
clientInDb.Age = client.Age;
clientInDb.Gender = client.Gender;
clientInDb.Name = client.Name;
clientInDb.Surname = client.Surname;
//Commit changes to the database
context.SaveChanges();
}
return View("ClientList", client);
}
This updates the client in the database and saves the changes.
I hope this helps to get you started.
More information on DbContext here
Thanks managed to get it to work with something similar :)
– Raymond Riekert
Nov 22 at 17:32
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Firstly, classes should be nouns according to Microsoft's naming conventions, so I would rename Clients to Client.
As you said you don't have issues with the database, I will focus on controllers and views.
List Clients
The following action in your client controller would return a list of clients from your database to a View:
// GET: CV_Website/Clients/ClientList
[HttpGet]
public ActionResult ClientList()
{
//using statement disposes the connection to the database once query has completed
using (var context = new ClientContext())
{
//.ToList runs the query and maps the result to List<Client>
var clients = context.Clients.ToList();
}
//Return view with list of clients as the model
return View("ClientList", clients);
}
Simply right-click anywhere within this method and select Add View to create the view.
If you select 'List' as the template and 'Client (CV_Website.Models)' as the Model class, it will create a View that lists the details for each client in the list.
In the View, you can see the following lines of code:
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ })
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
These are the URL's that point to actions within your controller.
Edit Client
As an example, here is the implementation for Edit:
//Added third parameter to clearly point to 'Client' controller
@Html.ActionLink("Edit", "Edit", "Client", new { id=item.ID })
This URL points to an action named Edit in the client controller that will read another View named 'Edit'. The implementation would look similar to this:
// GET: CV_Website/Clients/Edit/1
[HttpGet]
public ActionResult Edit(int id)
{
using (var context = new ClientContext())
{
//Using Linq, select the client with the matching ID or return null
var client = context.Clients.SingleOrDefault(c => c.Id == id);
}
return View("ClientList", client);
}
Once again, right-click and select Add View. This time choose the Edit template and the client model again.
This will create a View with a form that can be submitted to the controller. To improve readability, I would write the using statement like this:
@using (Html.BeginForm("Edit", "Client", FormMethod.Post))
The implementation of the Edit action would be similar to this:
// POST: CV_Website/Clients/Edit/{Client}
[HttpPost]
public ActionResult Edit(Client client)
{
using (var context = new ClientContext())
{
//Get client from database
var clientInDb = context.Clients.SingleOrDefault(c => c.Id == client.ID);
//Update client using properties from the client parameter
clientInDb.Age = client.Age;
clientInDb.Gender = client.Gender;
clientInDb.Name = client.Name;
clientInDb.Surname = client.Surname;
//Commit changes to the database
context.SaveChanges();
}
return View("ClientList", client);
}
This updates the client in the database and saves the changes.
I hope this helps to get you started.
More information on DbContext here
Firstly, classes should be nouns according to Microsoft's naming conventions, so I would rename Clients to Client.
As you said you don't have issues with the database, I will focus on controllers and views.
List Clients
The following action in your client controller would return a list of clients from your database to a View:
// GET: CV_Website/Clients/ClientList
[HttpGet]
public ActionResult ClientList()
{
//using statement disposes the connection to the database once query has completed
using (var context = new ClientContext())
{
//.ToList runs the query and maps the result to List<Client>
var clients = context.Clients.ToList();
}
//Return view with list of clients as the model
return View("ClientList", clients);
}
Simply right-click anywhere within this method and select Add View to create the view.
If you select 'List' as the template and 'Client (CV_Website.Models)' as the Model class, it will create a View that lists the details for each client in the list.
In the View, you can see the following lines of code:
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ })
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
These are the URL's that point to actions within your controller.
Edit Client
As an example, here is the implementation for Edit:
//Added third parameter to clearly point to 'Client' controller
@Html.ActionLink("Edit", "Edit", "Client", new { id=item.ID })
This URL points to an action named Edit in the client controller that will read another View named 'Edit'. The implementation would look similar to this:
// GET: CV_Website/Clients/Edit/1
[HttpGet]
public ActionResult Edit(int id)
{
using (var context = new ClientContext())
{
//Using Linq, select the client with the matching ID or return null
var client = context.Clients.SingleOrDefault(c => c.Id == id);
}
return View("ClientList", client);
}
Once again, right-click and select Add View. This time choose the Edit template and the client model again.
This will create a View with a form that can be submitted to the controller. To improve readability, I would write the using statement like this:
@using (Html.BeginForm("Edit", "Client", FormMethod.Post))
The implementation of the Edit action would be similar to this:
// POST: CV_Website/Clients/Edit/{Client}
[HttpPost]
public ActionResult Edit(Client client)
{
using (var context = new ClientContext())
{
//Get client from database
var clientInDb = context.Clients.SingleOrDefault(c => c.Id == client.ID);
//Update client using properties from the client parameter
clientInDb.Age = client.Age;
clientInDb.Gender = client.Gender;
clientInDb.Name = client.Name;
clientInDb.Surname = client.Surname;
//Commit changes to the database
context.SaveChanges();
}
return View("ClientList", client);
}
This updates the client in the database and saves the changes.
I hope this helps to get you started.
More information on DbContext here
answered Nov 22 at 13:03
Nathan Fulleylove
3112
3112
Thanks managed to get it to work with something similar :)
– Raymond Riekert
Nov 22 at 17:32
add a comment |
Thanks managed to get it to work with something similar :)
– Raymond Riekert
Nov 22 at 17:32
Thanks managed to get it to work with something similar :)
– Raymond Riekert
Nov 22 at 17:32
Thanks managed to get it to work with something similar :)
– Raymond Riekert
Nov 22 at 17:32
add a comment |
up vote
0
down vote
Code first creates a database for us based on our classes, Please refer the following links for your answer:
Basics for code first approach
Code first demo
Creating the Database is not the problem.To add the control the data with an MVC application is where I'm struggling
– Raymond Riekert
Nov 20 at 11:17
add a comment |
up vote
0
down vote
Code first creates a database for us based on our classes, Please refer the following links for your answer:
Basics for code first approach
Code first demo
Creating the Database is not the problem.To add the control the data with an MVC application is where I'm struggling
– Raymond Riekert
Nov 20 at 11:17
add a comment |
up vote
0
down vote
up vote
0
down vote
Code first creates a database for us based on our classes, Please refer the following links for your answer:
Basics for code first approach
Code first demo
Code first creates a database for us based on our classes, Please refer the following links for your answer:
Basics for code first approach
Code first demo
answered Nov 20 at 5:25
Divya Agrawal
12
12
Creating the Database is not the problem.To add the control the data with an MVC application is where I'm struggling
– Raymond Riekert
Nov 20 at 11:17
add a comment |
Creating the Database is not the problem.To add the control the data with an MVC application is where I'm struggling
– Raymond Riekert
Nov 20 at 11:17
Creating the Database is not the problem.To add the control the data with an MVC application is where I'm struggling
– Raymond Riekert
Nov 20 at 11:17
Creating the Database is not the problem.To add the control the data with an MVC application is where I'm struggling
– Raymond Riekert
Nov 20 at 11:17
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385852%2fhow-to-create-the-edit-delete-create-views-with-a-code-first-database%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
i think following a basic on-line tutorial would be more time effective for increasing your knowledge around this technology.
– JohnB
Nov 20 at 3:59