我正在努力让我的对话框将传递给视图的完整模型传回并绑定值。
这是我的脚本:
var scId = 0;
var monthId = 0;
var initializeDialogs = function () {
$('#read-transactions-dialog-form').dialog({
autoOpen: false,
width: 650,
modal: true,
buttons: {
"Submit": function () {
$('#form-read-transactions').submit();
$(this).dialog("close");
},
"Cancel": function () {
$(this).dialog("close");
}
},
open:
function(event, ui) {
$(this).load("/Month/ViewTransactionsForSubCategory/?scid=" + scId + "&mid=" + monthId);
}
});
function loadId(scid, mid) {
scId = scid;
monthId = mid;
}
$(document).ready(function () {
$('a#read-transactions').click(function () {
$('#read-transactions-dialog-form').dialog('open');
});
initializeDialogs();
});
我的视图对话框:
@model OnlineBudget.WebUI.Models.TransactionsViewModel
@using (Html.BeginForm("SubmitTransactions", "Month", new { tvm = Model.Transactions }, FormMethod.Post, new { id = "form-read-transactions" }))
{
<div id="read-transactions-dialog-form" class="modalDialog" title="Transactions">
<table class="table">
@if (Model.Transactions.Count() > 0)
{
<tr><th>Date</th><th>Amount</th><th>Description</th><th>Exclude</th><th>Delete</th> </tr>
}
@for (int i = 0; i < Model.Transactions.Count; i++)
{
@Html.HiddenFor(model => Model.Transactions[i].Id); @Html.HiddenFor(model => Model.Transactions[i].Title); @Html.HiddenFor(model => Model.Transactions[i].Date); @Html.HiddenFor(model => Model.Transactions[i].Amount);
@Html.HiddenFor(model => Model.Transactions[i].SubCategoryId); @Html.HiddenFor(model => Model.Transactions[i].MonthId);
if (!Model.Transactions[i].IsDeleted)
{
<tr>
<td>@Model.Transactions[i].Date.ToShortDateString()</td> <td>@Model.Transactions[i].Amount</td><td>@Model.Transactions[i].Title</td>
<td>@Html.CheckBoxFor(model => Model.Transactions[i].Excluded)</td><td>@Html.CheckBoxFor(model => Model.Transactions[i].IsDeleted)</td>
</tr>
}
}
</table>
</div>
}
最后是我的控制器:
public ActionResult ViewTransactionsForSubCategory(int scid, int mid)
{
TransactionsViewModel tvm = new TransactionsViewModel { MonthId = mid, Transactions = transactionRepository.Transactions.Where(t => t.SubCategoryId == scid && t.MonthId == mid).ToList() };
return PartialView("ReadTransactions", tvm);
}
[HttpPost]
public ActionResult SubmitTransactions(List<OnlineBudget.Domain.Entities.Transaction> tvm)
{
if (ModelState.IsValid)
{
//foreach (var tv in tvm.Transactions)
// transactionRepository.SaveTransaction(transactionRepository.Transactions.Where(t => t.Id == tv.Id).First());
}
return RedirectToAction("Index");
}
我最近将 post 控制器更改为仅接受列表 og 事务而不是 TransactionsViewModel,但这也不起作用。在这种情况下,我不断得到空值或空列表,但对话框中的表中肯定填充了数据。任何帮助都感激不尽。