ASP.Net MVC: Calling a method from a view

前端 未结 8 1292
轻奢々
轻奢々 2020-11-29 02:52

In my MVC app the controller gets the data (model) from an external API (so there is no model class being used) and passes that to the view. The data (model) has a container

8条回答
  •  生来不讨喜
    2020-11-29 03:14

    why You don't use Ajax to

    its simple and does not require page refresh and has success and error callbacks

    take look at my samlpe

    @Resource_en.ResendVerificationCode
    

    and in JQuery

     $("#ResendVerificationCode").on("click", function() {
                    getUserbyPhoneIfNotRegisterd($("#phone").val());
     });
    

    and this is my ajax which call my controller and my controller and return object from database

    function getUserbyPhoneIfNotRegisterd(userphone) {
    
                  $.ajax({
                        type: "GET",
                        dataType: "Json",
                        url: '@Url.Action("GetUserByPhone", "User")' + '?phone=' + userphone,
                        async: false,
                        success: function(data) {
                            if (data == null || data.data == null) {
                                ErrorMessage("", "@Resource_en.YourPhoneDoesNotExistInOurDatabase");
                            } else {
                                user = data[Object.keys(data)[0]];
                                    AddVereCode(user.ID);// anather Ajax call 
                                    SuccessMessage("Done", "@Resource_en.VerificationCodeSentSuccessfully", "Done");
                            }
                        },
                        error: function() {
                            ErrorMessage("", '@Resource_en.ErrorOccourd');
                        }
                    });
                }
    

提交回复
热议问题