[英]Submit button not posting on modal
I have a form on a bootstrap modal with two buttons. 我有一个带有两个按钮的引导程序模态表单。 This form is tied to an action named "DeleteWidgetConfirmed" I am trying to remove a widget from the database and from the front end, the panel gets removed from the front end but does not seem to get removed from the database. 该表单与一个名为“ DeleteWidgetConfirmed”的动作相关联。我试图从数据库中删除一个小部件,并且从前端将面板从前端删除,但似乎并未从数据库中删除。
Here is my Modal 这是我的模态
<div class="modal fade" id="deleteWidgetModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Delete widget?</h4><!--add depending on which panel you have clicked-->
</div>
<div class="modal-body" id="myModalBody">
<!--Depending on which panel insert content-->
@using (Html.BeginForm("DeleteWidgetConfirmed", "Dashboard", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
Do you wish to delete this widget?
<div class="form-group">
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="submit" value="DeleteWidgetConfirmed" class="btn btn-danger btn-ok" id="delete-widget">Delete</button>
</div>
</div>
</div>
}
</div>
</div>
</div>
Here is my action: 这是我的动作:
// POST: DashboardModels/Delete/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult DeleteWidgetConfirmed(int? id)
{
if(id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
DashboardModel dashboardModel = db.dashboards.Find(id);
db.dashboards.Remove(dashboardModel);
db.SaveChanges();
return new EmptyResult();
}
From my javascript I get the ID from the panel and store it into a variable, I then get the action attribute from my form and append the ID to the action attribute. 从我的JavaScript中,我从面板上获取ID并将其存储到变量中,然后从表单中获取action属性并将ID附加到action属性。
$(document).ready(function () {
$('#columns').on('click', '.glyphicon.glyphicon-trash', function (event) {
var panel = this;
//get id here
//toggle the modal
$('#deleteWidgetModal').modal('show');
var widgetID = $(this).closest('.panel.panel-default').attr('data-widgetid');
document.getElementById('delete-widget').onclick = function (event) {
event.stopPropagation();
//we make an ajax call to the controller on click
$.ajax({
url: '@Html.Raw(Url.Action("Dashboard", "DeleteWidgetConfirmed"))',
data: { id: widgetID},
type: 'POST',
dataType: 'json',
contentType: 'application/json; charset=utf-8',
success: function(data){
var parentElement = $(panel).closest(".col-md-4.column");
var targetElement = $(panel).closest(".panel.panel-default");
targetElement.remove();
//parentElement.addClass("expand-panel");
checkEmptyPanelContainers();
$('#deleteWidgetModal').modal('hide');
},
error: function (response) {
}
})
}
})
}); });
I have a hunch that maybe within my javascript I have overridden the default behaviour of the event. 我有一种预感,也许在我的JavaScript中,我已经覆盖了事件的默认行为。
What I want to achieve ultimately is 我最终想要实现的是
Try using AJAX to asynchronously post to your controller: 尝试使用AJAX异步发布到您的控制器:
$(document).ready(function () {
$('#columns').on('click', '.glyphicon.glyphicon-trash', function (event) {
var panel = this;
//get id here
//toggle the modal
$('#deleteWidgetModal').modal('toggle');
var widgetID = $(this).closest('.panel.panel-default').attr('data-widgetid');
$.ajax({
url: '/Dashboard/DeleteWidgetConfirmed/',
type: 'POST',
data: { id: widgetid },
dataType: 'json',
contentType: 'application/json; charset=utf-8',
error: function (xhr) {
// request failed, handle here!
},
success: function (result) {
// request succeeded! handle that here. close the modal? remove the item from the UI?
}
});
}
});
});
How you handle the success callback depends on the UI, you can use the data- attributes to do so quite easily. 如何处理成功回调取决于UI,您可以使用data-attribute来轻松实现。
You need to decorate your action method as POST if you do this: 如果执行此操作,则需要将操作方法装饰为POST:
[HttpPost]
public ActionResult DeleteWidgetConfirmed(int id) {
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.