I’m working in a web project and I decide to turn in MVC 4 from ASP.NET.
After many issues which I face it, I’m in a position to handle the events raised from the program.
In my previous environment ASPX.NET I was double clicked on a <asp:Button.../>
control and automatically in the code behind I see the related procedure.
And of course every time I hit the button the program goes to this procedure, and the related event click
.
Now in MVC 4 I following the same process, but when I’m trying to hit the button the program never goes to the procedure.
Another example is, when I have a Label control and I want to make it visible or Hidden then I use the
labelId.style.add(“Visibility”,”Hidden”)
or “Visible”.
And another one is:
ValidationSummary.ValidationGroup
Now in MVC happen the same but before I use the controller.
(The reason to use the controller is that I want to go to the asp:button
on_click procedure since the program refuse to go through the event.)
After the controller process the Label control is evaluated but it is completely empty, and with an empty control we can’t do anything.
The same happen in ValidationSummary
is empty and then it threw an error
What I have done until now
When I use the inheritance web.UI.Page
the program throws me this error:
The view at '~ / Views / Home / Index.aspx' must derive from ViewPage
So I change the inheritance to ViewPage
And I use the following Javescript to call the controller:
<script type="text/javascript">
function logSubmit() {
var url = 'loginSubmit';
$.ajax({
type: 'GET',
url: url,
dataType: 'json',
});
}
</script>
And from controller I use these instructions in the function:
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
When that happen not any control works from my page.aspx
Is there someone to assist me on this issue?
aspx page must specify to inherit from the code-behind base class. To do this, use the Inherits attribute for the @ Page directive. The . aspx page inherits from the code-behind class, and the code-behind class inherits from the Page class.
The advantages brought by the code-behind model are clear and to a large extent also unquestionable. You deal with classes and integrate your own classes with the existing framework. The code and layout physical separation enable graphic designers and page developers to work on the same resource at the same time.
As said on the comments Asp.net web forms and Asp.Net MVC are a totally different concept. I know you know that. Asp.net Web forms work on code behind concept whereas Asp.Net MVC has route-based URLs means URLs are divided into controllers and actions and moreover it is based on controller not on physical file. In web forms, there are server controls but Asp.Net MVC follow customizable syntax (Razor as default). And again as said on comments you cannot change System.Web.UI.Page with System.Web.Mvc.ViewPage in existing ASPX web forms page. So you need to have your views using a razor syntax. From your question what I can suggest is like below.
// 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
And in the controller.
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
And the model is.
//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
I just described the way to handle your issue to my understanding from your question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With