Estou tentando chamar uma Action através do .ajax que roda dentro de um botão de um Modal mas aparentemente a url não é processada. Minha View está assim:
@using (Html.BeginForm())
{
<div class="form-horizontal">
<h2>Resfriador</h2>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Data_Coleta, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-2">
@Html.EditorFor(model => model.Data_Coleta, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Data_Coleta, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Hora_Coleta, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-2">
@Html.EditorFor(model => model.Hora_Coleta, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Hora_Coleta, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.KW_entrada_resfriador, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-2">
@Html.EditorFor(model => model.KW_entrada_resfriador, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.KW_entrada_resfriador, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.AG_entrada_resfriador, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-2">
@Html.EditorFor(model => model.AG_entrada_resfriador, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.AG_entrada_resfriador, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.WGW_entrada_resfriador, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-2">
@Html.EditorFor(model => model.WGW_entrada_resfriador, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.WGW_entrada_resfriador, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.normal_temp_inicial, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-2">
@Html.EditorFor(model => model.normal_temp_inicial, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.normal_temp_inicial, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.normal_temp_final, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-2">
@Html.EditorFor(model => model.normal_temp_final, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.normal_temp_final, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.moido_temp_inicial, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-2">
@Html.EditorFor(model => model.moido_temp_inicial, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.moido_temp_inicial, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.moido_temp_final, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-2">
@Html.EditorFor(model => model.moido_temp_final, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.moido_temp_final, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.granulometria63, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-2">
@Html.EditorFor(model => model.granulometria63, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.granulometria63, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.granulometria200, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-2">
@Html.EditorFor(model => model.granulometria200, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.granulometria200, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.temp_entrada, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-2">
@Html.EditorFor(model => model.temp_entrada, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.temp_entrada, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.temp_saida, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-2">
@Html.EditorFor(model => model.temp_saida, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.temp_saida, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Salvar" class="btn btn-default " data-toggle="modal" data-target="#myModal" />
</div>
</div>
</div>
}
@if (ViewBag.Message == "Negado")
{
<div id="myModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
<h4>ATENÇÃO!</h4>
<p id="demo">
Valor fora do parâmetro. Deseja salvar?
</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary waves-effect" data-dismiss="modal">Fechar</button>
<button type="submit" class="btn btn-success btnSalvar" id="Salvar" onclick="myFunction(this.id)" >Salvar</button>
</div>
</div>
</div>
</div>
}
Script:
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script>
$(document).ready(function () {
$('#myModal').modal('show');
});
</script>
<script>
function myFunction(value)
{
$.ajax({
type: 'POST'
url:' @Url.Action("Salvar", "ResfriadorLAB") ',
})
};
</script>
}
Action Salvar:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Salvar(ResfriadorLAB resfriadorLAB)
{
var objlogin = db.Login.OrderByDescending(p => p.LoginId).FirstOrDefault();
var objref = db.ReferenciasLABs.OrderByDescending(p => p.ReferenciasLABId).FirstOrDefault();
resfriadorLAB.Data_Cadastro = DateTime.Today;
resfriadorLAB.Hora_Cadastro = DateTime.Now;
resfriadorLAB.LoginId = objlogin.UsuarioId;
resfriadorLAB.ReferenciasLABId = objref.ReferenciasLABId;
if (ModelState.IsValid)
{
db.ResfriadorLABs.Add(resfriadorLAB);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.LoginId = new SelectList(db.Login, "LoginId", "LoginId", resfriadorLAB.LoginId);
ViewBag.ReferenciasLABId = new SelectList(db.ReferenciasLABs, "ReferenciasLABId", "ReferenciasLABId", resfriadorLAB.ReferenciasLABId);
return View(resfriadorLAB);
}
Quando clico no botão Salvar de dentro da Modal não acontece nada. Já tentei colocar a url: '~/ResfriadorLAB/Salvar' e também usar o método callPostBack mas mesmo assim não consigo abrir a Action Salvar. Preciso salvar mesmo quando o valor cadastrado está fora do parâmetro de validação. Alguém consegue ver o porquê do botão não direcionar para Action?