简体   繁体   中英

Constant 400 response, sending complex javascript object to MVC controller

Yet again another MVC issue. One day I'll look at my "MVC learning days" and smile.

So to the point and exactly as the title indicates; A constant 400 response, I've read numerous posts on SO and still cannot seem to get my logic working. If I may show you the logic, someone maybe able to help me.

Controller:

public JsonResult CreateExtendedProperty(ExtendedPropertyDefinitionViewModel extendedPropertyDefinitionViewModel)
{

   //I originally had JsonResult as ActionResult, still no change.
    var p = extendedPropertyDefinitionViewModel;

    //Temp
    //return Json(new { Success = false, ErrorMessage = "Error creating property" });
}

The Ajax/Javascript:

        var extendedPropertyDefinition = JSON.stringify({
            DefinitionId: '0',
            Title: propertyInfo["Title"],
            OrganisationId: '0',
            Organisation: '',
            TypeId: propertyInfo["TypeId"],
            SortOrder: 0,
            IsEnumerated: propertyInfo["IsEnumerated"],
            AllowMultiSelect: propertyInfo["AllowMultiSelect"],
            IsDate: propertyInfo["IsDate"],
            LastUpdatedBy: "",
            LastUpdatedDateTime: new Date().toISOString(),
            CreatedBy: "",
            CreatedByDateTime: new Date().toISOString(),
            Options: {
                OptionId: '0',
                Option: '',
                OptionValue: '',
                SortOrder: 0,
                LastUpdatedBy: '',
                LastUpdatedDateTime: new Date().toISOString(),
                CreatedBy: '',
                CreatedByDateTime: new Date().toISOString(),
            }
        });

        $.ajax({
            url: "<%= Url.Action('CreateExtendedProperty', 'Organisation') %>",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            //traditional: true,
            type: 'POST',
            data: extendedPropertyDefinition,
            success: function(e) {
                alert('success');
                //Rebuild Grid? 
            },
            error: function(e) {
                alert('request failed (500)');

            }
        });

The payload: 在此处输入图片说明

Hopefully someone can put on the right path, all will be much appreciated.

Regards,

So to just get the answer from comments - you have to ensure that <%= Url.Action('CreateExtendedProperty', 'Organisation') %> gets compiled. If it is in a separate JS file you have to pass it from your view. Otherwise it could be some other problem(if you are using Razor syntax, it should something like @Url.Action("CreateExtendedProperty", "Organisation") )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM