【发布时间】:2015-08-11 20:58:24
【问题描述】:
我正在开发一个 ASP.NET MVC 项目,在该项目中,提交表单后,模型会在数据库中创建一条新记录并将记录 ID 发送回表单(然后将在一些额外的 JavaScript 中对其进行操作代码)。
我正在使用 Telerik Kendo 窗口控件,因此其想法是用户将单击 Kendo 弹出窗口中的提交按钮并设置事件链。收到值后,Kendo 弹出窗口将关闭。
我一直在关注这个帖子中的例子:PartialView with a form in a kendo window
就提交表单而言,此行为可以正常工作。此代码不会从控制器接收任何值。在视图中:
<input type="button" value="Create" class="btn btn-default" onclick="formSubmit()"/>
后来……
<script>
function formSubmit() {
if (!inIframe()) {
// We are a parent window
$('form').submit();
}
else {
// We are a pop-up window
$('form').submit();
parent.$('#window').data('kendoWindow').close();
}
}
</script>
@(Html.Kendo().Window()
.Name("window")
.Title("Attached Report")
.Content("Loading...")
.Actions(actions => actions.Close())
.Iframe(true)
.Height(700)
.Width(1000)
.Modal(true)
.AutoFocus(true)
.Visible(false)
)
在控制器中:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(VMMyViewModel viewModel)
{
// Code omitted...basically, we save to the database
db.SaveChanges();
if (viewModel.IsInPopupWindow)
{
return null; // We return null to help us exit out of the popup window.
}
else
{
return RedirectToAction("Index"); // Since we're in the main window, go back to the index after submitting.
}
}
我想获取我们刚刚保存到数据库中的记录的记录 ID,并以某种方式将其反馈给 jQuery 调用代码。我可以在控制器中发回记录 ID 的实际值,但它将作为包含记录 ID 的网页返回...
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(VMMyViewModel viewModel)
{
// Code omitted...basically, we save stuff
db.SaveChanges();
if (viewModel.IsInPopupWindow)
{
return Content(reportHeader.Id.ToString());
}
else
{
return RedirectToAction("Index"); // Since we're in the main window, go back to the index after submitting.
}
}
在 jQuery 方面,我似乎不应该使用 $('form').submit() 因为没有办法通过成功/错误机制取回数据。我尝试了其他几种方法(viewbags、jQuery trigger、jQuery POST、Kendo 窗口控件上的关闭机制),但到目前为止我还无法将所有部分放在一起。
我怀疑我的处理方式完全错误。任何建议将不胜感激!
【问题讨论】:
-
您目前所做的只是提交一个标准表单,该表单将执行重定向。您需要使用 ajax 发布表单数据并将
ID值作为 JsonResult 返回 -
你能显示一些关于你的表单的代码吗?
-
我猜它可能是 iframe。我看到你剑道窗口有一个选项 iframe(true),这是否意味着 iframe 中的表单?iframe 就像另一个窗口,在你的主页中,很难了解 js 在 iframe 中的作用
标签: jquery asp.net-mvc kendo-asp.net-mvc