0

help please

I have a view with many buttons...each button has unique ID. I want to open a partial view in a jquery dialog box. I can see the dialog box and even see the partial form in it, but I cannot get a parameter passed to it. the following script works...but notice I have hard coded the id in the url. How do I put a variable in there instead of a hardcoded id number. Thanks very much

<script>
               var ASeatId;  //this code works

               function showPersonDialog(seatId) {
                   ASeatId = seatId;
                   $('#dialog').dialog('open');
               }

               $('#dialog').dialog({
                   autoOpen: false,
                   width: 800,
                   resizable: false,
                   title: 'Edit Guest',
                   modal: true,
                   open: function (event, ui) {
                       var url = '@Html.Raw(Url.Action("Guest", "Seat", new{seatID=3}))';         
            $(this).load(url);
        },
        buttons: {
            "Save": function () {
                $(this).dialog("save");
                // prevent the default action, e.g., following a link   
                return false;
            },
            "Close": function () {
                $(this).dialog("close");
                // prevent the default action, e.g., following a link   
                return false;
            }
        }
});
           </script>

so when I step through controller code

  public ActionResult Guest(int? seatId)
    {


        return PartialView("_Guest");
        //return PartialView("_Guest");
    }

I can see the number 3

I would like to pass a variable instead based on the button click...this code that follows does NOT work

       <script>  ///this code does not work
           var ASeatId;

           function showPersonDialog(seatId) {
               ASeatId = seatId;
               $('#dialog').dialog('open');
           }

           $('#dialog').dialog({
               autoOpen: false,
               width: 800,
               resizable: false,
               title: 'Edit Guest',
               modal: true,
               open: function (event, ui) {
                   var url = '@Html.Raw(Url.Action("Guest", "Seat", new{seatID=ASeatID}))';  ///this being the variable       
        $(this).load(url);
    },
    buttons: {
        "Save": function () {
            $(this).dialog("save");
            // prevent the default action, e.g., following a link   
            return false;
        },
        "Close": function () {
            $(this).dialog("close");
            // prevent the default action, e.g., following a link   
            return false;
        }
    }
 });
       </script>

Thanks again

5 Answers 5

2
var url = '@Url.Action("Guest", "Seat", new{ seatID = "-1"}))';
url = url.replace("-1", ASeatId);

Often getting variables into another variable needs a little help.

Sometimes it may be necessary to convert an integer to number when the action controller is expecting a search string.

var aid = ASeatId.toString();
var url = '@Url.Action("Guest", "Seat", new{ seatID = "-1"}))';
url = url.replace("-1", aid);
$(this).load(url)

Most times, a simple load call ( as referenced above) on document ready works best for me (passing the model param).

$(this).load('@Url.Action("Guest", "Seat", new{ seatID = Model.ASeatId}))');
Sign up to request clarification or add additional context in comments.

Comments

0

How about this (in the dialog's open function)

 var url = '@Html.Raw(Url.Action("Guest", "Seat", new{seatID='+ASeatID+'}))';  ///this being the variable 

variable does not go to string .!

2 Comments

Thank you for responding. I tried your suggestion. Still not working. Getting a bad compile constant value error. CS1012 Too many characters in character literal.
Just a wild guess, but var url = '@Html.Raw(Url.Action("Guest", "Seat", new{seatID=eval(ASeatID)'}))'; may be?
0

Just use like this

$("#customerTabBody").load('Home/GetCustomerList', { 'searchString': searchCriteria });

Here the searchCriteria is the parameter you have to pass

1 Comment

Thank you for your response. I tried this way and still not working. Don't get any errors but my partial view does not load at all...I have a code break at my controller and it doesn't even hit the controller...so seems like the .load(url, {'seatId':ASeatId}) doesn't get to the controller
0
var url = '@Html.Raw(Url.Action("Guest", "Seat")))
url = url + ASeatId;

Comments

0

try this code:

var url = "@Url.Action("Guest", "Seat")" +"/" + ASeatID;

1 Comment

welcome to stackoverflow! generally, code-only answers without explanations are not preferred... would you consider adding some explanation to your answer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.