0

I have a dynamic table in my form on Save button click I am accessing the whole table and storing it to array using javascript. FOllowing is the javascript

function SaveData() {
    var MedicineName = new Array();
    var BatchNo = new Array();
    var ExpiryDate = new Array();
    var Qty = new Array();
    var FreeQty = new Array();
    var PurRate = new Array();
    var Mrp = new Array();
    var SellRate = new Array();
    var Unit = new Array();
    var Amount = new Array();
    var DiscPer = new Array();
    var DiscAmt = new Array();
    var GrossAmt = new Array();
    var VatPer = new Array();
    var VatAmt = new Array();
    var AddVatPer = new Array();
    var AddVatAmt = new Array();
    var NetAmt = new Array();
    $("table#tbl1 tr").each(function (row, tr) {
    MedicineName[row] = $(tr).find('td:eq(1)').text()
    BatchNo[row]  = $(tr).find('td:eq(2)').text()
    ExpiryDate[row]  = $(tr).find('td:eq(3)').text()
    Qty[row]  = $(tr).find('td:eq(4)').text()
    FreeQty[row]  = $(tr).find('td:eq(5)').text()
    PurRate[row]  = $(tr).find('td:eq(6)').text()
    Mrp[row]  = $(tr).find('td:eq(7)').text()
    SellRate[row]  = $(tr).find('td:eq(8)').text()
    Unit[row]  = $(tr).find('td:eq(9)').text()
    Amount[row]  = $(tr).find('td:eq(10)').text()
    DiscPer[row]  = $(tr).find('td:eq(11)').text()
    DiscAmt[row]  = $(tr).find('td:eq(12)').text()
    GrossAmt[row]  = $(tr).find('td:eq(13)').text()
    VatPer[row]  = $(tr).find('td:eq(14)').text()
    VatAmt[row]  = $(tr).find('td:eq(15)').text()
    AddVatPer[row]  = $(tr).find('td:eq(16)').text()
    AddVatAmt[row]  = $(tr).find('td:eq(17)').text()
    NetAmt[row]  = $(tr).find('td:eq(18)').text()
    TableData.shift();
    MedicineName.shift();
    BatchNo.shift();
    ExpiryDate.shift();
    Qty.shift();
    FreeQty.shift();
    PurRate.shift();
    Mrp.shift();
    SellRate.shift();
    Unit.shift();
    Amount.shift();
    DiscPer.shift();
    DiscAmt.shift();
    GrossAmt.shift();
    VatPer.shift();
    VatAmt.shift();
    AddVatPer.shift();
    AddVatAmt.shift();
    NetAmt.shift();
}

Now I want to store it in ViewBag or ViewData or any more efficient way from this 2 to controller.

Note: I dont only have to save value of table there are couple other fields I am storing and all things are inter related so I want to get done this thing at once not like calling some other function first to save this data and other function for other data of the same form

Kirtesh
  • 117
  • 3
  • 15
  • Your browser has no concept of `ViewBag` or `ViewData` classes (which are c# code). What are you actually trying to do. –  May 14 '15 at 06:55
  • I am currently doing it on vb with mvc. You mean javascript dont support viewbag view data as it is client site and viewbag view data on server side – Kirtesh May 14 '15 at 07:06
  • Exactly., But what are you actually doing. You accessing the `.text()` value of table cells so its not even something which is edited, meaning your controller sent it to the view, so why are your sending it back unchanged? And in a format which cant possibly be bound to a model! –  May 14 '15 at 07:09
  • It looks like you could really benefit from a client side framework such as Angular and knockout. Management of those javascript variables is going to get **very** messy, especially when you're wanting to submit changes to the server. – Mathew Thompson May 14 '15 at 07:17
  • No its not the controller set it to the view. i am filling those table using popup menu by javascript. Any other way that I can pass it to the controller this array – Kirtesh May 14 '15 at 07:27
  • I assume you mean by using a pop-up form, in which case why are you not posting the form (via ajax) and saving the data as you create each row? –  May 14 '15 at 07:30
  • No its not the way it will be too hard to handle during edit the user might delete some rows he can edit some rows so changing the arrays value everytime i guess its not a good thing. so i am trying to get this as array during save click – Kirtesh May 14 '15 at 07:36
  • So anyway of doing java script array to controller will help me a lot – Kirtesh May 14 '15 at 07:37
  • @Snehal, I mean as you edit each row, you use ajax to call a controller method which saves the form data, and if successful you then update the table values (ditto for adding or deleting a row). –  May 14 '15 at 07:48
  • I got this right I tried this but there are too much data which will make it slow so if any efficient way to do this and + thing is that there is not only the table i want to save there are many other things its like a invoice bill and it will make a problem with id binding if i do it like that so i want a solution to store value some where and then save it on http post method of controller – Kirtesh May 14 '15 at 08:22
  • @Snehal, I think you need to do some research. What I am suggesting is far faster. But if your talking about dynamically creating objects that will bind to a collection in a single post, [this answer](http://stackoverflow.com/questions/29837547/set-class-validation-for-dynamic-textbox-in-a-table/29838689#29838689) may give you some hints. I also suggest your read the help files and learn how to send a message to a user (see how this message starts) –  May 14 '15 at 08:50

0 Answers0