【问题标题】:Ajax POST not hitting controller break pointAjax POST 没有达到控制器断点
【发布时间】:2021-01-13 23:49:48
【问题描述】:

我的 ajax POST 请求似乎没有击中我的控制器,尽管我在我的应用程序中使用了其他 Ajax 发布请求,但这个请求似乎不起作用。我的第一个想法是模型中的数据类型不是字符串,但除此之外我似乎无法弄清楚它为什么不起作用。

型号

public class AppointmentViewModel
{
    public List<DTO.Appointment> appointments { get; set; }

    public DTO.Appointment appointment { get; set; }

    public int AppointmentId_Input { get; set; }

    public string AppointmentTitle_Input { get; set; }

    public string AppointmentName_Input { get; set; }

    public DateTime AppointmentStartTime_Input { get; set; }

    public DateTime AppointmentEndTime_Input { get; set; }

    public int AppointmentType_Input { get; set; }

    public List<DTO.AppointmentType> appointmentTypes { get; set; }

}

控制器:

    public ActionResult AddAppointment(Models.AppointmentViewModel avm)
    {
        BLL.FTSPManager fm = new BLL.FTSPManager();

        DTO.Appointment a = new DTO.Appointment()
        {
            Id = avm.AppointmentId_Input,
            Info = avm.AppointmentTitle_Input,
            Name = avm.AppointmentName_Input,
            StartTime = avm.AppointmentStartTime_Input,
            EndTime = avm.AppointmentEndTime_Input,
            type = new DTO.AppointmentType()
            {
                Id = avm.AppointmentType_Input
            }
        };

        

        return Json(avm);
    }

Ajax 请求:

   $('#btnAdd').click(function () {

            var id, title, name, startTime, endTime, AppointmentType

            id = $('#hdnAppointmentId').val();
            title = $('#txtTitle').val();
            name = $('#txtName').val();
            startTime = $('#txtStartDate').val();
            endTime = $('#txtEndDate').val();
            AppointmentType = $('#drptype').val();

            var JsonData = {
                AppointmentId_Input: id,
                AppointmentTitle_Input: title,
                AppointmentName_Input: name,
                AppointmentStartTime_Input: startTime,
                AppointmentEndTime_Input: endTime,
                AppointmentType_Input: AppointmentType
            };

            $.ajax({
                type: "POST",
                url: '@Url.Action("AddAppointment", "Admin")',
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(JsonData),
                dataType: "json",
                success: function (data) {

                return false;
                }
            });
        })

【问题讨论】:

    标签: c# ajax model-view-controller razor


    【解决方案1】:

    您可以使用 FormData 函数。我可以给你一个工作的例子: Html代码(按钮,没有其他“输入”标签:

    <div class="form-group">
        <button type="button" id="dataSend">DataSend</button>
    </div>
    

    Javascript 代码:

    $("#dataSend").on('click', function () {
    var formData = new FormData();
    formData.append('Image', $('#file')[0].files[0]);
    formData.append('Title', document.getElementById('Title').value);
    formData.append('Description', document.getElementById('Description').value);
    formData.append('Topic', document.getElementById('Topic').value);
    formData.append('AdditionalFields', JSON.stringify(obsFields));
    
    $.ajax({
        contentType: false,
        processData: false,
        type: 'POST',
        url: '/Collection/Create',
        data: formData,
        success: function () {
            console.log("success.");
        },
        error: function () {
            console.log("error.");
        },
    });});
    

    C#代码(控制器):

    [HttpPost]
        public async Task<IActionResult> Create(DataForm AllData)
        {   
            return RedirectToAction("Action-method", "Controller");
        }
    

    C#代码(模型):

    public class DataForm
    {
        public string Title { get; set; }
        public string Description { get; set; }
        [EnumDataType(typeof(Topic))]
        public Topic? Topic { get; set; }
        public IFormFile Image { get; set; }
        public string AdditionalFields { get; set; }
    }
    

    字段“AdditionalFields”是一个子转换并在控制器中反序列化为“列表列表”。 我在等你的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-19
      • 1970-01-01
      • 1970-01-01
      • 2019-04-06
      • 2020-08-11
      • 2017-06-13
      • 1970-01-01
      相关资源
      最近更新 更多