0
我正在构建一个MVC5应用程序(使用Razor)。在顶部的导航栏中,我有一个按钮,可用于反馈的表单淡入。我能够成功地将这些数据发布到不同的控制器(而不是当前正在生成的页面),但是我无法使页面不加载控制器的发布方法。我可以重新加载当前页面,但我想阻止任何屏幕更改。这可能吗?MVC5发布到控制器无需离开页面
我尝试使用an AJAX method来完成这个,但它没有奏效。
这里是认为我当前的代码:
<div class="modal fade" id="feedbackForm" tabindex="-1" role="dialog" aria-labelledby="feedbackFormLabel">
<div class="modal-dialog" role="document">
<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>
<h5 class="modal-title">Feedback for @ViewBag.Title</h5>
</div>
@using (Html.BeginForm("CreateRemote", "Feedback", FormMethod.Post)) {
<div class="modal-body">
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Pain)
<div class="col-md-10">
@Html.EditorFor(model => model.Pain, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Pain, "", new { @class = "text-danger" })
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<input id="FeedbackSubmit" type="submit" value="Submit" class="btn btn-success" />
</div>
}
</div>
</div>
</div>
编辑
这是我如何修改它的结束使用AJAX:如果我离开
@*@using (Html.BeginForm("CreateRemote", "Feedback", FormMethod.Post)) {*@
<div class="modal-body">
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Pain)
<div class="col-md-10">
@Html.EditorFor(model => model.Pain, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Pain, "", new { @class = "text-danger" })
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<input id="FeedbackSubmit" value="Submit" class="btn btn-success" />
<script type="text/javascript">
$('#FeedbackSubmit').click(function() {
var url = "/Feedback/CreateRemote";
var pain = 10;
$.post(url, { LanId: 'C15919' }, function (data) {
alert(data);
});
})
</script>
</div>
@*}*@
原样,input
不采取任何操作,并且不会调用控制器。如果我将其更改为<input type="submit" ....
,则该页面将更改为控制器的POST操作。
可以完成,随着AJAX。 –
我试过了,但没有奏效。不过,我对AJAX不太熟悉,所以我可能会做错了。 –
如果您想在不刷新页面的情况下将数据发布到后端,AJAX是一种适用的技术。更新您的代码以显示您在AJAX上的尝试。您现在显示的代码将执行正常的HTTP发布,因此对回答您的问题没有帮助。 –