简体   繁体   中英

how to call jquery function from controller and from jquery to controller

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:

  • Using client-side validation (jQuery has a validation plugin)
  • Using AJAX requests to validate before POSTing, rather than doing it afterwards
  • Splitting this action into two

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:

  • The simplest is to put your code in the $(document).ready() and it'll run when the page is loaded.
  • To run code at a time other than page load, have the client periodically check with the server (via AJAX) whether or not it should run some code. When you want to "call" the client-side code, have the server reply with something that means "yes", and then when the client gets that response, it runs the code. The problem with this method is that it requires the page to keep sending requests, which eats bandwidth and processing power and is generally a bad idea for big stuff, although you'll see it used a fair bit for requests that involve a very small amount of data and/or are fairly infrequent.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM