2016-12-14 67 views
1

我有模型视图邮政数组作为对象到控制器,ASP.NET MVC

public class GoalView 
{ 
    public int MandatoryGoalID { get; set; } 
    public string MandatoryGoalName { get; set; } 
    public string MandatoryGoalTarget { get; set; } 
} 

我与输入的列表视图:

    @foreach (var item in Model.MandatoryGoals) 
        { 
         <tr> 
          <td> 
           <div class="checkbox"> 
            <label><input type="checkbox" name="MandatoryGoalName[]" value="1">@item.GoalName</label> 
           </div> 
          </td> 
          <td width="40"> 
           <input disabled type="text" name="MandatoryGoalTarget[]" style="width: 40px;" value="@item.GoalTarget" class="form-control input-sm"> 
           <input type="hidden" value="@item.ID" name="MandatoryGoalID[]"> 
          </td> 
         </tr> 
        } 

JS代码

   var userGoals = { 
     MandatoryGoalID : $('input[name="MandatoryGoalID[]"]').serialize(), 
     MandatoryGoalName : $('input[name="MandatoryGoalName[]"]').serialize(), 
     MandatoryGoalTarget : $('input[name="MandatoryGoalTarget[]"]').serialize() 
     }; 

    $.post('/Home/UpdateGoals', $.param(userGoals), function (data) { 
    }); 

控制器

[HttpPost] 
    public ActionResult UpdateGoals(List<GoalView> userGoals) 
    { 


     //return Content(userGoals.First().MandatoryGoalName); 

     return Content(""); 
    } 

控制器接收空对象。这里有什么问题?我需要接收一个对象GoalView数组。

请帮助:)

更新

好吧,我固定对象的名单问题,JS

var mandatoryTargetsValues = new Array(); 
    $('input[name="MandatoryGoalTarget[]"]').each(function() { 
     mandatoryTargetsValues.push($(this).val()); 
    }); 
    var mandatoryNamesValues = new Array(); 
    $('input[name="MandatoryGoalName[]"]').each(function() { 
     mandatoryNamesValues.push($(this).val()); 
    }); 
    var mandatoryIDValues = new Array(); 
    $('input[name="MandatoryGoalID[]"]').each(function() { 
     mandatoryIDValues.push($(this).val()); 
    }); 


    console.log(mandatoryTargetsValues); 
    console.log(mandatoryNamesValues); 
    console.log(mandatoryIDValues); 

    var ListOfMandatoryGoals= Array(); 

    var i; 
    var obj = {}; 
    for (i = 0; i < mandatoryTargetsValues.length; ++i) { 
     obj.MandataryGoalID = mandatoryIDValues[i]; 
     obj.MandatoryGoalName = mandatoryNamesValues[i]; 
     obj.MandatoryGoalTarget = mandatoryTargetsValues[i]; 
     ListOfMandatoryGoals.push(obj); 
    } 

    console.log(ListOfMandatoryGoals); 

    $.post('/Home/UpdateGoals', $.param(ListOfMandatoryGoals), function (data) { 
    }); 

此代码创建阵列中的所有对象 但仍然控制器收到NULL 请帮忙吧

Scree在Web控制台对象

enter image description here

+0

发送一个对象,但接收作为对象列表? –

+0

请建议ho做正确的,我有什么 - 输入和复选框列表 - 这是“对象”列表。如何正确发送和接收? – Fullbalanced

+0

'userGoals'是一个JS对象。然后它包含一个属性“MandatoryGoalID”。该属性包含一个数组,列出来自各种ID输入的所有ID。其他属性也一样。你的JS对象的结构都是错误的。尝试运行'console.log(JSON.stringify(userGoals));'查看结构,你会看到它不符合你的需求。您正在向控制器发送一个格式错误的对象而不是对象列表。我认为如果你只是一次性将整个表格序列化,那么你会有更好的运气 – ADyson

回答

0

问题是固定的nshot,

正确的AJAX请求 - 对象

 $.ajax({ 
     url: '/Home/UpdateGoals', 
     type: 'POST', 
     contentType: 'application/json', 
     data: JSON.stringify(ListOfMandatoryGoals) 
    }); 

名单是在更新开始后

感谢

相关问题