我不知道这是否是一个真正的问题,或者我对如何做事的想法有误,但是您如何将 json 对象(来自淘汰赛)与表单一起发布到 MVC 控制器?
首先,这是我的控制器:
[HttpPost]
public ActionResult CreateLoanApp(PeopleViewModel MyViewModel)
{
//Do something on MyViewModel;
return RedirectToAction("Index");
}
[HttpPost]
public JsonResult CreateLoanApp(string deductions)
{
//Do something on string deductions;
}
这是我的观点:
<h2>My Form </h2>
@using (Html.BeginForm(new { @class = "submitForm" }))
{
<label>Loan Amount</label>
@Html.DropDownListFor(model => model.Loan.LoanAmount, Model.DropDownOfLoanAmount, new { @class = "LoanAmount", @data_bind = "value: selectedLoanAmount" })
@Html.ValidationMessageFor(model => model.Loan.LoanAmount)
<label>Loan Receivable</label>
@Html.TextBoxFor(model => model.Loan.LoanReceivable, "{0:0,0.00}", new { @class = "LoanReceivable", @readonly = true, dir = "rtl", @data_bind = "value: loanReceivable" })
@Html.ValidationMessageFor(model => model.Loan.LoanReceivable)
<label>Interest</label>
@Html.TextBoxFor(model => model.Loan.Interest, "{0:0,0.00}", new { @readonly = true, @class = "Interest", dir = "rtl", @data_bind = "value: interest" })
<table class="input-group">
<tbody data-bind="foreach: loanDeductions">
<tr>
<td><strong data-bind='text: deductionName'></strong></td>
<td>
<input class="deductionCode form-control" data-bind='value: amount, valueUpdate: "afterkeydown"' /></td>
<td><a href='#' data-bind='click: $parent.removeLine'>Delete</a></td>
</tr>
</tbody>
</table>
<button type="button" class="btn btn-danger" data-bind="click: save">Save Deduction</button>
<button type="submit" class="btn btn-primary">Save changes</button>
}
如您所见,我有 2 个不同的保存按钮:
1.“保存扣除”按钮调用一个 ajax 函数,该函数将一个名为“扣除”的 json 字符串发布到我的控制器中的一个操作。
2. 另一方面,“保存更改”按钮是一个提交按钮,将表单提交到我的控制器并传递“MyViewModel”。
现在我要做的是将 2 个按钮合并为一个,并将 2 个对象一起传递到一个控制器中。
我的意思是我想创建一个接受 2 个参数的操作,如下所示:
[HttpPost]
public ActionResult CreateLoanApp(PeopleViewModel MyViewModel, string deductions)
{
// Do something on MyViewModel
//Do something on string deductions;
}
这可能吗?如果可以,你能告诉我它是如何完成的。
任何帮助将不胜感激。如果您需要更多详细信息,请发表评论。谢谢!