Javascript 如何在MVC集合中读取序列化数据(通过Jquery POST发送)

Javascript 如何在MVC集合中读取序列化数据(通过Jquery POST发送),javascript,model-view-controller,collections,Javascript,Model View Controller,Collections,我有一个表单和一个提交按钮点击,我序列化我的表单并将其发送给MVC action Jquery代码 var formCollection = $('form').serialize(); var path = $(this).attr('data-content-url'); // check if no more error $.ajax({

我有一个表单和一个提交按钮点击,我序列化我的表单并将其发送给MVC action

Jquery代码

           var formCollection = $('form').serialize();
           var path = $(this).attr('data-content-url');              

           // check if no more error 
               $.ajax({
                   type: 'POST',
                   url: path,
                   cache: false,
                   data: { collection: formCollection },
                   datatype: 'json',
                   success: function (data) {
                       // do stuff
                   }
               });
    [HttpPost]
    public ActionResult MethodName( FormCollection collection )
    {
    }
MVC侧

           var formCollection = $('form').serialize();
           var path = $(this).attr('data-content-url');              

           // check if no more error 
               $.ajax({
                   type: 'POST',
                   url: path,
                   cache: false,
                   data: { collection: formCollection },
                   datatype: 'json',
                   success: function (data) {
                       // do stuff
                   }
               });
    [HttpPost]
    public ActionResult MethodName( FormCollection collection )
    {
    }
我在我的集合变量中得到序列化数据

name=test&id=1 as collection[0]
如何中断此集合[0],以便将此
名称
id
直接分配给类参数,如

Person p = new person { collection["name"], collection["id"] }

非常感谢

您需要使用您的模型进行反序列化

试试这个

var response = JsonConvert.DeserializeObject<YourModel>(collection);

var name = response.name ;
var id  = response.id;
var response=JsonConvert.DeserializeObject(集合);
var name=response.name;
var id=response.id;

使用foreach循环,可以从列表中获取更多记录。如果这样做有效,可以创建一个新的帮助器类,按照您通常的预期在formcollection中转换序列化数据

private FormCollection DeSerialize(FormCollection form)
{
  FormCollection collection = new FormCollection();
  //un-encode, and add spaces back in
  string querystring = Uri.UnescapeDataString(form[0]).Replace("+", " ");
  var split = querystring.Split(new [] {'&'}, StringSplitOptions.RemoveEmptyEntries);
  Dictionary<string, string> items = new Dictionary<string, string>();      
  foreach (string s in split)
  {        
    string text = s.Substring(0, s.IndexOf("="));
    string value = s.Substring(s.IndexOf("=")+1);

    if (items.Keys.Contains(text))
      items[text] = items[text] + "," + value;
    else
     items.Add(text, value);
   }
   foreach (var i in items)
 {
   collection.Add(i.Key, i.Value);
 }            
   return collection;
 }
私有FormCollection反序列化(FormCollection表单)
{
FormCollection集合=新建FormCollection();
//取消编码,并重新添加空格
字符串querystring=Uri.UnescapeDataString(格式[0])。替换(“+”,”);
var split=querystring.split(新[]{'&'},StringSplitOptions.RemoveEmptyEntries);
字典项=新字典();
foreach(拆分中的字符串s)
{        
字符串text=s.Substring(0,s.IndexOf(“=”);
字符串值=s.Substring(s.IndexOf(“=”)+1);
if(items.Keys.Contains(text))
项目[文本]=项目[文本]+“,”+值;
其他的
添加(文本、值);
}
foreach(项目中的var i)
{
集合。添加(i.键,i.值);
}            
回收;
}

我最初没有使用Modal在表单上显示数据。我的页面上只有两个文本框,然后将它们序列化以将数据发送到MVC控制器。我用我的类名替换了针对您的代码,但它出错了
附加信息:解析值e时遇到意外字符。路径“”,第0行,位置0。
我作为集合传递的数据是
“name=test&id=1”
在任何人使用此方法之前,刚刚发现一个错误,如果您的数据中包含
&
作为文本,则此代码无效