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控制檯對象
發送一個對象,但接收作爲對象列表? –
請建議ho做正確的,我有什麼 - 輸入和複選框列表 - 這是「對象」列表。如何正確發送和接收? – Fullbalanced
'userGoals'是一個JS對象。然後它包含一個屬性「MandatoryGoalID」。該屬性包含一個數組,列出來自各種ID輸入的所有ID。其他屬性也一樣。你的JS對象的結構都是錯誤的。嘗試運行'console.log(JSON.stringify(userGoals));'查看結構,你會看到它不符合你的需求。您正在向控制器發送一個格式錯誤的對象而不是對象列表。我認爲如果你只是一次性將整個表格序列化,那麼你會有更好的運氣 – ADyson