How to update my data table with POST values. ASP.NET

I want to update values ​​in my database. to make sure my script was working and to send the values ​​I created a POST method to check and the values ​​are coming.

How to update my data table with POST values. ASP.NET

My question now is with the values ​​coming to the method, how to update or save my values ​​in the database?

How to update my data table with POST values. ASP.NET

Controller:

    [HttpPost]
    public JsonResult EditPost(Programa_Cor_Info_Status statusData)
    {
        Programa_Cor_Info_Status status = new Programa_Cor_Info_Status
        {
            ID_Info = statusData.ID_Info,
            Status = statusData.Status,
            Obs = statusData.Obs,
        };

        return Json(status, JsonRequestBehavior.AllowGet);
    }

I tried using db.savechanges on my controller but to no avail.
Could someone help me with an example?

Thanks

————Update—————————————–

[HttpPost]
    public ActionResult EditPost(Programa_Cor_Info_Status statusData, int ID_Status)
    {
        Programa_Cor_Info_Status status = new Programa_Cor_Info_Status
        {
            ID_Info = statusData.ID_Info,
            Status = statusData.Status,
            Obs = statusData.Obs,
        };

        var q = db.Programa_Cor_Info_Status.Where(m => m.ID_Info == ID_Status).FirstOrDefault();
        q.ID_Info = ID_Status;
        db.Entry(q).State = EntityState.Modified;
        db.SaveChanges();

        return Json(status, JsonRequestBehavior.AllowGet);
    }

How to update my data table with POST values. ASP.NET
namespace Balu0._1.Models
{
using System;
using System.Collections.Generic;

public partial class Programa_Cor_Info_Status
 {
    public int ID_Info { get; set; }
    public int ID_Programa { get; set; }
    public int ID_Linha_Cor { get; set; }
    public string Status { get; set; }
    public string Obs { get; set; }
 }
}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

If you stll don’t have, in your view add hidden model field with ID_Info value.
Change your action to this:

    public ActionResult EditPost(Programa_Cor_Info_Status statusData)
    {
            var existItem = db.Programa_Cor_Info_Status.Find(statusData.ID_Info);

// or if you dont have a  proper primary key you can try
 var existItem = db.Programa_Cor_Info_Status
                .Where( i=> i.ID_Info== statusData.ID_Info).FirstOrDefault();

                if (existItem != null)
                {
                    
                db.Entry(existItem).CurrentValues.SetValues(statusData);
                var result = db.SaveChanges(); // if result==0 then error
               } else ...error

        return Json(statusData, JsonRequestBehavior.AllowGet);
    }

Method 2

thanks for the help i already got !!!

db.Entry(status).State = EntityState.Modified;
        db.SaveChanges();
        return Json(status, JsonRequestBehavior.AllowGet);


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x