3
I have this javascript function.
function loginUsuario(){
alert();
if(($('#txtUsuario').val() != "") && ( $("#txtSenha").val() != ""))
resultado = JQuery.parseJSON('{"Usuario": "' + $('#txtUsuario').val() + '", "Senha": "' + $("#txtSenha").val() + '}');
else
return;
$.ajax({
url: '/Home/loginUsuario',
datatype: 'json',
contentType: "application/json; charset=utf-8",
type: "POST",
data: JSON.stringify({ loginUsuario: resultado }),
success: function (data) {
alert('Login realizado com sucesso!!');
},
error: function (error) {
loading(0, "");
}
});
}
and I have this action in the controller still under development
public ActionResult loginUsuario(LoginUsuario loginUsuario)
{
V99_WEB_QAEntities db = new V99_WEB_QAEntities();
EncriptyDecripty decripta = new EncriptyDecripty();
try
{
var resultado = (from log in db.T_Usuario
where log.Login == loginUsuario.Usuario && decripta.Decrypt(log.Pswd) == loginUsuario.Senha
select new { log.Nome }).FirstOrDefault();
}
catch (Exception ex)
{
string erro = ex.Message;
}
return View(loginUsuario);
}
It turns out that when I click the button to log in, nothing happens. There is no error and I realize that the form is sent, but it does not call the JS function and therefore does not call the action. What else should I do? Below is my CSHTML.
<form method="post" action="@Action" id="frmLogin">
<table style="margin-top:10px;">
<tr>
<td>
Usuário:
</td>
<td>
<input type="text" name="txtUsuario" id="txtUsuario" maxlength="20" class="" />
</td>
</tr>
<tr>
<td>
Senha:
</td>
<td>
<input type="password" name="txtSenha" id="txtSenha" maxlength="10" class="" />
</td>
</tr>
<tr>
<td>
@*<input type="submit" name="btnEntrar" id="btnEntrar" value="Entrar" onclick="loginUsuario();"/>*@
<button name="btnEntrar" id="btnEntrar" value="Entrar" onclick=" loginUsuario();">Entrar</button>
</td>
</tr>
</table>
</form>
A question. When I changed the action value in the form, it gave me this error:
Compiler Error Message: CS0118: 'System.Action' is a 'type' but is used like a 'variable'
Source Error:
Line 19: <fieldset>
Line 20: @*<legend>Informe seus dados</legend>*@
Line 21: <form method="post" action="@Action" id="frmLogin">
Line 22: <table style="margin-top:10px;">
Line 23: <tr>
Should I keep action="/Home/Minha_action" or can that be the case? For in another example in another company that I was, my friend did so as above and worked and gives me the error.
I put in Fidlle and gave me that mistake there:
{"error": "Shell form does not validate{'html_initial_name': u'initial-js_lib', 'form': <mooshell.forms.ShellForm object at 0x2145890>, 'html_name': 'js_lib', 'html_initial_id': u'initial-id_js_lib', 'label': u'Js lib', 'field': <django.forms.models.ModelChoiceField object at 0xfcc210>, 'help_text': '', 'name': 'js_lib'}{'html_initial_name': u'initial-js_wrap', 'form': <mooshell.forms.ShellForm object at 0x2145890>, 'html_name': 'js_wrap', 'html_initial_id': u'initial-id_js_wrap', 'label': u'Js wrap', 'field': <django.forms.fields.TypedChoiceField object at 0xfcc350>, 'help_text': '', 'name': 'js_wrap'}"}
added javascript libraries to my cshtml and continues to give the error: $ is not defined
– pnet
Your jQuery is installed?
– Guilherme Oderdenge
yes: <script src="@Url.Content("~/Scripts/jquery-1.10.2.js")"></script> <script src="@Url.Content("~/Scripts/jquery-1.10.2.min.js")"></script> <script src="@Url.Content("~/Scripts/jquery-2.1.0.js")"></script> <script src="@Url.Content("~/Scripts/jquery-2.1.0.min.js")"></script> <script src="@Url.Content("~/Scripts/jquery-migrate-1.2.1.js")"></script> <script src="@Url.Content("~/Scripts/jquery-migrate-1.2.1.min.js")"></script> <script src="@Url.Content("~/Scripts/jquery-ui.js")"></script> <script src="@Url.Content("~/Scripts/jquery-ui.min.js")"></script>
– pnet
Here’s the thing, I moved the Home/Home.js line of script to the end and I didn’t get the bug anymore. I didn’t know that until then. But still not in my way.
– pnet
What error the console displays, @pnet?
– Guilherme Oderdenge