簡體   English   中英

在同一解決方案中從另一個項目的視圖中調用類庫中的函數

[英]calling function in class library from view in another project in same solution

我在這樣的類庫中有一個功能

===============

  public class QUnitTestController : DesignerController
{
    public ActionResult Index()
    {
        InitializeModel();

        var designer = ((Designers)(Convert.ToInt32(DesignerId))).ToString();

        ViewBag.ControllerName = designer;

        return View("Index");
    }

    [HttpPost]
    [ActionName("designdocument")]
    public virtual ActionResult GetDesignDocument()
    {
        return GetJson("~/xmldata/Designer/QUnitDesignDoc.xml", true);

    }

    [HttpPost]
    [ActionName("designconfig")]
    public virtual ActionResult GetDesignConfig()
    {

        return GetJson("~/xmldata/Designer/QUnitDesignConfig.xml", true);
    }

    public ContentResult GetJson(string data, bool isUri)
    {
        var document = new XmlDocument();

        if (isUri)
        {
            document.Load(Server.MapPath(data));
        }
        else
        {
            document.LoadXml(data);
        }

        string jsonText = JsonConvert.SerializeXmlNode(document.DocumentElement);
        return Content(jsonText, "text/html", Encoding.UTF8);
    }
}

我在另一個MVC項目的自定義視圖中有一些jquery代碼,如下所示:

    <script type="text/javascript">

    //var designerDocument = null;
    //var designerConfig = null;
    //var designerController = null;
    //var zoom = null;


        function Test() {
            $.ajax({
                url: "QUnitTest/designdocument",
                type: "POST",
                dataType: 'json',
                async: true,//WHY??
                data: param = "",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    alert("success");
                },
                error: function () {
                    alert("something went wrong");
                }
            });

            $.ajax({
                url: "QUnitTest/designconfig",
                type: "POST",
                dataType: 'json',
                async: true,//WHY??
                data: param = "",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    alert("success");
                },
                error: function () {
                    alert("something went wrong");
                }
            });

            designerController = new Design.QUnitTestController();

            zoom = 0;
        }





        module("Load Designer Module", {
            setup: function () {
                //Test();
            }
        });
        test("Load Designer", function () {

            ok(true, "body was clicked!");
            var a = designerController.InitializeDesigner(designerDocument, designerConfig, "#designerContainer");
            equal(a, "expected value");
        });





</script>

問題是我無法在類庫中調用該函數。 請告訴我我該怎么做才能使其正常工作

您需要注冊正確的路由,以將諸如“ QUnitTest / someaction”之類的URL映射到您的控制器操作:

檢查您的Global.asax.cs或RouteConfig類,並將以下代碼添加到RegisterRoutes方法中:

routes.MapRoute(
    name: "QUnitTest",
    url: "QUnitTest/{action}",
    defaults: new { controller = "QUnitTest", action = "Index" },
    namespaces: new[] { "NamespaceOfQUnitTestControllerClass" }
);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM