0

So I have a list of objects in my controller, and I would like to have a button that initiates a pop-up window, which associates with each of the object in the list. I think this needs to be done by Javascript, and to identify what window to display, I would need to pass the ID parameter (one of the attributes of the object) to the javascript. I currently have the following code:

@Html.BeginForm()
{
            @for (int i = 0; i < Model.Count(); i++)
            {
                var theId = Model[i].CW_Id;
            <tr>
                <td>
                    <input type="button" id="btnpopup" value="Details" onclick="ShowModelPopUp()" data="theId" />
                </td>
                @Html.EditorFor(m => Model[i], "CWinline") //Compiles the list using Editor Template
            </tr>
            }
            </table>

        <input type="submit" value="Save" class="btn btn-default" />
    }

    <script type="text/javascript">
        ShowModelPopUp = function () {
            var theId = $(this).data();
            window.showModalDialog('/Default/Selected/?id='+theId, "WindowPopup", 'width=200,height=500');
        }

    </script>

Somehow the variables aren't passing properly. Does anyone know where it went wrong?

2 Answers 2

1

This:

<input 
  type="button" id="btnpopup" 
  value="Details" 
  onclick="ShowModelPopUp()" 
  data="theId" />

...will put the string "theId" in the attribute. You want this:

<input 
  type="button" id="btnpopup" 
  value="Details" 
  onclick="ShowModelPopUp()" 
  data="@theId" />

The @ will let the razor template engine know to switch to output a .NET value.

Sign up to request clarification or add additional context in comments.

3 Comments

Somehow the value is not getting passed properly. It's been passed as "[object Object]". Do you know why that is happening?
@theId is going to basically do a @theId.ToString(); if you want it to be JSON, you'll need to serialize it.
With JSON.NET, for example: @JsonConvert.SerializeObject(theId)
0

try

 <input type="button" id="btnpopup" value="Details" onclick="ShowModelPopUp()" data="@Model.id_attribute" />

Comments

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.