Javascript 尝试在jQuery对话框中的按钮单击上执行两种不同的功能
我正在做MVC4项目。我使用对话框来执行操作。我使用视图和局部视图来完成我的任务 我尝试执行两个操作的意思是我想调用一个 将现有数据保存在数据库中并关闭的操作方法 该对话框与添加功能(即添加)相同 在数据库中新建行并正确关闭对话框 现在发生了什么Javascript 尝试在jQuery对话框中的按钮单击上执行两种不同的功能,javascript,jquery,asp.net-mvc,Javascript,Jquery,Asp.net Mvc,我正在做MVC4项目。我使用对话框来执行操作。我使用视图和局部视图来完成我的任务 我尝试执行两个操作的意思是我想调用一个 将现有数据保存在数据库中并关闭的操作方法 该对话框与添加功能(即添加)相同 在数据库中新建行并正确关闭对话框 现在发生了什么 我可以成功地编辑记录并将其保存在数据库中。 但一旦我点击“创建新链接”,它就会出现 显示在编辑弹出窗口中输入的记录 如果我第一次点击创建新链接,我会弹出保存窗口,一旦我点击保存按钮,它就不会保存到数据库中 总之 我无法在我的应用程序中调用对话框的关闭方
显示在编辑弹出窗口中输入的记录
@model PITCRoster.RosterManagementEntities
@{
ViewBag.Title = "CurrentLocation";
}
<script src="~/Content/PopUp.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<h2>CurrentLocation</h2>
@*<input type="button" value="List All Current Locations" onclick="Display('DisplayCurrentLocation')" />*@
@{
Html.RenderPartial("_DisplayCurrentLocation", Model.tblCurrentLocations);
}
<div id="dialog">
@using (Html.BeginForm("AddLocation", "Lookups", FormMethod.Post))
{
Html.RenderPartial("_AddLocation", new PITCRoster.tblCurrentLocation());
}
</div>
@model IEnumerable<PITCRoster.tblCurrentLocation>
<p>
<a href="#" class="create">Create New</a>
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.LocationId)
</th>
<th>
@Html.DisplayNameFor(model => model.Location)
</th>
<th>
@Html.DisplayNameFor(model => model.Description)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.LocationId)
</td>
<td>
@Html.DisplayFor(modelItem => item.Location)
</td>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td>
<a href="#" class="edit" data-url="@Url.Action("EditLocation", "Lookups", new { id=item.LocationId})">Edit</a>
@using (Html.BeginForm("DeleteLocation", "Lookups", new { id = @item.LocationId }, FormMethod.Post, new { @class="deleteForm"}))
{
@Html.AntiForgeryToken();
<input type="submit" value="Delete" />
}
</td>
</tr>
}
</table>
@model PITCRoster.tblCurrentLocation
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>tblCurrentLocation</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Location)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Location)
@Html.ValidationMessageFor(model => model.Location)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Description)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Description)
@Html.ValidationMessageFor(model => model.Description)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
@model PITCRoster.tblCurrentLocation
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
@using (Html.BeginForm("SaveLocation", "Lookups", null, FormMethod.Post, new { @class = "saveForm" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>tblCurrentLocation</legend>
<div class="editor-label">
@Html.LabelFor(model => model.LocationId)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.LocationId)
@Html.ValidationMessageFor(model => model.LocationId)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Location)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Location)
@Html.ValidationMessageFor(model => model.Location)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Description)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Description)
@Html.ValidationMessageFor(model => model.Description)
</div>
<p>
<input type="submit" value="Save"/>
</p>
</fieldset>
public ActionResult CurrentLocation()
{
return View(rosterManagementEntities);
}
\u添加位置局部视图
@model PITCRoster.RosterManagementEntities
@{
ViewBag.Title = "CurrentLocation";
}
<script src="~/Content/PopUp.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<h2>CurrentLocation</h2>
@*<input type="button" value="List All Current Locations" onclick="Display('DisplayCurrentLocation')" />*@
@{
Html.RenderPartial("_DisplayCurrentLocation", Model.tblCurrentLocations);
}
<div id="dialog">
@using (Html.BeginForm("AddLocation", "Lookups", FormMethod.Post))
{
Html.RenderPartial("_AddLocation", new PITCRoster.tblCurrentLocation());
}
</div>
@model IEnumerable<PITCRoster.tblCurrentLocation>
<p>
<a href="#" class="create">Create New</a>
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.LocationId)
</th>
<th>
@Html.DisplayNameFor(model => model.Location)
</th>
<th>
@Html.DisplayNameFor(model => model.Description)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.LocationId)
</td>
<td>
@Html.DisplayFor(modelItem => item.Location)
</td>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td>
<a href="#" class="edit" data-url="@Url.Action("EditLocation", "Lookups", new { id=item.LocationId})">Edit</a>
@using (Html.BeginForm("DeleteLocation", "Lookups", new { id = @item.LocationId }, FormMethod.Post, new { @class="deleteForm"}))
{
@Html.AntiForgeryToken();
<input type="submit" value="Delete" />
}
</td>
</tr>
}
</table>
@model PITCRoster.tblCurrentLocation
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>tblCurrentLocation</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Location)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Location)
@Html.ValidationMessageFor(model => model.Location)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Description)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Description)
@Html.ValidationMessageFor(model => model.Description)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
@model PITCRoster.tblCurrentLocation
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
@using (Html.BeginForm("SaveLocation", "Lookups", null, FormMethod.Post, new { @class = "saveForm" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>tblCurrentLocation</legend>
<div class="editor-label">
@Html.LabelFor(model => model.LocationId)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.LocationId)
@Html.ValidationMessageFor(model => model.LocationId)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Location)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Location)
@Html.ValidationMessageFor(model => model.Location)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Description)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Description)
@Html.ValidationMessageFor(model => model.Description)
</div>
<p>
<input type="submit" value="Save"/>
</p>
</fieldset>
public ActionResult CurrentLocation()
{
return View(rosterManagementEntities);
}
添加位置
[HttpPost]
公共操作结果AddLocation(tblCurrentLocation tblCurrentLocation)
{
unitOfWork.tblCurrentLocation.Insert(tblCurrentLocation);
unitOfWork.Save();
返回视图(“当前位置”,rosterManagementEntities);
}
编辑位置
public ActionResult EditLocation(string id)
{
int locationId = Convert.ToInt32(id);
tblCurrentLocation tblCurrentLocation = unitOfWork.tblCurrentLocation.GetByID(locationId);
return PartialView("_EditLocation", tblCurrentLocation);
}
[HttpPost]
public ActionResult SaveLocation(tblCurrentLocation tblCurrentLocation)
{
Response.Cache.SetExpires(DateTime.Now);
unitOfWork.tblCurrentLocation.Update(tblCurrentLocation);
unitOfWork.Save();
return View("CurrentLocation", rosterManagementEntities);
}
保存位置
public ActionResult EditLocation(string id)
{
int locationId = Convert.ToInt32(id);
tblCurrentLocation tblCurrentLocation = unitOfWork.tblCurrentLocation.GetByID(locationId);
return PartialView("_EditLocation", tblCurrentLocation);
}
[HttpPost]
public ActionResult SaveLocation(tblCurrentLocation tblCurrentLocation)
{
Response.Cache.SetExpires(DateTime.Now);
unitOfWork.tblCurrentLocation.Update(tblCurrentLocation);
unitOfWork.Save();
return View("CurrentLocation", rosterManagementEntities);
}
删除
[HttpPost]
public ActionResult DeleteLocation(int id)
{
unitOfWork.tblCurrentLocation.Delete(id);
unitOfWork.Save();
return View("CurrentLocation", rosterManagementEntities);
}
我将发布我的对话脚本
当用户单击编辑链接时,它将发送一个具有唯一ID的url,并且在jQuery$中。get()
将检索该数据并显示如下对话框
$('.edit').click(function () {
$.get($(this).data('url'), function (data) {
$('#dialog').dialog({
autoOpen: true,
modal: true,
bgiframe: true,
width: 800,
height: 250,
open: function () {
document.getElementById('dialog').innerHTML = data;
},
close: function () {
debugger;
document.getElementById('dialog').innerHTML = "";
document.getElementById('dialog').innerText = "";
$("#dialog").empty().dialog("destroy");
}
});
});
});
加载表单时,它将创建一个对话框,并将其设置为自动打开
当用户点击“创建新链接”时,它将打开该对话框
这是用于创建新位置的
$(".create").click(function CreateNewUser() {
debugger;
try {
$("#dialog").dialog('open');
}
catch (e) {
alert(e.message);
}
});
$("#dialog").dialog({
autoOpen: false,
modal: true,
bgiframe: true,
width: 800,
height: 600,
close: function () {
debugger;
document.getElementById('dialog').innerHTML = "";
document.getElementById('dialog').innerText = "";
$("#dialog").empty().dialog("destroy");
window.location.reload();
}
});
因此,我将再次说明我的问题
我的代码执行销毁对话框操作或将数据保存在数据库中;但不是两者都有
我无法在我的对话框中调用关闭方法和操作方法
控制器同时控制这两个部件
我应该更改什么代码来执行这两项任务
我有一种预感,应该使用Ajax。。。但是怎么做呢
请帮忙
抱歉发了这么长的帖子
谢谢你的耐心……) 这是你想要的功能吗?是的。。但是我怎样才能在我的控制器中调用action方法,并执行隐藏/销毁对话框的必要任务…你只需处理表单中的按钮-但你究竟为什么每次都要销毁对话框并进行刷新?如果要编辑行,只需将表单值设置为行中的值,如果要添加新行,请重置表单。在ajax回调中,如果成功,则更新相应行中的值。这样你也可以得到客户端验证和更好的性能。凯,我会试试的,谢谢,伙计:-)嘿@StephenMuecke,我试过你的代码。。它按照我的要求工作。。。但是我不能把你的代码和我的代码粘在一起。。。你能帮我在哪里修改你的代码以使其正常工作吗?这是你想要的功能吗?是的。。但是我怎样才能在我的控制器中调用action方法,并执行隐藏/销毁对话框的必要任务…你只需处理表单中的按钮-但你究竟为什么每次都要销毁对话框并进行刷新?如果要编辑行,只需将表单值设置为行中的值,如果要添加新行,请重置表单。在ajax回调中,如果成功,则更新相应行中的值。这样你也可以得到客户端验证和更好的性能。凯,我会试试的,谢谢,伙计:-)嘿@StephenMuecke,我试过你的代码。。它按照我的要求工作。。。但是我不能把你的代码和我的代码粘在一起。。。你能帮我把你的代码改成我的代码吗?