【问题标题】:Controller Parameters Empty Sending ViewModel JSON via AJAX控制器参数为空通过 AJAX 发送 ViewModel JSON
【发布时间】:2017-03-03 12:40:00
【问题描述】:

我正在尝试将PropertyDetails ViewModel 发送到控制器功能SaveDetails

我目前使用Newtonsoft.JsonConvert 序列化模型,然后我使用JSON.Stringify 转换为JSONString,然后AJAX 使用该Controller 将数据发送到Controller 但是当我在控制器函数上放置断点时SaveDetails 对象为空(非空),但 ViewModel 内的对象为空。

 var json = @Html.Raw(@JsonConvert.SerializeObject(@Model));
 console.log(JSON.stringify(json));

    $.ajax({
        url: "@Url.Action("SaveDetails")",
        type: "POST",
        data: JSON.stringify(json),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        error: function (response) {
        },
        success: function (response) {
            alert(response);
        }
    });

控制器功能SaveDetails

[HttpPost]
public ActionResult SaveDetails(PropertyDetailsViewModel PropertyDetail)
    {
        if(PropertyDetail != null)
        {

           return Json("Success");

        }
        return Json("Failed");

    }

JSON.Stringfy(@Model) 的 CONSOLE.LOG 使用 JSONLint 来检查它是否是有效的 JSON,它是。

{
	"PropertyID": 2,
	"PropertyDetail": {
		"AccessInformation": "",
		"AddressLine1": "",
		"AddressLine2": "",
		"AddressLine3": "",
		"AddressLine4": "",
		"AddressLine5": "",
		"BranchID": 1,
		"BuildingType": "",
		"CurrentValue": 0,
		"Description": "",
		"Garages": 0,
		"HouseAlarm": false,
		"LandlordID": "",
		"LastUpdatedTime": "2016-11-20T13:48:59.693",
		"LastUpdatedUser": "mimtiaz",
		"Latitude": 0,
		"LeaseEnd": null,
		"LeaseNotes": " ",
		"LeaseStart": null,
		"Longtitude": 0,
		"MarketingDescription": "",
		"NickName": "",
		"Notes": "",
		"ParentID": 0,
		"ParkingSpaces": 0,
		"Postcode": "",
		"PurchaseDate": null,
		"PurchaseNotes": " ",
		"PurchasePrice": 0,
		"PurchaseType": " ",
		"SaleDate": null,
		"SaleNotes": " ",
		"SalePrice": 0,
		"size": "",
		"SmokeAlarms": 0,
		"Status": "Active",
		"TemplateType": "1",
		"ValuationDate": null,
		"ValuationNotes": " ",
		"YearBuilt": 1900
	},
	"PropertyImages": [{
		"DateAdded": "2016-10-01T00:00:00",
		"Description": "",
		"Filename": "1872_MVR103597_IMG_09_0000_max_476x317.JPG",
		"ID": 1,
		"InventoryID": 0,
		"PropertyID": 2,
		"RoomID": 0,
		"Sequence": 1,
		"TenancyID": 0,
		"Title": "1872_MVR103597_IMG_09_0000_max_476x317.JPG",
		"Type": "P"
	}, {
		"DateAdded": "2016-10-01T00:00:00",
		"Description": "",
		"Filename": "5384_26648557_IMG_26_0000_max_476x317.jpg",
		"ID": 2,
		"InventoryID": 0,
		"PropertyID": 2,
		"RoomID": 0,
		"Sequence": 2,
		"TenancyID": 0,
		"Title": "5384_26648557_IMG_26_0000_max_476x317.jpg",
		"Type": "P"
	}, {
		"DateAdded": "2016-10-01T00:00:00",
		"Description": "",
		"Filename": "39050_6028636_IMG_19_0000_max_476x317.jpg",
		"ID": 3,
		"InventoryID": 0,
		"PropertyID": 2,
		"RoomID": 0,
		"Sequence": 3,
		"TenancyID": 0,
		"Title": "39050_6028636_IMG_19_0000_max_476x317.jpg",
		"Type": "P"
	}],
	"InventoryList": [],
	"PropertyPath": "/Insight/Property 2",
	"SideNavigationViewModel": {
		"DynamicEntities": [{
			"ID": 1,
			"EntityType": "O",
			"EntityLabel": "Solicitor",
			"Parent": "Other",
			"MultiplePerCase": true,
			"DynamicOrgID": 1,
			"OrgFKID": 2,
			"OrgID": "GARNER 003",
			"OrgContactID": "PARTIND001",
			"OrgRef": "",
			"DynamicPersonID": 0,
			"PersonFKID": 0,
			"ThirdPartyID": "",
			"PersonRef": "",
			"SectionHeaderID": 0,
			"EntityName": "Solicitor",
			"AccessLevel": "",
			"LetterFolder": ""
		}, {
			"ID": 2,
			"EntityType": "O",
			"EntityLabel": "Insurance",
			"Parent": "Other",
			"MultiplePerCase": false,
			"DynamicOrgID": 0,
			"OrgFKID": 0,
			"OrgID": "",
			"OrgContactID": "",
			"OrgRef": "",
			"DynamicPersonID": 0,
			"PersonFKID": 0,
			"ThirdPartyID": "",
			"PersonRef": "",
			"SectionHeaderID": 0,
			"EntityName": "Insurance",
			"AccessLevel": "",
			"LetterFolder": ""
		}, {
			"ID": 3,
			"EntityType": "O",
			"EntityLabel": "Gas",
			"Parent": "Other",
			"MultiplePerCase": false,
			"DynamicOrgID": 0,
			"OrgFKID": 0,
			"OrgID": "",
			"OrgContactID": "",
			"OrgRef": "",
			"DynamicPersonID": 0,
			"PersonFKID": 0,
			"ThirdPartyID": "",
			"PersonRef": "",
			"SectionHeaderID": 0,
			"EntityName": "Gas",
			"AccessLevel": "",
			"LetterFolder": ""
		}, {
			"ID": 4,
			"EntityType": "O",
			"EntityLabel": "Electric",
			"Parent": "Other",
			"MultiplePerCase": false,
			"DynamicOrgID": 0,
			"OrgFKID": 0,
			"OrgID": "",
			"OrgContactID": "",
			"OrgRef": "",
			"DynamicPersonID": 0,
			"PersonFKID": 0,
			"ThirdPartyID": "",
			"PersonRef": "",
			"SectionHeaderID": 0,
			"EntityName": "Electric",
			"AccessLevel": "",
			"LetterFolder": ""
		}, {
			"ID": 5,
			"EntityType": "O",
			"EntityLabel": "Council",
			"Parent": "Other",
			"MultiplePerCase": false,
			"DynamicOrgID": 0,
			"OrgFKID": 0,
			"OrgID": "",
			"OrgContactID": "",
			"OrgRef": "",
			"DynamicPersonID": 0,
			"PersonFKID": 0,
			"ThirdPartyID": "",
			"PersonRef": "",
			"SectionHeaderID": 0,
			"EntityName": "Council",
			"AccessLevel": "",
			"LetterFolder": ""
		}, {
			"ID": 6,
			"EntityType": "O",
			"EntityLabel": "Water",
			"Parent": "Other",
			"MultiplePerCase": false,
			"DynamicOrgID": 0,
			"OrgFKID": 0,
			"OrgID": "",
			"OrgContactID": "",
			"OrgRef": "",
			"DynamicPersonID": 0,
			"PersonFKID": 0,
			"ThirdPartyID": "",
			"PersonRef": "",
			"SectionHeaderID": 0,
			"EntityName": "Water",
			"AccessLevel": "",
			"LetterFolder": ""
		}, {
			"ID": 7,
			"EntityType": "D",
			"EntityLabel": "Purchase & Sale Details",
			"Parent": "Property",
			"MultiplePerCase": false,
			"DynamicOrgID": 0,
			"OrgFKID": 0,
			"OrgID": "",
			"OrgContactID": "",
			"OrgRef": "",
			"DynamicPersonID": 0,
			"PersonFKID": 0,
			"ThirdPartyID": "",
			"PersonRef": "",
			"SectionHeaderID": 0,
			"EntityName": "PurchDets",
			"AccessLevel": "",
			"LetterFolder": ""
		}],
		"PropertyID": 2,
		"LandlordID": 0
	}
}

PropertyDetailsViewModel

public class PropertyDetailsViewModel
{

    public int PropertyID { get; set; }
    public PropertyDetail PropertyDetail { get; set; }
    public PropertyImages PropertyImages { get; set; }
    public InventoryList InventoryList { get; set; }

    public string PropertyPath { get; set; }

    public SideNavigationViewModel SideNavigationViewModel { get;set;}
}

【问题讨论】:

  • 请包含 PropertyDetailsViewModel
  • 我做了,但有人编辑了它。生病尝试再次添加

标签: c# jquery json ajax model-view-controller


【解决方案1】:

您需要使用 [FromBody] 属性。

让你的 post 方法看起来像这样:

[HttpPost]
public ActionResult SaveDetails([FromBody]PropertyDetailsViewModel PropertyDetail)
{
    if(PropertyDetail != null)
    {

       return Json("Success");

    }
    return Json("Failed");

}

【讨论】:

  • 找不到类型或命名空间。我在顶部使用 System.web.http 包含在内,对吗?
  • 您使用的是哪个 MVC 版本以及哪个框架?是的,命名空间应该是 System.Web.Http
  • 我正在使用 MVC 5,RAZOR
  • 是的,它应该在 System.Web.Http
  • 我可能真的错了。您有可能必须编写自己的表单模型绑定器。检查这个:stackoverflow.com/questions/32012288/…
【解决方案2】:

我做了更多的研究,发现了这种让它工作的方法,不确定它是否是最好的解决方案,但绝不是一个解决方案。它现在有效。

 public ActionResult SaveDetails()
    {

        Request.InputStream.Seek(0, SeekOrigin.Begin);
        string jsonData = new StreamReader(Request.InputStream).ReadToEnd();
        PropertyDetailsViewModel PropertyDetail = JsonConvert.DeserializeObject<PropertyDetailsViewModel>(jsonData);

        if (PropertyDetail != null)
         {

         return Json("Success");

        }
        return Json("Failed");

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-14
    • 2013-05-03
    • 2017-10-31
    • 1970-01-01
    • 2014-04-26
    • 1970-01-01
    相关资源
    最近更新 更多