Reputation: 367
I am using controller to try and edit values within the database.
public ActionResult test(String t, String s)
{
if (t!= null)
{
if (radio_select == "s")
{
if (s!= null)
{
// I want to know how to edit my database values here.
}
}
}
return View();
}
Below is my database
I want to Edit these values from 9 -> 10 and 19 -> 20 . And Do this in controller.
Upvotes: 0
Views: 1957
Reputation: 218702
To edit a record, you need to select a record first.To select a record, you need a unique id.
The below code will update your record to the new value.
// The id and new values are hard coded here.
// You might want to get it from the view as your action method parameters
// or somewhere else.
var id = 2;
var responseBitNewValue = 10;
var frequencyNewValue = 19;
var db = new YourDbContext();
var entityItem = db.Responses.FirstOrDefault(s=>s.Id==id);
if(entityItem !=null)
{
entityItem.response_bit = responseBitNewValue ;
entityItem.frequency = frequencyNewValue ;
db.Entry(entityItem).State = EntityState.Modified;
db.SaveChanges();
}
Assuming YourDbContext
is the name of your db context class and Responses
is a property on that ( which is a collection of entities representing your table record)l
Upvotes: 1
Reputation: 13551
First of all, yes, this is entirely possible and in fact is the case for edit and create links.
You will need to add a model to your project, you can then use that and entity framework to update your database. See this tutorial from MS.
In fact you should always be making changes to your database from within a controller and never from within a view.
Upvotes: 0