Thursday 19 June 2014

Parameter passing at Url.Action

What is my goal: Open a model window (partial page) through a javascript function, passing a parameter with it.
Where the challenge is: The parameter cannot be directly added in the url Action.
What is the solution: Add a hardcoded parameter value and replace it in the next step
Normally we would like to pass the parameter in the url in the below way:
reqid = paramvalue;
url = '@Url.Action("PageName", new { requestId = reqid })';

This above code doesn’t work. Below is the sample code which works perfectly. We set a dummy value “-1” as the parameter in the first step. In the next step we replace the dummy value with the actual one.

<div id="modelwindow">
</div>

function ViewRecord(objValue) {       
        reqid = objValue;
        url = '@Url.Action("PageName", new { requestId = "-1" })';
        url = url.replace("-1", reqid);
        modelwindow = $("#modelwindow")
            .kendoWindow({
                title: true,
                modal: true,
                visible: false,
                resizable: true,
                width: '800px',
                content: url
            }).data("kendoWindow");

            modelwindow.title("Request Details");
            modelwindow.center().open();

    }

No comments: