Asp.net mvc 如何基于模型值调用脚本
我有以下登录时使用的代码,它可以正常工作。但是,如果登录成功,我需要关闭表单。我有一个按钮,用户可以点击它,但我不知道如何连接它,这样我就可以通过编程调用它。该模型有一个名为IsAuthenticated的属性。如果这是真的,那么我需要调用cancelLogin()函数来关闭窗口Asp.net mvc 如何基于模型值调用脚本,asp.net-mvc,Asp.net Mvc,我有以下登录时使用的代码,它可以正常工作。但是,如果登录成功,我需要关闭表单。我有一个按钮,用户可以点击它,但我不知道如何连接它,这样我就可以通过编程调用它。该模型有一个名为IsAuthenticated的属性。如果这是真的,那么我需要调用cancelLogin()函数来关闭窗口 @using (Ajax.BeginForm("Login", "Account", new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTar
@using (Ajax.BeginForm("Login", "Account", new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "loginSection", }))
{
@Html.Partial("_LoginInfoPartial", Model)
<input} type="submit" value="Log in" />
<button type="button" id="close_button" onclick="cancelLogin()" >Cancel</button>
<script>
function cancelLogin()
{
var window = $("#loginWindow").data("kendoWindow");
window.close();
}
</script>
}
@使用(Ajax.BeginForm(“Login”、“Account”、新的AjaxOptions{InsertionMode=InsertionMode.Replace、UpdateTargetId=“loginSection”,}))
{
@Html.Partial(“\u logininfoportial”,Model)
取消
函数cancelogin()
{
var窗口=$(“#loginWindow”).数据(“kendoWindow”);
window.close();
}
}
成功登录后,您可以从控制器内部返回对该函数的调用。例如:
public ActionResult Login() {
// login logic here
if(loginSuccess)
return Content("<script>cancelLogin();</script>");
else
return View();
}
public ActionResult登录(){
//这里的登录逻辑
如果(登录成功)
返回内容(“cancelLogin();”;
其他的
返回视图();
}
因此,当表单从后端加载时,它将使用此Javascript替换您的
div
,该Javascript将执行并关闭窗口。成功登录后,您可以从控制器内部返回对该函数的调用。例如:
@using (Ajax.BeginForm("Login", "Account", new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "loginSection", }))
{
@Html.Partial("_LoginInfoPartial", Model)
<input} type="submit" value="Log in" />
<button type="button" id="close_button" onclick="cancelLogin()" >Cancel</button>
<script>
function cancelLogin()
{
var window = $("#loginWindow").data("kendoWindow");
window.close();
}
</script>
}
public ActionResult Login() {
// login logic here
if(loginSuccess)
return Content("<script>cancelLogin();</script>");
else
return View();
}
public ActionResult登录(){
//这里的登录逻辑
如果(登录成功)
返回内容(“cancelLogin();”;
其他的
返回视图();
}
因此,当表单从后端加载时,它将使用此Javascript替换您的div
,该Javascript将执行并关闭窗口
@using (Ajax.BeginForm("Login", "Account", new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "loginSection", }))
{
@Html.Partial("_LoginInfoPartial", Model)
<input} type="submit" value="Log in" />
<button type="button" id="close_button" onclick="cancelLogin()" >Cancel</button>
<script>
function cancelLogin()
{
var window = $("#loginWindow").data("kendoWindow");
window.close();
}
</script>
}