编辑:我有成功刷新页面的方法,但它只创建了2次调用我的控制器方法,这是不必要的,页面在第一次调用时不会更新。任何人都可以帮助在第一次通话时更新页面吗?
现在我是Ajax调用的新手,所以我不知道如何在执行我的控制器方法后刷新我的视图或Webgrid。 而且我也无法在Ajax中查找是否调用了success方法。 我在每个Ajax调用中都面临这个问题,为什么在控制器方法返回时不会更新View。
我所做的是,有DropDownlist
,并且在选择值并提交时会显示webgrid
。
Webgrid
有复选框,选中复选框并点击提交将一些数据通过AJax传递给控制器方法。数据在DataBase 中更新,但它不会反映在视图中直到我刷新它。
如何自动刷新视图?
任何帮助都将不胜感激。
我有View
如下:
$("#save_btn").on("click", function () {
var ischecked = 0;`enter code here`
$('#tblFormentry').find("input:checkbox").each(function () {
if (this.checked) {
var chck = $(this).attr("value");
var hidden = $(this).closest('td').find(':hidden');
villcode = villcode + "," + hidden.val();
singlestring = singlestring + "," + chck;
}
});
if ((singlestring != null && singlestring != "") && (villcode != null && villcode != "")) {
alert(singlestring);
if (confirm("Are you sure?") == true) {
$.ajax({
url: "@Url.Action("getmultipleids", "Admin")",
contentType: "application/json; charset=utf-8",
data: { 'ids': singlestring, 'villcode': villcode },
type: "GET",
cache: false,
success: function (result) {
$("body").html(result);
singlestring = "";
villcode = "";
},
failure: function (errMsg) {
alert(errMsg);
}
});
}
else {
singlestring = "";
}
}
else {
alert("Please select at least 1 Entry!");
}
});
<div class=" col-md-10">
@using (Html.BeginForm("GetGridData", "Admin", FormMethod.Post, new { @id = "verifyentry_form", role = "form" }))
{
@*@Html.ValidationSummary(false, "", new { @class = "text-danger" })*@
if (!string.IsNullOrEmpty(Convert.ToString(TempData["ErrorMessage"])))
{
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<strong>Error! </strong>@TempData["ErrorMessage"].ToString()
<div style="display:none">@TempData.Remove("ErrorMessage")</div>
</div>
}
if (!string.IsNullOrEmpty(Convert.ToString(TempData["Message"])))
{
<div class="alert alert-success alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<strong></strong>@TempData["Message"].ToString()
<div style="display:none">
@TempData.Remove("Message")
</div>
</div>
}
<div class="panel-default box box-primary">
<div class="panel-heading">
<h2 class="panel-title">Verify Entry</h2>
</div>
<div class="panel-body">
<div class="row">
<div class="form-group">
@Html.LabelFor(model => model.agency_id, new { @class = "control-label" })
@*@Html.DropDownListFor(m => m.DistID, (SelectList)TempData["Districts"], "Select", new { @class = "form-control" })*@
@Html.DropDownListFor(model => model.agency_id, (SelectList)@ViewBag.Agencylist, "Select", new { @class = "form-control", @id = "ddID" })
</div>
</div>
<div class=" row ">
<input type="button" name="submitbtn" value="Submit" class="btn btn-primary" id="databtn" />
</div>
</div>
</div>
}
<div class="box box-primary" id="ajaxdiv">
@if (ViewBag.formentry != null)
{
int PageSizeValue = 1;
//if (!string.IsNullOrEmpty(Convert.ToString(Session["Tbl_TraMstPageSizeValue"])))
//{
// PageSizeValue = Convert.ToInt32(Session["Tbl_TraMstPageSizeValue"]) > 0 ? Convert.ToInt32(Session["Tbl_TraMstPageSizeValue"]) : 1;
//}
WebGrid grid = new WebGrid(ViewBag.formentry, ajaxUpdateContainerId: "ajaxdiv", rowsPerPage: 10, ajaxUpdateCallback: "DetailsUpdate");
@grid.GetHtml(
htmlAttributes: new { id = "tblFormentry" },
tableStyle: " table table-bordered table-hover dataTable", headerStyle: "",
mode: WebGridPagerModes.All,
firstText: "<<",
lastText: ">>",
nextText: ">",
previousText: "<",
columns: new[]{
@*grid.Column(header:"{checkall}",format:@<text>
<input type="checkbox" name="Tbl_TraMstId" value="@item.agency_id" onclick="CheckClick(this)" />
</text>,style:"TableCheckBoxStyle",canSort:false),*@
grid.Column("district_name","District Name",canSort:false),
grid.Column("block_name","Block Name",canSort:false),
grid.Column("village_name","Village Name", canSort:false),
grid.Column("name","Name", canSort:false),
grid.Column("formno","Form No", canSort:false),
grid.Column("agency_name","Agency Name", canSort:false),
//grid.Column("Verify", format: (item) => item.GetSelectLink(item.formno)),
grid.Column("Verify",format:@<text>
<form method="post" action="" id="chckboxform">
<input type="hidden" name="village_code" value="@item.village_code" />
<input type="checkbox" name="formno" value="@item.formno" id="chckbox1" />
</form>
</text>,style:"tablebutton",canSort:false)
})
}
</div>
<input type="button" name="submit" value="Submit" id="save_btn" class="btn btn-primary" />
<input type="button" name="reset" value="Reset" id="reset_btn" class="btn btn-primary" />
以下是在jQuery AJax调用的帮助下调用的控制器方法。
public ActionResult getmultipleids(string ids, string villcode)
{
//need verificatN date, verified, verification user = admin, formno, villagecode
int i;
string[] formno = ids.Split(',');
string[] village_code = villcode.Split(',');
DataTable dt = new System.Data.DataTable("Entries");
DateTime today = DateTime.Today;
DataRow dr;
DataColumn dc;
dt.Columns.AddRange(new DataColumn[5] {
new DataColumn("verification_date", typeof(DateTime)),
new DataColumn("verified", typeof(Char)),
new DataColumn("verification_user", typeof(string)),
new DataColumn("formno", typeof(Int64)),
new DataColumn("village_code", typeof(string))
});
for (i = 1; i < formno.Count(); i++)
{
dr = dt.NewRow();
dr["verification_date"] = today;
dr["verified"] = 'Y';
dr["verification_user"] = "admin";
dr["formno"] = Int64.Parse(formno[i]);
dr["village_code"] = village_code[i];
dt.Rows.Add(dr);
}
try
{
actionResult = objfamilyBAL.Update_Entry(dt);
TempData["Message"] = actionResult.Message;
ViewBag.Agencylist = (SelectList)Session["ddlist"];
if (ShowResultMessage(actionResult, true, false))
{
int op = 1;
int agncyid = (int)Session["agency_id"];
String WorkArea = Session["wrkArea"].ToString();
actionResult = objfamilyBAL.Fetch_verify_griddata(op, agncyid, WorkArea);
if (actionResult.IsResult)
{
List<CommonTableEntity> family_entitylist = (List<CommonTableEntity>)actionResult.ObjResult;
ViewBag.formentry = family_entitylist;
}
}
if (actionResult.IsErrorMessage)
{
TempData["ErrorMessage"] = actionResult.ErrMessage;
}
}
catch (Exception ex)
{
ModelState.AddModelError("", ex.Message);
}
return View("VerifyEntry");
}
很抱歉很长的帖子。