1
  public ActionResult GiveTicket(Guid voteId, Guid applyId,string cptcha)
    {       
        //檢查此票選是否允許此登入方式
        var canVoteWay = _voteService.GetVoteWay(voteId);

        string message = string.Empty;
        string loginPath = $"{ConfigurationManager.AppSettings["DomainName"]}/Account/Login?returnUrl={Request.UrlReferrer}";

        //檢查是否已登入
        if (User.Identity.IsAuthenticated && WebLogic.HasValue(canVoteWay, (int)CurrentUser.LoginType))
        {               
            // [驗證圖形驗證碼]
            if (string.IsNullOrEmpty(cptcha) || cptcha != Session["VerificationCode"]?.ToString())
            {
                Response.Write("<script language=javascript> bootbox.alert('圖形驗證碼驗證錯誤,請重新輸入!!')</script>");
                return null;
            }

            //var result = _voteService.GiveTicket(voteId, applyId, CurrentUser.Id, CurrentUser.LoginType);
            Response.Write("<script language=javascript> bootbox.alert('投票成功')</script>");
            return null;              
        }


        message = _voteService.VoteWayString(canVoteWay, "請先登入,才能參與投票!! 投票允許登入的方式:");
        Response.Write("<script language=javascript> if (confirm('" + message + "',callback:function(){})){window.location = '" + loginPath + "'}</script>");
        return null;
    }     

My ajax code

 function GiveTicket(applyId) {
        var voteId = $('input[name="Id"]').val();
        var captcha = $('input[name="Captcha"]').val();
        $.ajax({
            url: '@Url.Action("GiveTicket", "Vote")',
            data: { applyId: applyId, voteId: voteId, cptcha: captcha },
            type: 'Get',
            success: function (data) {
                console.log(data);

                //bootbox.alert(data);
            }
        });
    }

Like you see. I have many condition. SomeTime I need to pass alert or confirm to web client . when I pass confirm. if user click Yes. I need to redirect Url. So that I decide to write string to web client.

The problem is How I can just execute string from MVC like alert,confirm...

Leon Huang
  • 103
  • 11
  • 1
    You can return JavaScript result for example: `return JavaScript("alert();")` – solrac May 22 '18 at 09:59
  • I have tried using return JavaScript("alert();"). but it doesn't work. – Leon Huang May 22 '18 at 10:03
  • You should just return some _data_ from the server which indicates what the result is (and potentially contains some text to display). Write the actual JavaScript code to show your message (and carry out any other operation) in your "success" function. Script returned from a request like that won't be executed for security reasons. – ADyson May 22 '18 at 10:06
  • 1
    Could you not return a Json object with the values you need? i.e. a flag to say whether to display the alert and a string. – Wheels73 May 22 '18 at 10:15
  • Sorry. return JavaScript("alert();") is work. Thank everyone. – Leon Huang May 22 '18 at 10:17

1 Answers1

0

hello hopefully this post help you
you can passe your string to view using viewbag or viewModel as you like
then in this view you put your redirect logic using razor.

Ayoub Codes.
  • 870
  • 5
  • 9