C# 无法将Person类型的对象强制转换为PersonViewModel类型
我正在尝试向服务器发送数据的表单数组,但绑定不正确C# 无法将Person类型的对象强制转换为PersonViewModel类型,c#,jquery,ajax,serialization,asp.net-mvc-5,C#,Jquery,Ajax,Serialization,Asp.net Mvc 5,我正在尝试向服务器发送数据的表单数组,但绑定不正确 public class PersonViewModel { public List<Person> Persons {get; set} } public class Person{ public string FirstName {get; set;} } // view model that wil render all the partial view models showing all the
public class PersonViewModel
{
public List<Person> Persons {get; set}
}
public class Person{
public string FirstName {get; set;}
}
// view model that wil render all the partial view models showing all the people
@using (Html.BeginForm("Action", "Controller", FormMethod.Post, new { id="people-form" autocomplete = "off" }))
{
<div class="container">
@for (int i = 0; i < Model.Persons.Count; i++)
{
<div>
<label>
@Html.TextBoxFor(x => x.Persons[i].FirstName)
</label>
</div>
}
</div>
}
// ajax used to post, trying to serialize it as an array but still when it hits my action it is not binding.
return $.ajax({
method: 'POST',
url: 'SavePeople',
data: $('#people-form').serializeArray(),
}).done(function (response) {
}).fail(function (err) {
});
[HttpPost]
public JsonResult SavePeople(PersonViewModel vm /* this comes in as null */)
{
}
公共类PersonViewModel
{
公共列表人员{get;set}
}
公共阶层人士{
公共字符串名{get;set;}
}
//视图模型,它将渲染显示所有人的所有局部视图模型
@使用(Html.BeginForm(“Action”、“Controller”、FormMethod.Post、new{id=“people form”autocomplete=“off”}))
{
@对于(int i=0;ix.Persons[i].FirstName)
}
}
//ajax过去常常发布,试图将其序列化为数组,但当它遇到我的操作时,它仍然没有绑定。
返回$.ajax({
方法:“POST”,
url:“SavePeople”,
数据:$(“#人员表单”).serializeArray(),
}).完成(功能(响应){
}).失败(功能(错误){
});
[HttpPost]
public JsonResult SavePeople(PersonViewModel vm/*该值为null*/)
{
}
发送的数据看起来像Persons[0]。FirstName:41441,但出于某种原因,它正试图将其直接绑定到PersonViewModel中,而不是将其添加到Persons集合中。它似乎遇到了在中识别的相同错误 MVC中有一个错误,他们拒绝修复,如果collection属性名称以类型名称开头,则它不会绑定 尝试更改:
public class PersonViewModel
{
public List<Person> Persons {get; set}
}
公共类PersonViewModel
{
公共列表人员{get;set}
}
致:
公共类PersonViewModel
{
公共列表人员{get;set}
}
试试这个;如果您仍然有问题,请在下面发布,我将提供帮助。这看起来像是在中发现的相同错误 MVC中有一个错误,他们拒绝修复,如果collection属性名称以类型名称开头,则它不会绑定 尝试更改:
public class PersonViewModel
{
public List<Person> Persons {get; set}
}
公共类PersonViewModel
{
公共列表人员{get;set}
}
致:
公共类PersonViewModel
{
公共列表人员{get;set}
}
试试这个;如果您在下面的帖子中仍然遇到问题,我会提供帮助。首先,请确保您的ajax正常工作
return $.ajax({
contentType: 'application/json; charset=utf-8',
url: DeploymentPath + '/ControllerName/ActionName',
type: "POST",
data: JSON.stringify({ "parameterName": _input})
});
项目的变量引用
- DeploymentPath:指向项目的url
- ControllerName:SavePeople()函数的控制器名称
- 操作名称:SavePeople
- 参数名称:vm
- _输入:您的个人对象
一旦您的ajax函数能够执行,您就可以使用
vm.Persons
-这是一个列表,直接访问SavePeople操作中的值。首先,请确保您的ajax工作正常
return $.ajax({
contentType: 'application/json; charset=utf-8',
url: DeploymentPath + '/ControllerName/ActionName',
type: "POST",
data: JSON.stringify({ "parameterName": _input})
});
项目的变量引用
- DeploymentPath:指向项目的url
- ControllerName:SavePeople()函数的控制器名称
- 操作名称:SavePeople
- 参数名称:vm
- _输入:您的个人对象
一旦ajax函数能够执行,您就可以使用
vm.Persons
-这是一个列表,直接访问SavePeople操作中的值。假设您没有自定义序列化设置,并且模型是@model HomeController.PersonViewModel
。下面的表达是有用的
@Html.TextBoxFor(x => @Model.Persons[i].FirstName)
如果没有任何效果,只需手动使用IFormCollection和解析值,或在中调试特定问题
假设您没有自定义序列化设置,并且模型为
@model HomeController.PersonViewModel
。下面的表达是有用的
@Html.TextBoxFor(x => @Model.Persons[i].FirstName)
如果没有任何效果,只需手动使用IFormCollection和解析值,或在中调试特定问题
顺便说一句,您应该发送类似{persons:[firstName:'test']}的内容。没有发送json。实际上,我正在序列化生成persons%5B0%5D的整个表单。firstName=55353如果这不在集合中,它将绑定,但作为集合会导致问题。顺便说一句,您应该发送类似{persons:[firstName:'test']}的内容实际上没有发送json,我正在序列化整个表单,该表单生成Persons%5B0%5D。FirstName=55353如果这不在集合中,它将绑定,但作为集合会导致问题。嘿,它似乎仍然不起作用。我更改了代码以反映您的更改,但在绑定中直到null。现在发送的数据看起来像Person[0]。FirstNames听起来像您还需要在视图-模型中进行更改。Person而不是Model.Person。嘿,它似乎仍然不起作用。我更改了代码以反映您的更改,但在绑定中直到null。现在发送的数据看起来像Persons[0]。FirstNames听起来像您还需要在视图-Model.People中更改,而不是Model.Persons