How to update a table in controller based on values in viewHow do you give a C# Auto-Property a default value?How do I update the GUI from another thread?How to loop through all enum values in C#?Dynamic Controls in a Strongly Typed View (ASP.NET MVC)Why does a submit button pass null data from view to controllerjqGrid not displaying data but paging and column names are displayed/working fineHow to send dropdown value from view to controllerHow to mass update a table in MVC view instead of one record at a time?How do I update two tables in a single method?how to select records from multiple table with max count value from one table using Linq in Asp.net MVC C#

Was it ever possible to target a zone?

What are some interesting features that are common cross-linguistically but don't exist in English?

Papers on arXiv solving the same problem at the same time

How to respectfully refuse to assist co-workers with IT issues?

Another solution to create a set with two conditions

Is "The life is beautiful" incorrect or just very non-idiomatic?

Sum ergo cogito?

Numbers Decrease while Letters Increase

Why do all fields in a QFT transform like *irreducible* representations of some group?

Is it okay to keep opened loose leaf tea packages in the freezer?

Why do banks “park” their money at the European Central Bank?

Handling Disruptive Student on the Autistic Spectrum

What would make bones be of different colors?

Why is there a difference between predicting on Validation set and Test set?

Why did Khan ask Admiral James T. Kirk about Project Genesis?

Did a flight controller ever answer Flight with a no-go?

Why doesn't the Bitcoin-qt client ask for the Wallet passphrase upon startup?

I don't have the theoretical background in my PhD topic. I can't justify getting the degree

Is gzip atomic?

moon structure ownership

Can I add a link to my website in a paper I'm coauthoring?

Can RMSE and MAE have the same value?

How to know which loss function is suitable for image classification?

Two questions about typesetting a Roman missal



How to update a table in controller based on values in view


How do you give a C# Auto-Property a default value?How do I update the GUI from another thread?How to loop through all enum values in C#?Dynamic Controls in a Strongly Typed View (ASP.NET MVC)Why does a submit button pass null data from view to controllerjqGrid not displaying data but paging and column names are displayed/working fineHow to send dropdown value from view to controllerHow to mass update a table in MVC view instead of one record at a time?How do I update two tables in a single method?how to select records from multiple table with max count value from one table using Linq in Asp.net MVC C#






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I have a model that includes several tables. My Edit View allows users to modify a single record from the main table ICS_Transactions. Based on the users selected action (from dropdown). . . I need to update a field, in a secondary table (ICS_Supplies). In short, The field (OnHand) that shows the amount of inventory on hand, needs to be updated to the OnHand Amount minus the ordered amount.



The problem I am having is that my Edit View updates the ICS_Transaction Table, but I also need to update the ICS_Supplies table. And because MVC5 and the use of controllers (and Entity Framework and C#) are relatively new to me . . I am struggling. I come from the older asp.net web form days where I would just write the sql in vb.net and execute it on event.



I have a class which contains all the fields from both tables



 public class Edit2ViewModel

public int? TransID get; set;

[Display(Name = "Supplies")]
public string ItemDescription get; set;

[Display(Name = "Contact")]
public string ContactName get; set;

[Display(Name = "Delivery Unit")]
public string DeliveryUnit get; set;

public string Street get; set;

public string Building get; set;

public string Room get; set;
public string City get; set;
public string State get; set;
public string ZipCode get; set;
public string PhoneNumber get; set;
public string UnitMeasure get; set;
public string CurrentStatus get; set;
public int? OnHand get; set;
public int? UnitsOrdered get; set;




Then, on my Edit Controller, I have the following



 public ActionResult Edit(int? id)

if (id == null)

return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

ICS_Transactions iCS_Transactions = db.ICS_Transactions.Find(id);
if (iCS_Transactions == null)

return HttpNotFound();


// Find User Friendly Values

var r = from tr in db.ICS_Transactions

join un in db.ICS_Units
on tr.DeliveryUnitID equals un.DeliveryUnitID
join kt in db.ICS_Contacts
on tr.Contact equals kt.LoginID
join sp in db.ICS_Supplies on tr.SuppliesID equals sp.Supplies_ID
where tr.TransID == id

select new Edit2ViewModel



ItemDescription = sp.ItemDescription,
ContactName = kt.ContactName,
DeliveryUnit = un.DeliveryUnit,
Street = un.Street,
Building = un.Building,
City = un.City,
State = un.State,
ZipCode = un.ZipCode,
PhoneNumber = un.PhoneNumber,
UnitMeasure = sp.UnitMeasure,
CurrentStatus = tr.CurrentStatus,
OnHand = sp.OnHand,
UnitsOrdered = tr.UnitsOrdered



;

// set user friendly values as ViewBag


ViewBag.Stock = r.FirstOrDefault().OnHand;
ViewBag.Ordered = r.FirstOrDefault().UnitsOrdered;
ViewBag.Total = (int)ViewBag.Stock - (int)ViewBag.Ordered;

return View(iCS_Transactions);



This is where I begin to get lost. So far, all works as expected. On My EDIT VIEW, I can use the values from the Viewbag(s) to populate the view with the amount of stock on hand. The amount of stock ordered, and then the remaining stock after the order is procsessed (Viewbag.Total). These values are NOT located in the ICS_Transactions table . . which is why I used the Viewbag to pull them from the ICS_Supplies Table.



But now, when the user processes the order, and hits the save button, I want to update the ICS_Supplies Table (not the ICS_Transactions Table) with the NEW amount on hand (Viewbag.Total). But I can't figure out how to step backwards to the controller and update the ICS_Supplies table.



Here is my Edit/Post Code (so far). I think this is where I would put the code to update, but I need some help getting started . . .



 // POST: OrderManagement/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "TransID,SuppliesID,OriginalDate,TransType,LastUpdatedBy,Contact,OpenClosed,CurrentStatus,CurrentStatusDate,RequsitionNumber,PONumber,DeliveryMonth,DeliveryYear,UnitsOrdered,Emergency,Comments,DeliveryUnitID")] ICS_Transactions iCS_Transactions)

if (ModelState.IsValid)

db.Entry(iCS_Transactions).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");

return View(iCS_Transactions);










share|improve this question






























    0















    I have a model that includes several tables. My Edit View allows users to modify a single record from the main table ICS_Transactions. Based on the users selected action (from dropdown). . . I need to update a field, in a secondary table (ICS_Supplies). In short, The field (OnHand) that shows the amount of inventory on hand, needs to be updated to the OnHand Amount minus the ordered amount.



    The problem I am having is that my Edit View updates the ICS_Transaction Table, but I also need to update the ICS_Supplies table. And because MVC5 and the use of controllers (and Entity Framework and C#) are relatively new to me . . I am struggling. I come from the older asp.net web form days where I would just write the sql in vb.net and execute it on event.



    I have a class which contains all the fields from both tables



     public class Edit2ViewModel

    public int? TransID get; set;

    [Display(Name = "Supplies")]
    public string ItemDescription get; set;

    [Display(Name = "Contact")]
    public string ContactName get; set;

    [Display(Name = "Delivery Unit")]
    public string DeliveryUnit get; set;

    public string Street get; set;

    public string Building get; set;

    public string Room get; set;
    public string City get; set;
    public string State get; set;
    public string ZipCode get; set;
    public string PhoneNumber get; set;
    public string UnitMeasure get; set;
    public string CurrentStatus get; set;
    public int? OnHand get; set;
    public int? UnitsOrdered get; set;




    Then, on my Edit Controller, I have the following



     public ActionResult Edit(int? id)

    if (id == null)

    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

    ICS_Transactions iCS_Transactions = db.ICS_Transactions.Find(id);
    if (iCS_Transactions == null)

    return HttpNotFound();


    // Find User Friendly Values

    var r = from tr in db.ICS_Transactions

    join un in db.ICS_Units
    on tr.DeliveryUnitID equals un.DeliveryUnitID
    join kt in db.ICS_Contacts
    on tr.Contact equals kt.LoginID
    join sp in db.ICS_Supplies on tr.SuppliesID equals sp.Supplies_ID
    where tr.TransID == id

    select new Edit2ViewModel



    ItemDescription = sp.ItemDescription,
    ContactName = kt.ContactName,
    DeliveryUnit = un.DeliveryUnit,
    Street = un.Street,
    Building = un.Building,
    City = un.City,
    State = un.State,
    ZipCode = un.ZipCode,
    PhoneNumber = un.PhoneNumber,
    UnitMeasure = sp.UnitMeasure,
    CurrentStatus = tr.CurrentStatus,
    OnHand = sp.OnHand,
    UnitsOrdered = tr.UnitsOrdered



    ;

    // set user friendly values as ViewBag


    ViewBag.Stock = r.FirstOrDefault().OnHand;
    ViewBag.Ordered = r.FirstOrDefault().UnitsOrdered;
    ViewBag.Total = (int)ViewBag.Stock - (int)ViewBag.Ordered;

    return View(iCS_Transactions);



    This is where I begin to get lost. So far, all works as expected. On My EDIT VIEW, I can use the values from the Viewbag(s) to populate the view with the amount of stock on hand. The amount of stock ordered, and then the remaining stock after the order is procsessed (Viewbag.Total). These values are NOT located in the ICS_Transactions table . . which is why I used the Viewbag to pull them from the ICS_Supplies Table.



    But now, when the user processes the order, and hits the save button, I want to update the ICS_Supplies Table (not the ICS_Transactions Table) with the NEW amount on hand (Viewbag.Total). But I can't figure out how to step backwards to the controller and update the ICS_Supplies table.



    Here is my Edit/Post Code (so far). I think this is where I would put the code to update, but I need some help getting started . . .



     // POST: OrderManagement/Edit/5
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = "TransID,SuppliesID,OriginalDate,TransType,LastUpdatedBy,Contact,OpenClosed,CurrentStatus,CurrentStatusDate,RequsitionNumber,PONumber,DeliveryMonth,DeliveryYear,UnitsOrdered,Emergency,Comments,DeliveryUnitID")] ICS_Transactions iCS_Transactions)

    if (ModelState.IsValid)

    db.Entry(iCS_Transactions).State = EntityState.Modified;
    db.SaveChanges();
    return RedirectToAction("Index");

    return View(iCS_Transactions);










    share|improve this question


























      0












      0








      0








      I have a model that includes several tables. My Edit View allows users to modify a single record from the main table ICS_Transactions. Based on the users selected action (from dropdown). . . I need to update a field, in a secondary table (ICS_Supplies). In short, The field (OnHand) that shows the amount of inventory on hand, needs to be updated to the OnHand Amount minus the ordered amount.



      The problem I am having is that my Edit View updates the ICS_Transaction Table, but I also need to update the ICS_Supplies table. And because MVC5 and the use of controllers (and Entity Framework and C#) are relatively new to me . . I am struggling. I come from the older asp.net web form days where I would just write the sql in vb.net and execute it on event.



      I have a class which contains all the fields from both tables



       public class Edit2ViewModel

      public int? TransID get; set;

      [Display(Name = "Supplies")]
      public string ItemDescription get; set;

      [Display(Name = "Contact")]
      public string ContactName get; set;

      [Display(Name = "Delivery Unit")]
      public string DeliveryUnit get; set;

      public string Street get; set;

      public string Building get; set;

      public string Room get; set;
      public string City get; set;
      public string State get; set;
      public string ZipCode get; set;
      public string PhoneNumber get; set;
      public string UnitMeasure get; set;
      public string CurrentStatus get; set;
      public int? OnHand get; set;
      public int? UnitsOrdered get; set;




      Then, on my Edit Controller, I have the following



       public ActionResult Edit(int? id)

      if (id == null)

      return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

      ICS_Transactions iCS_Transactions = db.ICS_Transactions.Find(id);
      if (iCS_Transactions == null)

      return HttpNotFound();


      // Find User Friendly Values

      var r = from tr in db.ICS_Transactions

      join un in db.ICS_Units
      on tr.DeliveryUnitID equals un.DeliveryUnitID
      join kt in db.ICS_Contacts
      on tr.Contact equals kt.LoginID
      join sp in db.ICS_Supplies on tr.SuppliesID equals sp.Supplies_ID
      where tr.TransID == id

      select new Edit2ViewModel



      ItemDescription = sp.ItemDescription,
      ContactName = kt.ContactName,
      DeliveryUnit = un.DeliveryUnit,
      Street = un.Street,
      Building = un.Building,
      City = un.City,
      State = un.State,
      ZipCode = un.ZipCode,
      PhoneNumber = un.PhoneNumber,
      UnitMeasure = sp.UnitMeasure,
      CurrentStatus = tr.CurrentStatus,
      OnHand = sp.OnHand,
      UnitsOrdered = tr.UnitsOrdered



      ;

      // set user friendly values as ViewBag


      ViewBag.Stock = r.FirstOrDefault().OnHand;
      ViewBag.Ordered = r.FirstOrDefault().UnitsOrdered;
      ViewBag.Total = (int)ViewBag.Stock - (int)ViewBag.Ordered;

      return View(iCS_Transactions);



      This is where I begin to get lost. So far, all works as expected. On My EDIT VIEW, I can use the values from the Viewbag(s) to populate the view with the amount of stock on hand. The amount of stock ordered, and then the remaining stock after the order is procsessed (Viewbag.Total). These values are NOT located in the ICS_Transactions table . . which is why I used the Viewbag to pull them from the ICS_Supplies Table.



      But now, when the user processes the order, and hits the save button, I want to update the ICS_Supplies Table (not the ICS_Transactions Table) with the NEW amount on hand (Viewbag.Total). But I can't figure out how to step backwards to the controller and update the ICS_Supplies table.



      Here is my Edit/Post Code (so far). I think this is where I would put the code to update, but I need some help getting started . . .



       // POST: OrderManagement/Edit/5
      // To protect from overposting attacks, please enable the specific properties you want to bind to, for
      // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
      [HttpPost]
      [ValidateAntiForgeryToken]
      public ActionResult Edit([Bind(Include = "TransID,SuppliesID,OriginalDate,TransType,LastUpdatedBy,Contact,OpenClosed,CurrentStatus,CurrentStatusDate,RequsitionNumber,PONumber,DeliveryMonth,DeliveryYear,UnitsOrdered,Emergency,Comments,DeliveryUnitID")] ICS_Transactions iCS_Transactions)

      if (ModelState.IsValid)

      db.Entry(iCS_Transactions).State = EntityState.Modified;
      db.SaveChanges();
      return RedirectToAction("Index");

      return View(iCS_Transactions);










      share|improve this question














      I have a model that includes several tables. My Edit View allows users to modify a single record from the main table ICS_Transactions. Based on the users selected action (from dropdown). . . I need to update a field, in a secondary table (ICS_Supplies). In short, The field (OnHand) that shows the amount of inventory on hand, needs to be updated to the OnHand Amount minus the ordered amount.



      The problem I am having is that my Edit View updates the ICS_Transaction Table, but I also need to update the ICS_Supplies table. And because MVC5 and the use of controllers (and Entity Framework and C#) are relatively new to me . . I am struggling. I come from the older asp.net web form days where I would just write the sql in vb.net and execute it on event.



      I have a class which contains all the fields from both tables



       public class Edit2ViewModel

      public int? TransID get; set;

      [Display(Name = "Supplies")]
      public string ItemDescription get; set;

      [Display(Name = "Contact")]
      public string ContactName get; set;

      [Display(Name = "Delivery Unit")]
      public string DeliveryUnit get; set;

      public string Street get; set;

      public string Building get; set;

      public string Room get; set;
      public string City get; set;
      public string State get; set;
      public string ZipCode get; set;
      public string PhoneNumber get; set;
      public string UnitMeasure get; set;
      public string CurrentStatus get; set;
      public int? OnHand get; set;
      public int? UnitsOrdered get; set;




      Then, on my Edit Controller, I have the following



       public ActionResult Edit(int? id)

      if (id == null)

      return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

      ICS_Transactions iCS_Transactions = db.ICS_Transactions.Find(id);
      if (iCS_Transactions == null)

      return HttpNotFound();


      // Find User Friendly Values

      var r = from tr in db.ICS_Transactions

      join un in db.ICS_Units
      on tr.DeliveryUnitID equals un.DeliveryUnitID
      join kt in db.ICS_Contacts
      on tr.Contact equals kt.LoginID
      join sp in db.ICS_Supplies on tr.SuppliesID equals sp.Supplies_ID
      where tr.TransID == id

      select new Edit2ViewModel



      ItemDescription = sp.ItemDescription,
      ContactName = kt.ContactName,
      DeliveryUnit = un.DeliveryUnit,
      Street = un.Street,
      Building = un.Building,
      City = un.City,
      State = un.State,
      ZipCode = un.ZipCode,
      PhoneNumber = un.PhoneNumber,
      UnitMeasure = sp.UnitMeasure,
      CurrentStatus = tr.CurrentStatus,
      OnHand = sp.OnHand,
      UnitsOrdered = tr.UnitsOrdered



      ;

      // set user friendly values as ViewBag


      ViewBag.Stock = r.FirstOrDefault().OnHand;
      ViewBag.Ordered = r.FirstOrDefault().UnitsOrdered;
      ViewBag.Total = (int)ViewBag.Stock - (int)ViewBag.Ordered;

      return View(iCS_Transactions);



      This is where I begin to get lost. So far, all works as expected. On My EDIT VIEW, I can use the values from the Viewbag(s) to populate the view with the amount of stock on hand. The amount of stock ordered, and then the remaining stock after the order is procsessed (Viewbag.Total). These values are NOT located in the ICS_Transactions table . . which is why I used the Viewbag to pull them from the ICS_Supplies Table.



      But now, when the user processes the order, and hits the save button, I want to update the ICS_Supplies Table (not the ICS_Transactions Table) with the NEW amount on hand (Viewbag.Total). But I can't figure out how to step backwards to the controller and update the ICS_Supplies table.



      Here is my Edit/Post Code (so far). I think this is where I would put the code to update, but I need some help getting started . . .



       // POST: OrderManagement/Edit/5
      // To protect from overposting attacks, please enable the specific properties you want to bind to, for
      // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
      [HttpPost]
      [ValidateAntiForgeryToken]
      public ActionResult Edit([Bind(Include = "TransID,SuppliesID,OriginalDate,TransType,LastUpdatedBy,Contact,OpenClosed,CurrentStatus,CurrentStatusDate,RequsitionNumber,PONumber,DeliveryMonth,DeliveryYear,UnitsOrdered,Emergency,Comments,DeliveryUnitID")] ICS_Transactions iCS_Transactions)

      if (ModelState.IsValid)

      db.Entry(iCS_Transactions).State = EntityState.Modified;
      db.SaveChanges();
      return RedirectToAction("Index");

      return View(iCS_Transactions);







      c# asp.net-mvc-5






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 27 at 18:26









      ExecChefExecChef

      1459 bronze badges




      1459 bronze badges

























          0






          active

          oldest

          votes










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55384193%2fhow-to-update-a-table-in-controller-based-on-values-in-view%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes




          Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.







          Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.



















          draft saved

          draft discarded
















































          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55384193%2fhow-to-update-a-table-in-controller-based-on-values-in-view%23new-answer', 'question_page');

          );

          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







          Popular posts from this blog

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

          은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현