c# - How To Perform Remote Validation in MVC4 Without Using Entity Framework -
i have developed web application using mvc without entity framework. in application, want perform remote validation check whether user name exist or not.i read many blogs remote validation. in of blogs use entity framework data model whereas wish use sql command that. can't achieve desired validation.
here code: model,controller,view code.
mode:
public class remotevalidation { [remote("validateusername","test",errormessage="username exist")] public string username { get; set; } public string password { get; set; } }
controller:
[httpget] public actionresult remotevalidation() { return view(); } [httppost] public actionresult remotevalidation(remotevalidation rv) { if (modelstate.isvalid) { sqlconnection conn = new sqlconnection(connstring); conn.open(); sqlcommand cmd = new sqlcommand("insert adminsignup (username,password) values ('"+rv.username+"','"+rv.password+"')", conn); cmd.executenonquery(); conn.close(); } return view(); } public jsonresult validateusername(string username) { sqlconnection conn = new sqlconnection(connstring); sqlcommand cmd = new sqlcommand("select*from adminsignup username='"+username+"'", conn); conn.open(); sqldatareader dr = cmd.executereader(); while (dr.read()) { if (dr.hasrows == true) { return json("user name exist", jsonrequestbehavior.allowget); } } return json(true, jsonrequestbehavior.allowget); }
view:
@model testmvc.models.remotevalidation
<body> @using (html.beginform()) { <div> @html.textboxfor(m=>m.username) @html.validationmessagefor(x=>x.username) </div> <div> @html.passwordfor(m=>m.password) @html.validationmessagefor(x=>x.password) </div> <div> <input type="submit" value="check" /> </div> } </body>
i don't know why can't output 1 please me , suggest how acheive this..
have configured web.config enable validation ?
<appsettings> <add key="clientvalidationenabled" value="true" /> <add key="unobtrusivejavascriptenabled" value="true" /> </appsettings>
use browsers developer tools see http traffic generated.
please @ parameterised queries , sqlcommand - never hand build sql strings in web sites - opens sql injection attacks.
Comments
Post a Comment