(jtable, ajax) моя клиентская сторона отправляет все данные в api null и 0 - PullRequest
0 голосов
/ 02 мая 2020

Я пытаюсь создать таблицу crud с jTable , но

все данные, которые я пытаюсь отправить на мой api , должны быть null пытается ли создать или обновить или удалить , как показано Здесь ..

Id = 0

цена = 0

ProductName = null

ProductCode = null

Это мой клиентский код jquery с использованием jtable , как описано в jtable документации

    $('#container').jtable({
        title: 'Table of people',
        actions: {
            listAction:'http://localhost:62881/product/GetAllProducts',
            createAction:'http://localhost:62881/product/AddNewProduct',
            updateAction: 'http://localhost:62881/product/EditProduct',
            deleteAction: "http://localhost:62881/product/DeleteProduct"
        },
        fields: {

            Id: {
                key: true,
                create:false,
                edit:false,
                list: false

            },
            ProductName: {
                title: 'Name',
                width: '40%'
            },
            Price: {
                title: 'Price',
                width: '20%'
            },
            ProductCode: {
                title: 'Product Code',
                width: '30%'
            }

        }


    });
    $('#container').jtable('load');

listAction работает только

А это мой контроллер

[ApiController]
[Route("[controller]/[action]/{id?}/{obj?}")]
public class ProductController : Controller
{
    private readonly IProductRep rep;

    public ProductController(IProductRep rep)
    {
        this.rep = rep;
    }


    [EnableCors("allow")]
    [HttpPost]
    public IActionResult GetAllProducts()
    {
        try
        {
            return Json(new { Result = "OK", Records = rep.GetAllProducts() });
        }
        catch (Exception ex)
        {

            return Json(new { Result = "ERROR", Message = ex.Message });
        }

    }


    [EnableCors("allow")]
    [HttpPost]
    public JsonResult AddNewProduct(Product obj)
    {
        try
        {
            if (!ModelState.IsValid)
            {
                return Json(new { Result = "ERROR", Message = "Form is not valid! Please correct it and try again." });
            }

            Product product = rep.AddNewProduct(obj);
           return Json(new { Result = "OK", Record = product });
        }
        catch (Exception ex)
        {

            return Json(new { Result = "ERROR", Message = ex.Message });
        }

    }


    [EnableCors("allow")]
    [HttpPost]
    public JsonResult GetProductById(int id)
    {
        try
        {
            return Json( new { Result = "OK", Records = rep.GetProductById(id) });
        }
        catch (Exception ex)
        {

            return Json(new { Result = "ERROR", Message = ex.Message });
        }

    }



    [EnableCors("allow")]
    [HttpPost]
    public JsonResult DeleteProduct(int id)
    {
        try
        {
            rep.DeleteProduct(id);
            return Json( new { Result = "OK" });
        }
        catch (Exception ex)
        {

            return Json(new { Result = "ERROR", Message = ex.Message });
        }

    }



    [EnableCors("allow")]
    [HttpPost]
    public JsonResult EditProduct(Product obj)
    {

        try
        {
            return Json( new { Result = "OK", Records = rep.EditProduct(obj) });
        }
        catch (Exception ex)
        {
            return Json(new { Result = "ERROR", Message = ex.Message });
        }


    }

}

Кто-нибудь знает, где ошибка, пожалуйста?

...