【发布时间】:2017-04-11 17:51:08
【问题描述】:
我有这个 JQuery 函数:
function EditContactPost()
{
var urlEditPost = $("#hdnInfo").data("url_add_contact");
var test = $("#formEditContact").serialize();
alert(test);
$.ajax({
type: 'post',
url: urlEditPost,
data:{ marketingContactVM: test },
})
}
如您所见,我正在发出警报,并且我正在观察测试变量的表单已正确序列化所有值。但是,当控制器操作在服务器端收到此请求时,marketingContactVM 始终为空。为什么会这样?
我的控制器操作如下所示:
[HttpPost]
public ActionResult AddContact(MarketingVM marketingContactVM) //always null
{
some code...
}
以防万一这是我的模型类,它包含另一个列表:
public class MarketingVM
{
public IEnumerable<string> States { get; set; }
public List<MarketingVM> MarketingList { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
public string Company { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public string Country { get; set; }
public string Phone { get; set; }
public string Fax { get; set; }
public string Mail{ get; set; }
public string URL { get; set; }
public bool IsTrue{ get; set; }
public int OtherId{ get; set; }
}
我检查了 DevTools 中的 Network 选项卡,这是 Request URL:http://localhost:56602/ControllerName/AddContact ,应该没问题。
但数据是以FormData 的形式发送的,如下所示:
marketingContactVM:Company=Microsoft+Company&Email=user10%40email.co&First=
Auto&Last=Mapper&Phone=98797988997&Fax=&URL=ww.auto.org&Adress=automapper+
street&City=auto+city&State=FL&Zip=33654&Country=USA&MarketingContacts%5B0%5D
.Name=1%2F03%2F2012+1%3A15+PM&MarketingContacts%5B0%5D.IsSelected=false
.. 就这样。也许是发送数据的方式..我认为正确的格式应该是:
marketingContactVM:
Company=Microsoft Company Email=user10@email.co First=Charles Last=Johnston Phone=98797988997 Fax=989898 URL=ww.auto.org Adress=North streetCity=Sacramento State=FL Zip=33654 Country=USA MarketingContacts.Name=Piotr
MarketingContacts.IsSelected=false
【问题讨论】:
标签: javascript jquery ajax asp.net-mvc