Home > Mobile >  How to transform a object from action to view
How to transform a object from action to view

Time:12-08

I want transform a data who live in field from a action to view and Consider we cant use Viewbag

this is my action

   public ActionResult RenderOnetimePassword(SignInModel model)

        {


            var selectedCountry = _countryCallingCodeService.GetCountryCallingCodeByTwoLetterIsoCode(model.SelectedCountryTwoLetterIsoCode);
            var callingCode = selectedCountry.Code;
            var credential = model.CredentialType == CredentialType.Email ? model.Credential : callingCode   model.Credential;
            var customerfor = _customerService.GetCustomerByMobile(credential);
            if (customerfor == null)
                throw new ArgumentException("No customer found with the specified id");

            if (selectedCountry == null)
                return Json(new
                {
                    success = false,
                    message = _localizationService.GetResource("Account.PhoneNumberConfirmation.NoCountryFound")
                });



            var verificationCodeJsonResult = GenerateVerificationCode(customerfor);
            if (verificationCodeJsonResult != null)
                return verificationCodeJsonResult;


            var isSent = MessagingHelper.SendVerificationCodeMessageToCustomer(customerfor, credential, _code, selectedCountry,
                   _settingService, _tokenizer, _smsInterfaceService, _localizationService, _queuedSMSService, _logger);

            var signInModel = new SignInModel()
            {
                Credential = credential,
                CredentialType = CredentialType.PhoneNumber,
                DisplayCaptcha = _captchaSettings.Enabled && _captchaSettings.ShowOnLoginPage,
                WaitingTimeForNextResendCodeRequestInSecond = _phoneAuthenticationSetting.WaitingTimeForNextResendCodeRequestInSeconds

            };


            return Json(new
            {
                success = true,
            

            });
        }

so I Dont want you you read all action just a field who live in signinmodel I need first time this filed give data WaitingTimeForNextResendCodeRequestInSecond in RenderOnetimePassword Action and then we transform data to view and in ajax Process we get data for some function who need this data

@model SignInModel     
$(document).on('click', function  () {
        $("#PassCode").click(function () {

            data = { Credential: $("#Credential").val(), SelectedCountryTwoLetterIsoCode: $("#SelectedCountryTwoLetterIsoCode").val() };
            var count =@Model.WaitingTimeForNextResendCodeRequestInSecond;
    var counter;

    function timer() {
        count = count - 1;
        if (count <= 0) {
            clearInterval(counter);
            $('.remainingTimeForResendCodeRequest').hide();
            $('#PassCode').show();
            return;
        }
        $('.remainingTimeForResendCodeRequest').text('@T("Account.SMSCodeVerification.RequestForResendCode")'   " "   count   " "   '@T("Account.SMSCodeVerification.Seconds")')
    }

    $(document).ready(function () {
        counter = setInterval(timer, 1000);
        $("#Code").trigger("focus");
    });

        $.ajax({
            cache: false,
            url: '@Url.Action("RenderOnetimePassword", "Customer")',
            data: data,
            type: 'post',
            beforeSend: function () {
                $("#Password").addClass("loading");
            },
            success: function (response) {
                if (!response.success)
                    showError(response.message, response.captcha_string);
                else if (response.url)
                    window.location.href = response.url;
                else if (response.update_section)
                    $("."   response.update_section.name).html(response.update_section.html);

            },
            success: function (response) {
             

                $('#PassCode').hide();
                $('.remainingTimeForResendCodeRequest').text('@T("Account.SMSCodeVerification.RequestForResendCode")'   " "   count   " "   '@T("Account.SMSCodeVerification.Seconds")')
                $('.remainingTimeForResendCodeRequest').show();
                if (!response.success) {
                    validator.showErrors({
                        "Code": response.message
                    });
                }
            },

CodePudding user response:

you can send your result in your action to your function of view like that

  return Json(new
            {
                success = true,
                somthing = <somthing u want send to your view >

            }) ;

and in your view call your result in your function

 success: function (response) {

                somevaribale =   response.somthing
                $('#PassCode').hide();
                $('.remainingTimeForResendCodeRequest').text('@T("Account.SMSCodeVerification.RequestForResendCode")'   " "   count   " "   '@T("Account.SMSCodeVerification.Seconds")')
                $('.remainingTimeForResendCodeRequest').show();
                if (!response.success) {
                    validator.showErrors({
                        "Code": response.message
                    });
                }
            },

I hope its work for you :)

  • Related