我有一个视图,我称之为模态弹出窗口,用于插入或编辑数据。 我已经在我的视图上为DropDownList实现了选择js,这就是问题的开始。 当我没有对jquery脚本的引用时,一切正常,在POST之后它将我重定向到Index页面并刷新表。
但是,只要我放<script src="~/Scripts/jquery-1.12.4.min.js"></script>
,
在POST之后,它将我重定向到浏览器中的JSON文件。我明白了
:
我可以以某种方式实现这个吗?或者我必须在我的控制器中返回RedirectToaction而不是JSON?
这是我的观点:
@model aspEvidencijaZgrada.Models.Korisnik
@{
if (Session["userID"] == null)
{
Response.Redirect("~/Login/Index");
}
}
<h2 style="text-align:center">Stanar</h2>
<hr />
@using (Html.BeginForm("AddOrEdit", "Korisnik", FormMethod.Post, new { onsubmit = "return SubmitFormK(this)" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal" style="padding-left:150px;">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.ID)
<div class="form-group">
@Html.LabelFor(model => model.imePrezime, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.imePrezime, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.imePrezime, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.jmbg, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.jmbg, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.jmbg, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.brStana, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.brStana, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.brStana, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.kvadratura, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.kvadratura, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.kvadratura, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.id_zgrade, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.id_zgrade, new SelectList(Model.ZgradaCollection, "ID", "naziv"), new { @class = "form-control", @id = "ddlZgrada" })
@Html.ValidationMessageFor(model => model.id_zgrade, "", new { @class = "text-danger" })
</div>
</div>
<br />
<br />
<div class="form-group" style="padding-left:150px;">
<input type="submit" value="Sacuvaj" class="btn btn-primary" style="margin-right:25px;" />
<input type="reset" value="Ponisti" class="btn" />
</div>
</div>
}
<script src="~/Scripts/jquery-1.12.4.min.js"></script>
<script src="~/Scripts/chosen.jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("#ddlZgrada").chosen();
});
</script>
这是我的控制者:
[HttpPost]
public ActionResult AddOrEdit(Korisnik model)
{
try
{
using (conn = new SqlConnection(conString))
{
if (model.ID != 0)
{
conn.Open();
string query = "UPDATE Korisnik SET imePrezime=@imePrezime,jmbg=@jmbg,brStana=@brStana,kvadratura=@kvadratura,id_zgrade=@id_zgrade WHERE ID=@id";
SqlCommand cmd = new SqlCommand(query, conn);
cmd.Parameters.AddWithValue("@id", model.ID);
cmd.Parameters.AddWithValue("@imePrezime", (object)model.imePrezime ?? DBNull.Value);
cmd.Parameters.AddWithValue("@jmbg", (object)model.jmbg ?? DBNull.Value);
cmd.Parameters.AddWithValue("@brStana", (object)model.brStana ?? DBNull.Value);
cmd.Parameters.AddWithValue("@kvadratura", (object)model.kvadratura ?? DBNull.Value);
cmd.Parameters.AddWithValue("@id_zgrade", model.id_zgrade);
cmd.ExecuteNonQuery();
return Json(new { success = true, message = "Update uspesan" }, JsonRequestBehavior.AllowGet);
}
else
{
conn.Open();
string query = "INSERT INTO Korisnik (imePrezime,jmbg,brStana,kvadratura,id_zgrade) VALUES(@imePrezime,@jmbg,@brStana,@kvadratura,@id_zgrade)";
SqlCommand cmd = new SqlCommand(query, conn);
cmd.Parameters.AddWithValue("@imePrezime", (object)model.imePrezime ?? DBNull.Value);
cmd.Parameters.AddWithValue("@jmbg", (object)model.jmbg ?? DBNull.Value);
cmd.Parameters.AddWithValue("@brStana", (object)model.brStana ?? DBNull.Value);
cmd.Parameters.AddWithValue("@kvadratura", (object)model.kvadratura ?? DBNull.Value);
cmd.Parameters.AddWithValue("@id_zgrade", (object)model.id_zgrade ?? DBNull.Value);
cmd.ExecuteNonQuery();
return Json(new { success = true, message = "Uspesno sacuvano" }, JsonRequestBehavior.AllowGet);
}
}
}
catch (Exception ex)
{
return Json(new { success = false, message = ex.Message }, JsonRequestBehavior.AllowGet);
}
}
这是我调用Popup并提交的JS代码:
function PopupForm(url) {
var formDiv = $('<div/>');
$.get(url)
.done(function (response) {
formDiv.html(response);
Popup = formDiv.dialog({
autoOpen: true,
resozable: false,
title: "Podaci o stanaru",
height: 600,
width: 900,
open: function (event, ui) {
$('.ui-dialog').css('overflow-x', 'hidden');
$('.ui-dialog-content').css('overflow-x', 'hidden');
},
close: function () {
$('body').css('overflow', 'auto');
Popup.dialog('destroy').remove();
}
});
});
}
function SubmitFormK(form) {
$.validator.unobtrusive.parse(form);
if ($(form).valid()) {
$.ajax({
type: "POST",
url: form.action,
data: $(form).serialize(),
success: function (data) {
if (data.success) {
Popup.dialog('close');
var table = $("#tableKorisnik").DataTable();
table.ajax.reload();
$.notify(data.message, {
globalPosition: "top center",
className: "success"
});
}
}
});
}
return false;
}