当我发布表单时,我需要从HTML表格和其他表单字段中保存数据。 在这里,我需要将表单和其他数组数据发布到action.This是我尝试过的代码。
示例视图
<div class="form-group">
<label for="Support" class="col-sm-2 control-label">Support Desired</label>
<div class="col-sm-10">
@Html.TextAreaFor(model => model.SupportDesired, new { @class = "form-control", @placeholder = "Support Desired", @tabindex = "4" })
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<table class="table table-condensed" id="tblselectedcontact">
<thead>
<tr>
<th style="width: 30px"></th>
<th>Name</th>
<th>Designation</th>
<th>Department</th>
<th>Phone</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="#" class="glyphicon glyphicon-remove" onclick="CloseUserRow(this)"></a></td>
<td><input data-val="true" data-val-number="The field ContactId must be a number." data-val-required="The ContactId field is required." id="customer_ContactId" name="customer.ContactId" value="1" type="hidden">ABC</td>
<td>ABC</td>
<td>ABC</td>
<td>ABC</td>
</tr>
</tbody>
</table>
</div>
</div>
...../*all fields goes like this*/
......
脚本代码
function postform() {
var ActivityContacts= [];
$('#tblselectedcontact input:hidden').each(function () {
ActivityContacts.push({
"ContactId": $(this).val()
});
});
var data = $('#frmCreate').serializeArray();
data.push(ActivityContacts);
console.log(ActivityContacts);
$.post('@Url.Action("Create")', data);
}
行动方法
[HttpPost]
public ActionResult Create(Model.Activity model)
{
//Create code goes here
}
在动作方法中,我获取了模型中的所有表单字段,但没有找到其他数据。