weixin_33711647 2018-11-26 22:10 采纳率: 0%
浏览 12

将更改保存到DB MVC

I have a problem when I try to save some data to the database. I can see the ID and Date returning me appropriate values in the JS function... However, the parameter for the Process function inside the controller class remains null. I don't know why is that happening. There is a linq query that is also included in the Hello Model, but I didn't include it because there is no need for it.

Model:

 public class Hello
    {
        List<string> Ids { get; set; }

        List<string> Dates { get; set; }
    }

Controller:

   [HttpPost]
    public ActionResult Process(string ids, string dates) 
    {
        Hello model = new Hello();
        if (ModelState.IsValid)
        {

            using (db = new DB())
            {

                rp = new RequestProcess();
                //var c = rp.getHello(model, dates);
                var c = rp.getStuff();

                if (c != null)
                {
                    foreach (var i in c)
                    {
                        if (i != null)
                        {
                            ids = i.ID;
                            dates = i.Date.ToString();
                        }
                        db.SaveChanges();
                    }

                }

            }
            ViewBag.Message = "Success";
            return View(model);
        }
        else
        {
            ViewBag.Message = "Failed";
            return View(model);
        }
    }

View:

  <td><input class="id" type="checkbox" id=@item.ID /></td>
  <td>@Html.DisplayFor(x => @item.ID)</td>
  <td><input class="date" id=date@item.ID type="text" value='@item.Date'/></td>

  $(document).ready(function () {
        var ids = "";
        var dates = "";
        $("#btnSubmit").bind("click", function () {
            createUpdateArrays();
            var url = "/Sample/Process";
            $.ajax({
                type: "POST",
                url: url,
                data: { ids: ids, dates: dates },
                contentType: 'application/json; charset=utf-8',
                success: function (success) {
                    if (success === true) {
                        alert("HERE WE ARE");
                    }
                    else {
                        alert("eror");
                    }
                }
            });
            ids = "";
            dates = "";
        });
        function createUpdateArrays() {
            var i = 0;
            $('input.remedy-id:checkbox').each(function () {
                if ($(this).is(':checked')) {
                    var rid = $(this).attr("id");
                    $('.planned-date').each(function () {
                        var did = $(this).attr("id");
                        if (did === rid) {
                            var date = $(this).val();
                            ids += rid + ",";
                            dates += date + ",";
                        }
                    });
                };
            });
        };

Any help would be appreciated!

  • 写回答

2条回答 默认 最新

  • weixin_33681778 2018-11-26 22:18
    关注

    I think you need contentType: 'application/json' in your $.ajax({});

    $.ajax({
        type: "POST",
        url: url,
        data: JSON.stringify(list),
        contentType: 'application/json'
    });
    

    Also, try adding [FromBody]Hello model in your controller action.

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?