如何在jquery中将url注入razor url

时间:2011-08-17 12:12:46

标签: jquery asp.net-mvc-3 razor

我在这里尝试做的是从字符串参数获取一个json url。 我的看法 我的观点名称是Admin / Index

function blablasub(){

    submitter("ParentList", "ChildListTextArea", "~/Admin/DesignSubmit/", "ides", "productid");
}

并想在下面的函数中尝试使用它们。

function submitter(chosenListBox, chosenTextBox, jsonurl, textparameter, listparameter) {
    var ListBox = $("select#" + chosenListBox);
    var ListBoxVal = $("select#" + chosenListBox + " option:selected").val();
    if (ListBoxVal == "" || ListBoxVal == 0) {
        ListBox.css('background-color', '#FFBBBB');
    } else {
        ListBox.css('background-color', 'white');

        var TextBox = $("#" + chosenTextBox);
        var TextBoxVal = $("#" + chosenTextBox).val();
        if (TextBoxVal == "" || TextBoxVal == 0) {
            $("#" + chosenTextBox).css('background-color', '#FFBBBB');
        } else {
            TextBox.css('background-color', 'white');
            alert(ListBox.toString() + " - " + TextBox.toString() + " - " + '@Url.Content(" + jsonurl.toString() + ")' + " - " + textparameter.toString() + " - " + listparameter.toString() + " - Finito Baby");
            $.getJSON('@Url.Content(" + jsonurl.toString() + ")', { textparameter: TextBoxVal, listparameter: ListBoxVal }, function (data) {
                clearitem("#" + chosenListBox);
                $("#" + chosenListBox).removeAttr('disabled');
                $.each(data, function (i, c) {
                    $("#" + chosenListBox).append('<option value="' + c.Value + '">' + c.Text + '</option>');
                })
                $("#" + chosenListBox + " option:first").attr('selected', 'selected');
                $("#" + chosenListBox).change("#" + chosenListBox + "list");
            })
        }
    }
}

那么如何将url作为字符串注入razor @Url.Content()

编辑: 我的控制器

我的控制器名称是AdminController

    public JsonResult ProductSubmit(string ipro, Product prod)
    {
        prod.name = ipro;
        db.Products.InsertOnSubmit(prod);
        db.SubmitChanges();

        int pro = Convert.ToInt32(db.Products.Where(x => x.name == ipro).Select(x => x.id).Single());
        IEnumerable<SelectListItem> Items = db.Products.Where(d => d.id == pro).AsEnumerable().Select(c => new SelectListItem()
        {
            Text = c.name,
            Value = c.id.ToString()
        });
        SelectList data = new SelectList(Items, "Value", "Text");
        return Json(data, JsonRequestBehavior.AllowGet);
    }

    public JsonResult DesignSubmit(string ides, Design des, int productId)
    {
        des.name = ides;
        des.master_id = productId;
        db.Designs.InsertOnSubmit(des);
        db.SubmitChanges();
        IEnumerable<SelectListItem> Items = db.Designs.Where(x => x.master_id == productId).AsEnumerable().Select(c => new SelectListItem()
        {
            Text = c.name,
            Value = c.id.ToString()
        });
        SelectList data = new SelectList(Items, "Value", "Text");
        return Json(data, JsonRequestBehavior.AllowGet);
    }

1 个答案:

答案 0 :(得分:1)

执行Url.Content方法并输出服务器端。因此,在解析剃刀文件时,不存在客户端的URL的javascript值。所以我们需要做的是确保在调用javascript之前执行Url.Content,如下所示:

假设您的blahblah功能在您的视野范围内。 (根据您的上述评论)

function blablasub(){                                   /*works this way..*/
    submitter("ParentList", "ChildListTextArea", '@Url.Content("~/Admin/DesignSubmit/")', "ides", "productid");
}


$.getJSON(jsonurl, { textparameter: TextBoxVal, listparameter: ListBoxVal }, function (data) {
...
});