Update ReceivedDate Column using ActionResult MVC on button click

1.1k views Asked by At

I have a "Commit This Batch" button on my View where samples are entered into a pre-determined batch.

on click, I would like to do 2 updates:

1--Update all records with batch id=Session["BatchID"] to today's date. (Only one field needs updating but in multiple rows.)

2--update the batch's status to "C" (closed), in the batches table.

Pretty sure I do this from an ActionResult something like this which I know I won't have a problem with:

<button onclick="location.href='@Url.Action("<ActionResultName>", "ControllerName")';return false;">Commit This Batch</button>

In my ActionResult I am totally drawing a blank. Here is SQL from Query Window in Management Studio that I would like to emulate with as little code as possible:

UPDATE dbo.BactiBatches SET BatchStatus= 'C'
WHERE BactiBatchID= <SessionValue>

UPDATE dbo.BactiBucket SET ReceivedDate = GetDate()
WHERE BactiBatchID= <SessionValue>

I'm using EF and MVC4.

1

There are 1 answers

0
JustJohn On

Ok, not sure how to make my question simpler. But I found this answer that voila! works. https://stackoverflow.com/a/8566906/564810

So below is the code in my HomeController that when called does EXACTLY what I want. No need for anything else. Of course, the controller does a lot of other CRUD so EF declaration is at the top:

public class HomeController : Controller {

    DWS_Entities _db;


    public HomeController()
    {

        _db = new DWS_Entities();

    }

public ActionResult CommitThisBatch()
{
    _db.ExecuteStoreCommand("UPDATE dbo.BactiBatches SET BatchStatus = 'C' WHERE BactiBatchID= " + Session["ThisBatch"]);


    _db.ExecuteStoreCommand("UPDATE dbo.BactiBucket SET RecvDate = GetDate() WHERE Batch_ID= " + Session["ThisBatch"]);

    return RedirectToAction("AddColiform");

}