hi guyz how can i call the jquery function in my registernewacct.cshtml
from my homecontroller.cs
. and back and fort....
my code in homecontroller.cs
[AcceptVerbs(HttpVerbs.Get)]
private bool IsValidRegisterNewAcct(string acctname, string dispname, string email, string password)
{
var crypto = new SimpleCrypto.PBKDF2();
bool IsValid = false;
IsValidErrorMessage = "";
IsValidErrorMessageInt = 0;
string NewAcctNameHash = MD5(acctname);
string NewEmailHash = MD5(email);
try
{
using (var db = new MainDbContext())
{
var NewAcctName = db.user.FirstOrDefault(u => u.AcctNameSalt == NewAcctNameHash);
if (NewAcctName != null)
{
IsValid = false;
IsValidErrorMessage = "Account Name already Exist!";
}
else
{
var NewEmail = db.user.FirstOrDefault(u => u.EmailSalt == NewEmailHash);
if (NewEmail != null)
{
IsValid = false;
IsValidErrorMessage = "Email Address already Exist!";
}
else
{
//THIS AREA <-- I wan to call my jquery function in my registernewacct.cshtml which is the dialog confirmation will ask if save or not. now if the answer is yes then it will return value yes. if not then it will return not.
var InsertNewAcctName = db.user.CreateObject();
InsertNewAcctName.UserId = Convert.ToString(Guid.NewGuid());
InsertNewAcctName.AcctName = acctname;
InsertNewAcctName.AcctNameSalt = NewAcctNameHash;
InsertNewAcctName.DisplayName = dispname;
InsertNewAcctName.Email = email;
InsertNewAcctName.EmailSalt = MD5(email);
InsertNewAcctName.Password = crypto.Compute(password);
InsertNewAcctName.PasswordSalt = crypto.Salt;
InsertNewAcctName.UserLevel = 2;
db.osoa_user.AddObject(InsertNewAcctName);
db.SaveChanges();
IsValid = true;
}
}
}
}
this is the code in my registernewacct.cshtml
@section Scripts{
<script>
$(document).ready(function(){
//THIS IS FOR THE CANCEL ACCOUNT DIALOG
function OpenDialogConfirmation(){
$( "#SaveDialogPage" ).dialog( "open" );
}
$("#yessavedialog").click(function(e) {
$( "#SaveDialogPage" ).dialog( "close" );
//then the code here that will pass my value back to homecontroller.cs....
});
$("#cancelsavedialog").click(function(e) {
$( "#SaveDialogPage" ).dialog( "close" );
//then the code here that will pass my value back to homecontroller.cs....
});
});
</script>
}
can you give me some advice? thanks a lot
It might help you to read up a little to read up on the differences between server-side and client-side code; they're not exactly interchangeable, and you might run into some logic issues if you assume you can call back and forth at will. Your C# code sample shows that you're trying to get confirmation from the user in the middle of a controller method - this is NOT how controller methods should work. If you want this, you should probably consider:
Having said that, there are places where you'd want to pass control from one to the other, so here's how:
To call server-side code (eg. controller methods) from the client:
In a word, AJAX. You can use jQuery's AJAX support or ASP.NET MVC's Ajax.Action
and other AJAX helpers. However you do it, you basically need to POST the data to a URL that is routed to your controller method. By default, that'll be www.example.com/{controller}/{method}/{param}
where the param
is optional. Name the attributes of your POST request's JSON data payload the same as the parameters that your controller method is expecting. Get that method to return a JSON object (there's a built-in Json()
) conversion) and that JSON object is what will be passed into the AJAX success handler.
To call client-side code from the server:
Not strictly possible; you can't really call directly but there are ways to trigger code on the client:
$(document).ready()
and it'll run when the page is loaded. Call from client-side to server with Jquery Ajax request. From server to client-side you cant because there is no javascript function client-side while you're at server side. You need to understand the differences between server and client side languages.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.