我有一个剃须刀页面,如果模型状态无效,则会在回发时触发jQuery验证。
现在我有一个自定义函数或.js文件,我需要在回发时重新加载或触发1个函数,该怎么做?
无法直接找到答案,因为大多数人都说需要服务器端代码才能触发它。请提出建议。
如何调用“ callFN();”回发。
<html>
<head>
<script src="~/lib/jquery/dist/jquery.js"></script>
<script src="~/lib/jquery-validation/dist/jquery.validate.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>
</head>
<form method="post">
<input type="hidden" asp-for="AppUser.Id" />
<input type="hidden" asp-for="pageRet" />
<div class="form-group">
<label class="control-label">First Name</label>
<input asp-for="user.FName" class="form-control required" />
<span asp-validation-for="user.FName" class="text-danger"></span>
</div>
<div class="form-group">
<label class="control-label">Last Name</label>
<input asp-for="user.LName" class="form-control required" />
<span asp-validation-for="user.LName" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</form>
<script>
$(document).ready(function () {
function callFN() {
alert("My Call");
}
//called on page load, but how to call this on postback
callFN();
});
</script>
此处要求的是控制器代码:
[Authorize(Roles = "Administrator")]
public class EditUserModel : PageModel
{
private readonly ApplicationDbContext _context;
private readonly UserManager<AppUser> _userManager;
[BindProperty]
public string pageRet { get; set; }
[BindProperty]
public List<HostelUserHistory> huHistory { get; set; }
public EditUserModel(ApplicationDbContext context, UserManager<AppUser> userManager)
{
_context = context;
_userManager = userManager;
}
public async Task<IActionResult> OnGetAsync(string id)
{
huHistory = new List<UserHistory>();
pageRet = HttpContext.Request.Query["ret"].ToString();
if (id == null)
{
return NotFound();
}
huHistory = await _context.UserHistory
.Where(h => h.AppUserId == AppUser.Id)
.ToListAsync();
ViewData["huHistory"] = huHistory;
return Page();
}
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
//do some other thing
return Redirect(pageRet);
}
}