为什么是';这个javascript对象不能转换成C#类对象吗?
我正在开发一个小型测试台应用程序,你给这个应用程序一个音符名称、符号和八度音,它会给出音符应该发出的频率 我对通过AJAX发送到服务器的JavaScript对象有问题。但是,它根本不会映射到操作上的类 发送方代码是一段JavaScript:为什么是';这个javascript对象不能转换成C#类对象吗?,c#,javascript,jquery,ajax,asp.net-mvc-4,C#,Javascript,Jquery,Ajax,Asp.net Mvc 4,我正在开发一个小型测试台应用程序,你给这个应用程序一个音符名称、符号和八度音,它会给出音符应该发出的频率 我对通过AJAX发送到服务器的JavaScript对象有问题。但是,它根本不会映射到操作上的类 发送方代码是一段JavaScript: $someContainer.on('click', '.raise-pitch', function (e) { e.preventDefault(); var $line = $(this).parents('.item-line'),
$someContainer.on('click', '.raise-pitch', function (e) {
e.preventDefault();
var $line = $(this).parents('.item-line'),
// Cache GUI elements for this line. Not pertinent.
var data = {
'model': {
'NoteName': $noteName.val(),
'Sign': $noteSign.val(),
'Octave': $noteOctave.val()
},
'steps': 1
};
var dataString = JSON.stringify(data);
$.ajax({
type: 'POST',
url: '@Url.Action("AlterPitch", "Home")',
data: dataString,
async: true,
dataType: 'json',
success: function (result) {
// Unimportant things.
}
});
});
在数据
对象中,您可以看到我有两个东西:模型
和步骤
。这些步骤决定了我们改变音符的程度。我已经验证了注释名称
、符号
和倍频程
的值是否进入数据对象。步骤是预先确定的
但是,data
对象映射到C#侧的ViewModel,如下所示:
public class NoteViewModel
{
public enum NoteSign
{
Natural,
Sharp,
Flat
}
public string NoteName { get; set; }
public NoteSign Sign { get; set; }
public int Octave { get; set; }
public float Frequency { get; set; }
private static readonly Dictionary<string, float> FrequencyLookup = new Dictionary<string, float>
{
{"C", 16.35f},
{"C#", 17.32f},
{"D", 18.35f},
{"D#", 19.45f},
{"E", 20.60f},
{"F", 21.83f},
{"F#", 23.12f},
{"G", 24.50f},
{"G#", 25.96f},
{"A", 27.50f},
{"A#", 29.14f},
{"B", 30.87f}
};
// Methods...not important for reasons below.
}
…或者至少应该这样。我在控制器方法中设置了一个断点:
[HttpPost]
public ActionResult AlterPitch(AlterPitchViewModel model)
{
return Json(model.AlterPitch(), JsonRequestBehavior.AllowGet);
}
…但是模型为null,这会导致NullReference
异常
模型没有序列化到在此上下文中发送的对象。显然,我仍然在做错事问题是…什么?您的HttpPost方法必须有一个唯一的参数,即您的ViewModel。我的建议:根据post WebMethod创建一个viewmodel
因此,这里:
public class AlterPitchViewModel
{
public NoteViewModel note { get; set; };
public int Steps { get; set; };
}
并相应地调整Javascript。无需将javascript模型实例命名为ASP方法中的参数->一个对象到达并且只有一个参数可用,因此没有歧义。这种方法对我很有效
服务器端:
function CallTestingViewModel() {
// We create the javascript object based on the
// definition of our C# ViewModel class.
var udata = {
Id: 1,
Description: 'This is a test.'
};
// Calling the C# method via AJAX.
$.when(GenericAjaxCall('Home/TestingViewModel', false, udata)).then(function (result) {
// do stuff
});
}
// Return the AJAX call as a Promise Object
function GenericAjaxCall(controllerUrl, async, clientData) {
return $.ajax({
type: 'POST',
async: async,
url: controllerUrl,
data: JSON.stringify(clientData),
contentType: 'application/json;',
dataType: 'json'
});
}
首先,创建ViewModel
public class TestViewModel
{
public string Id { get; set; }
public string Description { get; set; }
}
然后,在控制器中:
[HttpPost]
public ActionResult TestingViewModel(TestViewModel udata)
{
// Stuff.
}
客户端:
function CallTestingViewModel() {
// We create the javascript object based on the
// definition of our C# ViewModel class.
var udata = {
Id: 1,
Description: 'This is a test.'
};
// Calling the C# method via AJAX.
$.when(GenericAjaxCall('Home/TestingViewModel', false, udata)).then(function (result) {
// do stuff
});
}
// Return the AJAX call as a Promise Object
function GenericAjaxCall(controllerUrl, async, clientData) {
return $.ajax({
type: 'POST',
async: async,
url: controllerUrl,
data: JSON.stringify(clientData),
contentType: 'application/json;',
dataType: 'json'
});
}
希望有帮助。方法顶部缺少[HttpPost]
属性?@Askolein-这很有帮助。尽管如此,请求的“模型”部分还是很奇怪,导致了404。您是否尝试过对对象进行字符串化并将请求内容类型设置为json?我查找了字符串化并添加了它。更新了我的代码,但我回到了最初的问题,即一个空模型进入了我的控制器。虽然我同意这是一个很好的架构决策,但我仍然有HTTP 404问题,这并没有解决。但是,这是一个很好的建议,谢谢!404? 这是一个路由问题。不是因为你的帖子问题。我确实尝试过这种模式,但是我仍然在把东西送到服务器上时遇到问题。我正在尝试更多地使用这个模式,因为Promise模式看起来更具可读性和组织性—感谢您向我展示这个!你得到的确切错误是什么?您能否提供有关Chrome在开发工具中提供的标题和响应的信息?我优化了我的对象,但没有使用嵌套对象,我的所有问题都消失了(并且,使用您上面提供的promise对象配置帮助简化了我的代码)。可能出于某种原因,我无法将嵌套对象发送到服务器。