mardi 5 mai 2015

MS MVC 4(VB) -batch post to ajax with json string

I'm another newbie to Microsoft MVC. I'm also using VB.NET. I have a CRUD app when I'm trying to write a batch update. Meaning loop though my HTML Table, find out if modified and POST to my MYSQL table.

I get two errors 1.) db.Entry(model).State = EntityState.Modified in my controller says "entitiy type List'1 not in my model" 2.) if I change one of my table fields to "EditorFor" because I want to edit that field my code doesn't pick up my changes only what was initially loaded to the table.

Any pointers would be appreciated

My MODEL

Namespace Models
Public Class Tickets
    <Display(name:="id")> Public Property id As Nullable(Of Integer)
    <Display(name:="status")> Public Property status As Nullable(Of Integer)
    <Display(name:="bug id")> Public Property bug_text_id As Nullable(Of     Integer)
    <Display(name:="build")> Public Property build As String
    <Display(name:="Summary")> Public Property summary As String
    <Display(name:="priority")> Public Property priority As Nullable(Of Integer)
End Class



My CONTROLLER VB
<HttpPost> _
Public Function Update(model As List(Of EFMySQLCrud10.Models.Tickets)) As ActionResult
    '
    Dim pta_Value = New Models.Tickets
    For Each item In model
        If item.id = item.id Then
            pta_Value = item
            If ModelState.IsValid Then
                db.Entry(model).State = EntityState.Modified
                db.SaveChanges()
            End If

            'Exit For
        End If
    Next

    'Return View(pta_Value)
    Return RedirectToAction("Index")
End Function

MY JS which is in my VIEW for now JS in my VIEW window.gbDataURL = '@Url.Action("Update", "PTA")';

    function batch_Save() {


            var table = document.getElementById('results');


       //setup model array
            model = [];


            ////
            //loop view table for values
            for (var i = 1; i < table.rows.length; i++) {
                var str_priority_id = (table.rows[i].cells[0].innerHTML);
                var str_priority = (table.rows[i].cells[1].innerHTML);
                var str_status = (table.rows[i].cells[2].innerHTML);
                var str_bugId = (table.rows[i].cells[3].innerHTML);
                var str_build = (table.rows[i].cells[4].innerHTML);


                //build jSON model
                var item = { id: str_priority_id, priority: str_priority };
                model.push(item);
            }


            $.ajax({
                url: gbDataURL,
                data: JSON.stringify(model),
                type: 'POST',
                contentType: 'application/json; charset=utf-8',
                success: function (data) {
                    //call is successfully completed and we got result in data
                    alert("ajax call was successful")

                },
                error: function (xhr, ajaxOptions, thrownError) {
                    //some errror, some show err msg to user and log the error
                    alert(xhr.responseText);

                }
            });

        }

Aucun commentaire:

Enregistrer un commentaire