weixin_33734785 2018-06-30 00:25 采纳率: 0%
浏览 117

将Json转换为字符串数组

This is my code for my form:

<div class="form-group">
    <label for="text">product name:</label>
    <input id="name" type="text" class="form-control"  placeholder="Enter product name" name="product[0].name">
</div>
<div class="form-group">
    <label for="text">product detail:</label>
    <input id="detail" type="text" class="form-control" placeholder="Enter prodcut detail" name="product[1].detail">
</div>

<button id="btn" type="submit" class="btn btn-default">Submit</button>

This is my javascript

<script>

    $(function postProduct() {
        $('#btn').click(function () {

            var productName = document.getElementById("name").value;
            var productDetail = document.getElementById("detail").value;

            var dimensions = [ productName, productDetail];
            var keys = $.map(dimensions, function (value, key) {
                return key;
            });

            if (productName == '' || productDetail == '') {
                alert("Please Fill All Fields");
            } else {

                $.ajax({
                    type: 'post',
                    url: "api/product/addproduct",
                    data: keys,
                    dataType: 'json',
                    success: function (result) {

                    },
                    error: function (error) {

                    }
                });
            }

        });
    });

</script>

My controller:

[HttpPost]
[Route("api/product/addproduct")]
public IActionResult AddProduct([FromBody] string[] addproduct)
{
    var pProductName= addProduct[0];
    var pProductDetail= addProduct[1];

    Product NewProduct = new Product();
    {
        NewProduct.ProductName= pProductName;
        NewProduct.ProductDetail = pProductDetail;
    }


    _db.Products.Add(NewProduct);
    _db.SaveChanges();

    //create a new route for post method by id
    return CreatedAtRoute(new { id = addproduct}, addproduct);
}

What I'm trying to do is after the user enters the information into the form and hits submit button. The information will be pass to the controller and save the information into the database. The issue that I have is my data in the form is a Json type, however, my controller is an array.

Is it possible to convert the form into an array?

  • 写回答

1条回答 默认 最新

  • weixin_33725722 2018-06-30 00:48
    关注

    I did not check how you are sending the data. So I assume that you are sending the data in the correct JSON format.

    The good news are: if you wrote your code in a proper way, ASP.Net Core will adapt the data from JSON to array automatically for you!

    The first thing to do is to remove that [FromBody] annotation.

    Now please use ViewModel approach to receive the data rather than receiving the data directly as an array. I.e., Create a view model class like this:

    public class MyViewModel
    {
        public string[] AddProduct { get; set; } // I find the name weired
    }
    

    Now adapt the action in the controller:

    public IActionResult AddProduct(MyViewModel viewModel)
    {
        // Now check viewModel.AddProduct, you should find the data in the array.
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划