0

我正在努力让我的对话框将传递给视图的完整模型传回并绑定值。

这是我的脚本:

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,但这也不起作用。在这种情况下,我不断得到空值或空列表,但对话框中的表中肯定填充了数据。任何帮助都感激不尽。

4

2 回答 2

0

我像这样使用 $.postify 将复杂的 js 对象发布到 MVC:

例如

var postData = {
    id : 123,
    childArray : [
        { subid : 456, value1 : "Foo" },
        { subid : 789, value1 : "Bat" }
    ]
};

$.post("postRoute", $.postify(postData), function(response) {
    // stuff
});

请参阅:http ://www.nickriggs.com/posts/post-complex-javascript-objects-to-asp-net-mvc-controllers/

于 2013-08-08T16:08:02.747 回答
0

我之前遇到过 MVC 接受集合作为与此类似的参数的问题。我最终将我的 javascript 调用切换为执行 $.ajax 帖子($.post 有时也有一些问题),特别是包括内容和数据类型

$.ajax({
   type: 'Post',
   dataType: 'json',
   url: 'url-goes-here',
   data: JSON.stringify({ data: data}),
   contentType: 'application/json; charset=utf-8'
});

不如自动绑定那么好,但它确实将数据作为集合发布回来。

于 2013-08-08T15:27:22.747 回答