你好。我之前发布了一个ajax表单的问题,但我仍然无法完成这个模块。问题是当我打开这个Formulario Ajax
(它以 a 打开dialog
并且它的div
形式在任何其他形式之外 - 它在里面)时,我添加了数据并且在提交它时通过(最多 10 次layout
)的操作发生了超过 1次controller
),就好像提交按钮被按下了很多次,并且它永远不会从textbox
.
<div id='Div-AB-Email' style="display: none">
<div id='Div-Email'></div>
</div>
JS:
function abrirFormEmail() {
Preloader();
preloaderBusquedas("Div-Email");
$("#Div-AB-Email").dialog({
autoOpen: true,
open: function (event, ui) {
$("#Div-Email").load("/EmailPersona/Create");
$("#Mail").val("");
$("#Observaciones").val("");
},
focus: function (event, ui) { $("#Mail").focus(); },
show: {
effect: "blind",
duration: 500,
},
hide: {
effect: "blind",
duration: 500
},
width: 1036,
height: 400,
title: "Email de contacto ",
closeOnEscape: true,
});
PreloaderOff();
}
C#:
public class EmailPersonaController : BaseController
{
public PartialViewResult Create()
{
return PartialView();
}
[HttpPost]
public PartialViewResult Create(EmailPersonaModel model)
{
if (model == null)
return PartialView();
if (model.IdPersona == 0)
{
model.IdPersona = ConsultaIdPersona();
ModelState.Remove("IdPersona");
}
try
{
if (ModelState.IsValid)
{
if (model.Nuevo())
{
model.grabado = true;
ListItemsCarga().Add(model);
var modelnull = new EmailPersonaModel();
return PartialView(modelnull);
}
else
{
return PartialView(model);
}
}
else
{
if (string.IsNullOrEmpty(model.Mail))
TempData["ErrorEmail"] = "Debe escribir el mail que desea agregar a la lista";
}
return PartialView(model);
}
catch (Exception e)
{
if (e.Message == "ERROR: 23505: llave duplicada viola restricción de unicidad «uq_email_per»")
{
TempData["ErrorEmail"] = "El Mail que quiere cargar pertenece a otra persona";
}
return PartialView();
}
}
}
剃刀:
@model QBOWEB3.Models.EmailPersonaModel
<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
@{
ViewBag.Title = "Create";
}
@using (Ajax.BeginForm("Create", "EmailPersona",
new AjaxOptions
{
HttpMethod = "POST",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "Div-Email"
}))
{
<fieldset>
<h3>Email De Contacto</h3>
<span>@ViewBag.Afiliado</span>
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<legend>EmailPersonaModel</legend>
<div id="contenidoEmail">
<table class="TableCampsEdicion">
<tr>
<td class="tdLabel">
@Html.LabelFor(model => model.Mail)
</td>
<td class="tdTextBox">
@Html.EditorFor(model => model.Mail)
</td>
<td class="tdLabel">
@Html.LabelFor(model => model.Observaciones)
</td>
<td class="tdTextBox">
@Html.EditorFor(model => model.Observaciones)
</td>
@Html.EditorFor(model => model.IdPersona)
<td class="tdTextBox">
<input type="submit" value="Agregar" class="botonAzulMediano" style="width: 95px; margin-left: 10px;" />
</td>
</tr>
<tr>
<td class="tdTextBox" colspan="5">
@Html.ValidationMessageFor(model => model.Mail)
@Html.ValidationMessageFor(model => model.Observaciones)
</td>
</tr>
</table>
@* <div id="itemsEmail">@Html.Action("Index")</div>*@
@if (@TempData["ErrorEmail"] != null && @TempData["ErrorEmail"].ToString() != "")
{
<script>
$(function () {
mensaje("@TempData["ErrorEmail"]");
});
</script>
}
@if (Model != null && Model.grabado)
{
<script>
$(function () {
$("#Mail").val("");
$("#Observaciones").val("");
});
</script>
}
<script>
$(function () {
$("#Mail").focus();
Enter_por_Tab();
PreloaderOff();
})
</script>
</div>
</fieldset>
}
正如我在评论中建议的那样,您可以尝试将 Ajax.BeginForm 更改为使用 jquery 中的 $.ajax 的调用。
要发送表单,您可以在其上使用 jquery 的.serialize()
form
带有 AJAX 的 Web API:序列化后提交表单数据
在示例中,观察到它用于
data: $('#form1').serialize(),
将数据发送form
到控制器的操作但不要在部分视图中定义 Ajax 或 Html BeginForm,而是
form
使用html
.>>在控制器的创建动作中,我应该返回什么?
返回取决于,有些返回 json 或HttpStatusCodeResult 指示
Ok
fromhttp
但它也可能是 void 或简单值,如字符串或布尔值