Let me start off by saying that I an new to Core 2.0 as well as MVC so please forgive my ignorance
I have used webforms for many years and would use AJAX to handle this, but I have had a hard time finding the answer for MVC.
I believe the issue I am having is that when I click the button on the page, that it is doing a full post and not using Ajax to partially post. The only reason this makes a difference is that on this login screen I have the login form fly down on page load.
<script type="text/javascript">
$().ready(function () {
setTimeout(function () { $('.card').removeClass('card-hidden'); }, 700)
});
</script>
If the user were to enter incorrect information and I have to display an error, the login form will once again fly in from the top.
So my question is two fold.
For starters is there any easy way to stop it from floating in on a postback? With webforms I could access the HTML directly in the code behind and remove the class before I return. Maybe I could do something like adding a state to my ViewModel and then I could switch the class based on that in Razer.
Alternatively it would be nice to be able to use AJAX like I am able to do in webforms as I am sure I will have additional more complex UI issues associated with page loading and current status of elements. Does anyone know of a good resource for this?
RECAP
- Using Asp.net Core 2.0 MVC
- Issue with the JQuery ready method firing on postback
- This causes my login form to fly in again
- Am I going to have to hack the UI as in item 1
- Should I focus on AJAX and what would be a good resource based on my needs.
Sample page: http://demos.creative-tim.com/material-dashboard-pro/examples/pages/login.html
Solution
First off a big thanks to David Lang for pointing me in the right direction. Between his post and the link I was able to work this out.A word of caution, much of my issues were caused by a nuget package not installing correctly. The Microsoft unobtrusive Ajax installed, but because I didn't have a folder called scripts, it never put the JS files in my project. Once I got those in my project the rest became easier.
Start by adding the script for the jQuery unobtrusive to your page.
<script src="~/js/jquery.unobtrusive-ajax.min.js"></script>Add the minimum form tags
<form asp-action="CheckPassword" data-ajax="true" data-ajax-method="post" data-ajax-success="onSuccess" >In your controller check your data, and in my case return a viewModel (not a view)
[HttpPost] public ActionResult CheckPassword([Bind("userName, password")] LoginVM loginModel) { if (loginModel.userName == "somename" && loginModel.password == "1234") { loginModel.invalidUser = false; return Ok(loginModel); } else { loginModel.invalidUser = true; return Ok(loginModel); } }Create JS function to do something with the returned data.
var onSuccess = function (context) { var errorMessage = $("#errorMessage"); errorMessage.html(""); if (context.invalidUser) { errorMessage.html("Invalid Credentials"); } else { errorMessage.html("Valid user!"); } };