开发者

How can i inject url into razor url in jquery

What im trying to do here is getting a json url from string argument. My View my view name is Admin/Index

function blablasub(){

   开发者_如何学JAVA submitter("ParentList", "ChildListTextArea", "~/Admin/DesignSubmit/", "ides", "productid");
}

and wanna try using these in the function below.

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");
            })
        }
    }
}

so how can i inject the url as string into razor @Url.Content() ?

Edit: My Controller

my controllers name is 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);
    }


The Url.Content method is executed and output server-side. As such the javascript value for the url, which is client side, is not present when the razor file is parsed. So what we need to do is make sure the Url.Content is executed before the javascript is called like so:

Assuming your blahblah function is within your view. (based on your comment above)

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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜