使用不引人注目的JavaScript,参数不为null,但其值为

当我的JavaScript与我的cshtml文件位于同一页面时,这是有效的:

function SaveEntity() { // more code here alert(entity.FirstName); // this shows that entity's properties have values $.post('/Home/Save', { entity: entity }, SaveComplete); } 

该代码现在位于单独的Index.js文件中。 在我的控制器中,entity参数不是null,而是它的所有值。 为什么会发生这种情况?

控制器签名:

 public ActionResult Save(Entity entity) { // method here } 

在此处输入图像描述

编辑 – Fiddler截图

在此处输入图像描述

在此处输入图像描述

编辑

这是由Entity Framework生成的Entity类:

 public partial class Entity { public Entity() { this.Contacts = new HashSet(); } public int EntityId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Address { get; set; } public string City { get; set; } public string State { get; set; } public string ZipCode { get; set; } public string Company { get; set; } public string LastModifiedUser { get; set; } public Nullable LastModifiedTime { get; set; } public string Notes { get; set; } public virtual ICollection Contacts { get; set; } } 

编辑

完整的Index.js,使用完整的SaveEntity()方法:

 var _currentEntities; $(function () { // Hide the ID column (column 1) $('#tableContacts').dataTable({ "aoColumnDefs": [ { "bSearchable": false, "bVisible": false, "aTargets": [0] } ], "bLengthChange": false, // don't show the number of records to show per page "bFilter": false, // don't show the search/filter box "iDisplayLength": 5 }); $('#waitImage').hide(); // Show the letters of the alphabet, with a link to call GetEntries() for each of them. for (var i = 65; i <= 90; i++) { $('#headerAlphabet').append('' + String.fromCharCode(i) + ' '); } }); function GetEntries(firstLetter) { // code here } function EntriesReceived(entities) { // code here } function DisplayPerson(entityId) { // code here } function SaveEntity() { // Grab our entity var entity; for (var i = 0; i < _currentEntities.length; i++) { if (_currentEntities[i].EntityId == $('#txtEntityId').val()) { entity = _currentEntities[i]; break; } } entity.FirstName = $('#txtFirstName').val(); entity.LastName = $('#txtLastName').val(); entity.Address = $('#txtAddress').val(); entity.City = $('#txtCity').val(); entity.State = $('#txtState').val(); entity.ZipCode = $('#txtZipCode').val(); entity.Company = $('#txtCompany').val(); entity.Notes = $('#txtNotes').val(); alert(entity.FirstName); $.post('/Home/Save', { entity: entity }, SaveComplete); } function SaveComplete(saveStatus) { alert(saveStatus); } function CancelChanges() { alert('Cancel will be done here.'); } 

首先,在进行任何类型的ajax提交时,你想要查看Http调试器中的实际请求,比如Fiddler。 你想看看它是否真的发布你认为它的值。 这将帮助您确定问题是在您的javascript中还是在服务器上。 现在,你不知道。

请参阅: 此和此

其次,您应该发布您的服务器端实体定义。

编辑:

这似乎是jQuery在默认情况下序列化对象的方式的一个问题,它使用括号表示法,它不能与默认的模型绑定器一起使用。 你必须使用Dave提到的传统:true参数。

更多关于这里 。 以下将解决您的问题。 当与true参数一起使用时, $.param方法将强制传统的序列化。

 $.post('/Home/Save', $.param(entity, true), SaveComplete);. 

我犹豫,b / c我的答案没有解决为什么这可以从View查看从js文件失败。 但我会使用ajax vs post调用来设置json和传统:

 $.ajax({ type: 'POST', url: '/Home/Save', dataType: 'json', traditional: true, data: entity , //$.toJSON(data), success: function (data) { SaveComplete(data); } });