i'm trying post form using c#
i make searches, couldn't code right way (i new in field).
here codes;
view;
<form> <div class="field-wrap"> <label> email address<span class="req">*</span> </label> <input type="email" id="input-username" name="username" required autocomplete="on" /> </div> <div class="field-wrap"> <label> password<span class="req">*</span> </label> <input type="password" id="input-password" name="password" required autocomplete="on"/> </div> <p class="forgot"><a href="#">forgot password?</a></p> <button class="button button-block" id="button-login">log in</button> </form>
controller;
// get: user [httppost] public actionresult login() { string username = session["username"].tostring(); string password = session["password"].tostring(); service ilocationservice = new service(); var result = service.membergetlogin( username, password, "127.0.0.1" ); viewbag.message = result; return view(); }
javascript;
jquery(document).ready(function () { $("#button-login").click(function () { $.ajax({ type: "post", url: "/controllers/usercontroller/login/", data: $(this).serialize(), datatype: "json" }) .done(function (result) { console.log(result); }) .fail(function (a) { console.log( a); }); });
});
what trying post input values chech user.
thanks in advance
look @ line
string username = session["username"].tostring();
in code trying read username , password values session variables. set user name , password session ? should reading posted form , use that.
[httppost] public actionresult login(string username,string password) { // username , password , return }
also, need make sure serializing form, not button clicked. prefer use html helper method generate form tag , use action attribute value of form in javascript code instead of hardcoding urls.
so in razor view
@using(html.beginform("login","user")) { //your existing form inputs goes here <button class="button button-block" id="button-login">log in</button> }
and in script
$("#button-login").click(function () { $.ajax({ type: "post", url: $(this).closest("form").attr("action"), data: $(this).closest("form").serialize() }) });
since doing ajax form submit, suggest return json response client code can parse , further things.
[httppost] public actionresult login(string username,string password) { //if username , password valid return json(new { status="success"}); // else return json(new { status="failed", message="invalid credentials}); }
and in done callback, should inspect value , further things
.done(function (result) { if(result.status==="success") { window.location.href="/home/index"; // change wherever want redirect } else { alert(result.message); } })
Comments
Post a Comment