0

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.

JustJohn
  • 1,362
  • 2
  • 22
  • 44
  • what exactly are you looking for? Do you have a clear flow in my mind? are you looking for EF query or Controller level code? if you've done anything in your controller, do post it here – Mox Shah Nov 15 '14 at 04:39
  • I would assume that I would use both, Linq to make the update inside of Controller ActionResult as it shows in my button click example. One table gets "O" changed to "C", other table gets null changed to datetime value for today. First table name is "BactiBatches". Second table name is BactiBucket. At the End of ActionResult that does the updating the user is returned to calling page. No extra views. – JustJohn Nov 15 '14 at 20:40
  • It really is simple, not sure how to ask such a simple question. I'll try again: MVC 4, Entity Framework, need to update a column value in 2 different tables. – JustJohn Nov 15 '14 at 20:53

1 Answers1

0

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");

}
Community
  • 1
  • 1
JustJohn
  • 1,362
  • 2
  • 22
  • 44