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;
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
add a comment |
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
add a comment |
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
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
c# asp.net-mvc-5
asked Mar 27 at 18:26
ExecChefExecChef
1459 bronze badges
1459 bronze badges
add a comment |
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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.
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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