page.aspx和code behind(page.aspx.vb)关系问题
page.aspx and code behind (page.aspx.vb) relations problems
我在一个 Web 项目中工作,我决定从 ASP.NET 转入 MVC 4。
在遇到很多问题后,我可以处理程序引发的事件。
在我以前的环境 ASPX.NET 中,我双击了一个 <asp:Button.../>
控件,并在后面的代码中自动看到了相关过程。
当然,每次我按下按钮时,程序都会转到此过程,以及相关事件 click
.
现在在 MVC 4 中,我遵循相同的过程,但是当我试图点击按钮时,程序永远不会进入过程。
另一个例子是,当我有一个 Label 控件并且我想让它可见或隐藏时,我使用
labelId.style.add(“Visibility”,”Hidden”)
或“可见”。
另一个是:
ValidationSummary.ValidationGroup
现在在 MVC 中发生相同但在我使用控制器之前。
(使用控制器的原因是我想去 asp:button
on_click 程序,因为程序拒绝通过事件。)
在控制器处理之后,对 Label 控件求值,但它完全是空的,对于空控件我们什么也做不了。
同样的事情发生在 ValidationSummary
是空的然后它抛出一个错误
到目前为止我做了什么
当我使用继承 web.UI.Page
时,程序会抛出这个错误:
The view at '~ / Views / Home / Index.aspx' must derive from ViewPage
所以我把继承改为ViewPage
我使用以下 Javescript 调用控制器:
<script type="text/javascript">
function logSubmit() {
var url = 'loginSubmit';
$.ajax({
type: 'GET',
url: url,
dataType: 'json',
});
}
</script>
从控制器我在函数中使用这些指令:
Public Function loginSubmit() As JsonResult
Dim routes As New RouteCollection With {.RouteExistingFiles = True}
Return Json(New With {Key Attributes.logSubmit.LoginButton_Click}, JsonRequestBehavior.AllowGet)
End Function
发生这种情况时,我的 page.aspx
无法进行任何控制
有人可以帮助我解决这个问题吗?
如评论所述Asp.net Web 表单和Asp.Net MVC 是一个完全不同的概念。我知道你知道。 Asp.net Web 表单基于概念背后的代码工作,而 Asp.Net MVC 具有基于路由的 URL,这意味着 URL 分为控制器和操作,而且它基于控制器而不是物理文件。在 Web 表单中,有服务器控件,但 Asp.Net MVC 遵循可自定义的语法(默认为 Razor)。再次如评论所述,您不能在现有 ASPX Web 表单页面中将 System.Web.UI.Page 更改为 System.Web.Mvc.ViewPage。所以你需要使用剃刀语法来获得你的观点。根据你的问题,我可以建议如下。
// View.
@ModelType ViewModel //Model you want to bind to the view.
@Using Html.BeginForm("Login", "Account", New With {.ReturnUrl = ViewBag.ReturnUrl}, FormMethod.Post) // we pass action name as first parameter and controller name as third parameter.
@Html.AntiForgeryToken()
@<text>
@Html.ValidationSummary(True, "", New With {.class = "text-danger"})
<div class="form-group">
@Html.LabelFor(Function(m) m.Email, New With {.class = "col-md-2 control-label"})
<div class="col-md-10">
@Html.TextBoxFor(Function(m) m.Email, New With {.class = "form-control"})
@Html.ValidationMessageFor(Function(m) m.Email, "", New With {.class = "text-danger"})
</div>
</div>
<div class="form-group">
@Html.LabelFor(Function(m) m.Password, New With {.class = "col-md-2 control-label"})
<div class="col-md-10">
@Html.PasswordFor(Function(m) m.Password, New With {.class = "form-control"})
@Html.ValidationMessageFor(Function(m) m.Password, "", New With {.class = "text-danger"})
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Log in" class="btn btn-default" />
</div>
</div>
</text>
End Using
并在控制器中。
Public Class AccountController
Inherits Controller
<HttpPost>
<AllowAnonymous>
<ValidateAntiForgeryToken>
Public Async Function Login(model As LoginViewModel, returnUrl As String) As Task(Of ActionResult)
If Not ModelState.IsValid Then
Return View(model)
End If
// Your code logic
Dim result = Await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout := False)
Select Case result
Case SignInStatus.Success
Return RedirectToLocal(returnUrl)
Case SignInStatus.LockedOut
Return View("Lockout")
Case SignInStatus.RequiresVerification
Return RedirectToAction("SendCode", New With {
returnUrl,
model.RememberMe
})
Case Else
ModelState.AddModelError("", "Invalid login attempt.") // Because of this we can show the validation summary on view using @Html.ValidationSummary
Return View(model)
End Select
End Function
End Class
模型是。
//In your case model class is different.
Public Class LoginViewModel
<Required>
<Display(Name:="Email")>
<EmailAddress>
Public Property Email As String
<Required>
<DataType(DataType.Password)>
<Display(Name:="Password")>
Public Property Password As String
End Class
我只是根据你的问题描述了我对你问题的处理方式。
我在一个 Web 项目中工作,我决定从 ASP.NET 转入 MVC 4。
在遇到很多问题后,我可以处理程序引发的事件。
在我以前的环境 ASPX.NET 中,我双击了一个 <asp:Button.../>
控件,并在后面的代码中自动看到了相关过程。
当然,每次我按下按钮时,程序都会转到此过程,以及相关事件 click
.
现在在 MVC 4 中,我遵循相同的过程,但是当我试图点击按钮时,程序永远不会进入过程。
另一个例子是,当我有一个 Label 控件并且我想让它可见或隐藏时,我使用
labelId.style.add(“Visibility”,”Hidden”)
或“可见”。
另一个是:
ValidationSummary.ValidationGroup
现在在 MVC 中发生相同但在我使用控制器之前。
(使用控制器的原因是我想去 asp:button
on_click 程序,因为程序拒绝通过事件。)
在控制器处理之后,对 Label 控件求值,但它完全是空的,对于空控件我们什么也做不了。
同样的事情发生在 ValidationSummary
是空的然后它抛出一个错误
到目前为止我做了什么
当我使用继承 web.UI.Page
时,程序会抛出这个错误:
The view at '~ / Views / Home / Index.aspx' must derive from ViewPage
所以我把继承改为ViewPage
我使用以下 Javescript 调用控制器:
<script type="text/javascript">
function logSubmit() {
var url = 'loginSubmit';
$.ajax({
type: 'GET',
url: url,
dataType: 'json',
});
}
</script>
从控制器我在函数中使用这些指令:
Public Function loginSubmit() As JsonResult
Dim routes As New RouteCollection With {.RouteExistingFiles = True}
Return Json(New With {Key Attributes.logSubmit.LoginButton_Click}, JsonRequestBehavior.AllowGet)
End Function
发生这种情况时,我的 page.aspx
有人可以帮助我解决这个问题吗?
如评论所述Asp.net Web 表单和Asp.Net MVC 是一个完全不同的概念。我知道你知道。 Asp.net Web 表单基于概念背后的代码工作,而 Asp.Net MVC 具有基于路由的 URL,这意味着 URL 分为控制器和操作,而且它基于控制器而不是物理文件。在 Web 表单中,有服务器控件,但 Asp.Net MVC 遵循可自定义的语法(默认为 Razor)。再次如评论所述,您不能在现有 ASPX Web 表单页面中将 System.Web.UI.Page 更改为 System.Web.Mvc.ViewPage。所以你需要使用剃刀语法来获得你的观点。根据你的问题,我可以建议如下。
// View.
@ModelType ViewModel //Model you want to bind to the view.
@Using Html.BeginForm("Login", "Account", New With {.ReturnUrl = ViewBag.ReturnUrl}, FormMethod.Post) // we pass action name as first parameter and controller name as third parameter.
@Html.AntiForgeryToken()
@<text>
@Html.ValidationSummary(True, "", New With {.class = "text-danger"})
<div class="form-group">
@Html.LabelFor(Function(m) m.Email, New With {.class = "col-md-2 control-label"})
<div class="col-md-10">
@Html.TextBoxFor(Function(m) m.Email, New With {.class = "form-control"})
@Html.ValidationMessageFor(Function(m) m.Email, "", New With {.class = "text-danger"})
</div>
</div>
<div class="form-group">
@Html.LabelFor(Function(m) m.Password, New With {.class = "col-md-2 control-label"})
<div class="col-md-10">
@Html.PasswordFor(Function(m) m.Password, New With {.class = "form-control"})
@Html.ValidationMessageFor(Function(m) m.Password, "", New With {.class = "text-danger"})
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Log in" class="btn btn-default" />
</div>
</div>
</text>
End Using
并在控制器中。
Public Class AccountController
Inherits Controller
<HttpPost>
<AllowAnonymous>
<ValidateAntiForgeryToken>
Public Async Function Login(model As LoginViewModel, returnUrl As String) As Task(Of ActionResult)
If Not ModelState.IsValid Then
Return View(model)
End If
// Your code logic
Dim result = Await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout := False)
Select Case result
Case SignInStatus.Success
Return RedirectToLocal(returnUrl)
Case SignInStatus.LockedOut
Return View("Lockout")
Case SignInStatus.RequiresVerification
Return RedirectToAction("SendCode", New With {
returnUrl,
model.RememberMe
})
Case Else
ModelState.AddModelError("", "Invalid login attempt.") // Because of this we can show the validation summary on view using @Html.ValidationSummary
Return View(model)
End Select
End Function
End Class
模型是。
//In your case model class is different.
Public Class LoginViewModel
<Required>
<Display(Name:="Email")>
<EmailAddress>
Public Property Email As String
<Required>
<DataType(DataType.Password)>
<Display(Name:="Password")>
Public Property Password As String
End Class
我只是根据你的问题描述了我对你问题的处理方式。