Ajax POST 未达到控制器断点 [英] Ajax POST not hitting controller break point

查看:64
本文介绍了Ajax POST 未达到控制器断点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

型号

公共类 AppointmentViewModel{公共列表<DTO.Appointment>约会{得到;放;}公共 DTO.Appointment 预约 { get;放;}公共 int AppointmentId_Input { 获取;放;}公共字符串 AppointmentTitle_Input { get;放;}公共字符串 AppointmentName_Input { get;放;}public DateTime AppointmentStartTime_Input { get;放;}public DateTime AppointmentEndTime_Input { get;放;}public int AppointmentType_Input { 获取;放;}公共列表约会类型{获取;放;}}

控制器:

 public ActionResult AddAppointment(Models.AppointmentViewModel avm){BLL.FTSPManager fm = new BLL.FTSPManager();DTO.Appointment a = new DTO.Appointment(){Id = avm.AppointmentId_Input,信息 = avm.AppointmentTitle_Input,名称 = avm.AppointmentName_Input,StartTime = avm.AppointmentStartTime_Input,EndTime = avm.AppointmentEndTime_Input,type = new DTO.AppointmentType(){Id = avm.AppointmentType_Input}};返回 Json(avm);}

Ajax 请求:

 $('#btnAdd').click(function () {var id、title、name、startTime、endTime、AppointmentTypeid = $('#hdnAppointmentId').val();title = $('#txtTitle').val();name = $('#txtName').val();startTime = $('#txtStartDate').val();endTime = $('#txtEndDate').val();AppointmentType = $('#drptype').val();var JsonData = {AppointmentId_Input: id,AppointmentTitle_Input:标题,AppointmentName_Input:姓名,AppointmentStartTime_Input: startTime,AppointmentEndTime_Input:结束时间,约会类型_输入:约会类型};$.ajax({类型:POST",url: '@Url.Action("AddAppointment", "Admin")',内容类型:应用程序/json;字符集=utf-8",数据:JSON.stringify(JsonData),数据类型:json",成功:功能(数据){返回假;}});})

解决方案

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

<按钮类型=按钮";id="dataSend">DataSend</button>

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({内容类型:假,过程数据:假,类型:'POST',url: '/集合/创建',数据:表单数据,成功:函数(){控制台日志(成功.");},错误:函数(){控制台日志(错误.");},});});

C# 代码(控制器):

[HttpPost]公共异步任务创建(DataForm AllData){return RedirectToAction("Action-method", "Controller");}

C# 代码(模型):

公共类DataForm{公共字符串标题{获取;放;}公共字符串 描述 { get;放;}[EnumDataType(typeof(Topic))]公共话题?主题{得到;放;}公共 IFormFile 图像 { 获取;放;}公共字符串 AdditionalFields { 获取;放;}}

字段AdditionalFields"是一个转换的儿子,在控制器中它反序列化为列表列表".我在等你的问题.

My ajax POST request does'nt seem to be hitting my contoller, although i have used other Ajax post requests in my application this one doesnt seem to be working. My first throught was the datatypes within the model were not strings but other than that i can't seem to figure out why it isnt working.

Model

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; }

}

Controller:

    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 Request:

   $('#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;
                }
            });
        })

解决方案

You can use a FormData function. I can give for you a working example: Html code(button, without other "input" tags:

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

Javascript code:

$("#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# code(controller):

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

C# code(model):

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; }
}

Field "AdditionalFields" is a son converted and in Controller it deserialized to 'List list'. I'm waiting for your questions.

这篇关于Ajax POST 未达到控制器断点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆