将更改保存到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!

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.

There are several issues in your code:

1) You're passing JSON string containing viewmodel properties, it is necessary to set contentType: 'application/json; charset=utf-8' option in AJAX callback to ensure model binder recognize it as viewmodel parameter.

2) return View() is not applicable for AJAX response, use return PartialView() instead and put html() to render response in target element.

Therefore, you should use AJAX setup as provided below:

$.ajax({
    type: "POST",
    url: url,
    data: JSON.stringify(list),
    contentType: 'application/json; charset=utf-8',
    success: function (result) {
        $('#targetElement').html(result);
    },
    error: function (xhr, status, err) {
        // error handling
    }
});

Controller Action

[HttpPost]
public ActionResult Process(Hello model) 
{
    if (ModelState.IsValid)
    {
        using (db = new DB())
        {
            // save data
        }
        ViewBag.Message = "Success";
        return PartialView("_PartialViewName", model);
    }
    else
    {
        ViewBag.Message = "Failed";
        return PartialView("_PartialViewName", model);
    }
}

Remember that AJAX callback intended to update certain HTML element without reloading entire view page. If you want to reload the page with submitted results, use normal form submit instead (with Html.BeginForm()).