从asp.NET核心控制器返回JavaScript

时间:2017-03-08 23:57:30

标签: javascript asp.net-core

我知道有很多关于此的帖子,但我无法得到任何建议,如...

Return Content("<script>alert('Hello world!');</script>")

......工作

我们正准备从MVC迁移到核心 - 这在MVC中工作并显示了一个引导模式,但我收集了对此方法的支持已被放入核心。

这就是启动模态'PopupModal'

的原因
 Public Function ModalValidation(Message As String) As String
    Try
        Dim SB As New StringBuilder
        SB.Append("$(document).ready(function(){")
        SB.Append("$('#PopupModal').modal();")
        SB.Append("var vBody = document.getElementById('ModalBodyText');")
        SB.Append("vBody.innerHTML = '" & Message & "';")
        SB.Append("var vTitle = document.getElementById('ModalTitle');")
        SB.Append("vTitle.innerHTML = 'Validation';")
        SB.Append("vTitle.style.color = 'orange';")
        SB.Append("});")
        Return SB.ToString
    Catch ex As Exception
        EmailError(ex, 170, "Functions")
        Return Nothing
    End Try
End Function

并在控制器上

 Private Function Modal(ModalScript As String) As JavaScriptResult
        Return JavaScript(ModalScript)
    End Function

这样称呼

Return Modal(ModalValidation("Got here"))

看起来我可以像这样重新创建JavaScriptResult

Public Class JSResult
        Inherits ContentResult
        Public Sub New(ByVal script As String)
            Me.Content = script
            Me.ContentType = "application/javascript"
        End Sub
    End Class

但是我用什么代替

Return JavaScript(ModalScript)

谢谢

2 个答案:

答案 0 :(得分:0)

想出来

 Private Function Modal(ModalScript As String) As JSResult
        Return New JSResult(ModalScript)
    End Function

答案 1 :(得分:-1)

public ActionResult Welcome(){
  return Content("You are welcomed");
}

通过ajax调用此函数并处理响应。