Jquery 确认数据行后如何刷新部分视图?
我在Asp MVC4中使用Ajax加载PartialView。但当我单击确认或删除时,如何在PartialView中刷新数据并保持当前页面状态? 这是Jquery:Jquery 确认数据行后如何刷新部分视图?,jquery,ajax,asp.net-mvc,asp.net-mvc-4,Jquery,Ajax,Asp.net Mvc,Asp.net Mvc 4,我在Asp MVC4中使用Ajax加载PartialView。但当我单击确认或删除时,如何在PartialView中刷新数据并保持当前页面状态? 这是Jquery: $("button").click(function () { var weekno = []; $.each($(".selectpicker option:selected"), function () { weekno.push($(this).v
$("button").click(function () {
var weekno = [];
$.each($(".selectpicker option:selected"), function () {
weekno.push($(this).val());
});
$.ajax({
url: "/Home/PartialTable",
type: "POST",
data: JSON.stringify(weekno),
cache: false,
contentType: "application/json; charset=utf-8",
dataType: "html",
success: function (view) {
$('#viewpartial').html(view);
},
error: function (req, status, error) {
alert("Error! Please select Weekly No !!");
}
});
});
加载PartialView Html:
<div id ="viewpartial" class="tab-content" style="overflow: scroll" </div>
我想知道什么是确认行动的回报!谢谢大家如果您不想刷新原始页面,您应该通过ajax进行确认呼叫 此外,最好将任何更新数据的代码放在
HttpPost
操作方法后面,以防止人们试图通过带有查询字符串值的浏览器执行您的方法
因此,让我们用HttpPost
属性标记Confirm
方法。可以将json结构作为方法的返回值返回
[HttpPost]
public ActionResult Confirm(Int fn)
{
try
{
// update your db record here
return Json( new { status="success"});
}
catch(Exception ex)
{
//log the exception
return Json( new { status="error"});
}
}
为您的链接提供一个id
,以便我们以后在jQuery中使用它
@Html.ActionLink("Confirm", "Confirm","Home",
new { fn = file.FILEID, new { id="confirmLink"})
在脚本中,您可以收听此链接上的单击
事件,然后对动作方法进行ajax调用。您可以使用jQuerypost
方法来实现这一点
$(function(){
$("#confirmLink").click(function(e){
e.preventDefault();
_this=$(this);
$.post(_this.attr("href"),function(res){
if(res.status==="success")
{
alert("Updated successfully");
//If you want to update some part of your page, you can do it here.
//Ex : Getting the updated content from an ajax call and update UI
}
else
{
alert("Error updating");
}
});
});
});
确认方法是否通过ajax调用?如果没有,那么在Confirm方法调用之后您希望看到什么?取消按钮在哪里?我不是通过ajax调用确认方法,我使用Html.Actionlink。调用确认方法后,我想刷新页面,包括keep PartialView.Thank,但RedirectToAction方法不刷新页面并保留我的PartialView。我通过Ajax调用的PartialView,不包括索引页。
$(function(){
$("#confirmLink").click(function(e){
e.preventDefault();
_this=$(this);
$.post(_this.attr("href"),function(res){
if(res.status==="success")
{
alert("Updated successfully");
//If you want to update some part of your page, you can do it here.
//Ex : Getting the updated content from an ajax call and update UI
}
else
{
alert("Error updating");
}
});
});
});