您有以下模态形式来执行我的 Controller 的 POST 方法
我的观点:
<div class="modal fade" id="agregarProducto">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
</div>
<div class="modal-body">
<form id="myForm">
<label>Agregar Cantidad</label>
<input type="checkbox" id="idcheckcantidad" />
<input type="text" class="form-control" name="cantidad" id="idcantidad" disabled="disabled" />
<label>Codigo Producto</label>
<input type="text" class="form-control" name="codigoproducto" id="idcodigoproducto" autofocus="true" />
</form>
</div>
<div class="modal-footer">
<input type="submit" value="Agregar Material" class="btn btn-primary" id="btnSubmit" />
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cerrar</button>
</div>
</div>
</div>
</div>
执行post方法的javascript如下...
<script>
$(document).ready(function () {
$("#btnSubmit").click(function () {
var myformdata = $("#myForm").serialize();
$.ajax({
type: "POST",
url: "/Despachos/AgregarProducto",
data: myformdata,
success: function () {
$("#agregarProducto").modal("hide");
}
})
})
})
</script>
问题是,当我完成执行 Agregar Producto 方法时,我的屏幕被挡住并且一片漆黑
我的 Controller :
[HttpPost]
public ActionResult AgregarProducto(int codigoproducto, int? cantidad)
{
//CONSULTO LOS PRODUCTOS QUE EXISTEN EN EL DETALLE
var despachotmp = db.DespachoDetalleTmps.Where(o => o.Email == User.Identity.Name && o.Kn_CodigoProducto == codigoproducto).FirstOrDefault();
if (despachotmp == null)
{
//BUSCO EL PRODUCTO
var producto = db.Productoes.Find(codigoproducto);
if (producto == null)
{
ViewBag.Error = "Debe Seleccionar un Producto o Material Válido";
return RedirectToAction("Create");
}
if (cantidad == null)
{
despachotmp = new DespachoDetalleTmp
{
v_Nombre = producto.v_Nombre,
Kn_CodigoProducto = producto.Kn_CodigoProducto,
Email = User.Identity.Name,
d_Cantidad = 1,
};
db.DespachoDetalleTmps.Add(despachotmp);
}
if (cantidad != null)
{
despachotmp = new DespachoDetalleTmp
{
v_Nombre = producto.v_Nombre,
Kn_CodigoProducto = producto.Kn_CodigoProducto,
Email = User.Identity.Name,
d_Cantidad = Convert.ToInt16(cantidad),
};
db.DespachoDetalleTmps.Add(despachotmp);
}
}
else
{
if (cantidad == 0)
{
despachotmp.d_Cantidad += 1;
db.Entry(despachotmp).State = EntityState.Modified;
}
if (cantidad != 0)
{
despachotmp.d_Cantidad += Convert.ToInt16(cantidad);
db.Entry(despachotmp).State = EntityState.Modified;
}
}
db.SaveChangesAsync();
return RedirectToAction("Create");
}
我做错了什么?为什么我会出现这种行为? 对我有什么帮助吗?
最佳答案
问题出在你的脚本上。您可以使用以下代码关闭模式。
$("#agregarProducto").hide();
您可以按照以下代码操作:
<script>
$(document).ready(function () {
$("#btnSubmit").click(function () {
var myformdata = $("#myForm").serialize();
$.ajax({
type: "POST",
url: "/Despachos/AgregarProducto",
data: myformdata,
success: function () {
$("#agregarProducto").hide();
}
})
})
})
</script>
试试这个代码。祝你好运!
关于javascript - 模态表单让我的屏幕被挡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50441935/