从 MVC3 控制器使用以下操作:
[HttpPost]
public ActionResult GetAsiguratiSuplimentari(int numarAsiguratiSuplimentari, AcpComplexAsigurat[] oldData)
{
var newData = new List<AcpComplexAsigurat>(oldData);
for(int i = newData.Count; i < numarAsiguratiSuplimentari; i++)
{
newData.Add(new AcpComplexAsigurat());
}
return PartialView("AsiguratiSuplimentari", newData);
}
和js:
$("#NumarAsiguratiSuplimentari").change(function (e) {
var data = { oldData: $("#div-asigurati-suplimentari").find('input').serialize(), numarAsiguratiSuplimentari: $("#NumarAsiguratiSuplimentari").val() };
var postData = JSON.stringify(data);
$.post('@Url.Action("GetAsiguratiSuplimentari")', postData, function (data) {
$("#div-asigurati-suplimentari").html(data);
});
});
我想获取用户选择的选择选项并序列化输入表以发送到上述操作:
<h3>Asigurati</h3>
<div class="field half odd">
<label>Numar asigurati suplimentari</label>
<select id="NumarAsiguratiSuplimentari" name="NumarAsiguratiSuplimentari">
@for (int i = 0; i <= 30; i++)
{
<option value="@i" @(Model.Asigurati.Count == i ? "selected" : "")>@i</option>
}
</select>
</div>
<div class="field full" id="div-asigurati-suplimentari">
@if (Model.Asigurati.Count > 0)
{
<table id="Asigurati">
<thead>
<tr>
<th>CNP</th>
<th>Nume</th>
<th>Prenume</th>
<th>Data nasterii</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.Asigurati)
{
<tr>
<td style="padding: 3px;">@Html.TextBoxFor(x => Model.Asigurati[Model.Asigurati.IndexOf(item)].Cnp, new { maxlength = 13 })</td>
<td style="padding: 3px;">@Html.TextBoxFor(x => Model.Asigurati[Model.Asigurati.IndexOf(item)].Nume)</td>
<td style="padding: 3px;">@Html.TextBoxFor(x => Model.Asigurati[Model.Asigurati.IndexOf(item)].Prenume)</td>
<td style="padding: 3px;">@Html.TextBoxFor(x => Model.Asigurati[Model.Asigurati.IndexOf(item)].DataNastere)</td>
</tr>
}
</tbody>
</table>
}
</div>
在单轨中,我不得不用 {JsonBinder] 属性来装饰输入参数,但在 MVC3 中我不知道如何继续。
它基本上给了我以下错误:
参数字典包含“BrokerPlatform”中方法“System.Web.Mvc.ActionResult GetAsiguratiSuplimentari(Int32,BrokerPlatform.Services.AcpComplex.AcpComplexAsigurat[])”的不可为空类型“System.Int32”的参数“numarAsiguratiSuplimentari”的空条目.Areas.AcpComplex.Controllers.CarpaticaController'。可选参数必须是引用类型、可空类型或声明为可选参数。参数名称:参数
这是它发送的 JSON:
{"numarAsiguratiSuplimentari":"2","oldData":""}