Jquery ui 无法使用jquery对话框和Razor MVC 4的模型绑定将视图模型传递回控制器
我正在努力让我的对话框将传递给视图的完整模型传递回绑定了值的视图 这是我的剧本:Jquery ui 无法使用jquery对话框和Razor MVC 4的模型绑定将视图模型传递回控制器,jquery-ui,asp.net-mvc-4,Jquery Ui,Asp.net Mvc 4,我正在努力让我的对话框将传递给视图的完整模型传递回绑定了值的视图 这是我的剧本: var scId = 0; var monthId = 0; var initializeDialogs = function () { $('#read-transactions-dialog-form').dialog({ autoOpen: false, width: 650, modal: true, buttons: { "Submit": function
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>
}
@model OnlineBudget.WebUI.Models.TransactionViewModel
@使用(Html.BeginForm(“SubmitTransactions”,“Month”,new{tvm=Model.Transactions},FormMethod.Post,new{id=“form read Transactions”}))
{
@if(Model.Transactions.Count()>0)
{
DateAmountDescriptionExcludeDelete
}
@对于(int i=0;imodel.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].subcategorid);@Html.HiddenFor(model=>model.Transactions[i].MonthId);
如果(!Model.Transactions[i].IsDeleted)
{
@Model.Transactions[i].Date.ToShortDateString()@Model.Transactions[i]。Amount@Model.Transactions[i] .头衔
@Html.CheckBoxFor(model=>model.Transactions[i].Excluded)@Html.CheckBoxFor(model=>model.Transactions[i].IsDeleted)
}
}
}
最后,我的控制器:
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");
}
public ActionResult ViewTransactionsForSubCategory(int scid、int mid)
{
TransactionViewModel tvm=new TransactionViewModel{MonthId=mid,Transactions=transactionRepository.Transactions.Where(t=>t.Subcategory ID==scid&&t.MonthId==mid).ToList();
返回PartialView(“ReadTransactions”,tvm);
}
[HttpPost]
公共行动结果提交交易(列表tvm)
{
if(ModelState.IsValid)
{
//foreach(tvm.Transactions中的var tv)
//SaveTransaction(transactionRepository.Transactions.Where(t=>t.Id==tv.Id).First());
}
返回操作(“索引”);
}
我最近将post控制器更改为只接受事务列表,而不是TransactionViewModel,但这也不起作用。在这种情况下,我不断得到null或空列表,但对话框中的表中肯定填充了数据。
任何帮助都将不胜感激。我以前在MVC接受集合作为类似参数时遇到过问题。最后,我将javascript调用切换为执行$.ajax post($.post有时也有一些问题),特别是包括内容和数据类型
$.ajax({
type: 'Post',
dataType: 'json',
url: 'url-goes-here',
data: JSON.stringify({ data: data}),
contentType: 'application/json; charset=utf-8'
});
虽然没有自动绑定那么好,但它确实会将数据作为一个集合发回。我使用$。类似这样的postfy将复杂的js对象发布到MVC: e、 g
请参阅:如何将模型放入数据变量?
var postData = {
id : 123,
childArray : [
{ subid : 456, value1 : "Foo" },
{ subid : 789, value1 : "Bat" }
]
};
$.post("postRoute", $.postify(postData), function(response) {
// stuff
});