0

I am looking to achieve the following:

I have 2 views, 1 partial and 1 index.

In the index I have a loop that goes through a list of models and passes each item to the partial view.

Now for each item in that list of models I want to be able to a modal dialog. I.e. for a list of user posts there is a buy or sell button on each post. All of which use the same div/modal. Is there any way I can retrieve say a textbox value from within that modal dialog, specifically for the dialog whose "Buy now" button I've clicked on?

Code:

Index:

 @model IEnumerable<BRApplication.Models.MarketPost> 

 @{
     foreach (var m in Model) {
             if (m.IsBuy) {
                 <li>
                         @Html.Partial("BuyPost_Partial", m) 
                 </li>
             } 
     }
 }

and in the BuyPost_Partial have this:

 <div id="mdlEmail" class="none">
     Please enter an email the owner of this book can contact you with. <br/>
      @Html.TextBox("txtRespondantEmail", "", new { id = "txtRespondantEmail" })
 </div>

I want to be able to get the value of the txtRespondantEmail textbox.

Any ideas on how I can make this textbox unique for each post? I don't know how I would access the ID if I append an ID to it either.

o JS for the Modal (I've put it in the Index.cshtml file)

         function EmailDialog() {
            // alert("email");
             $("#mdlEmail").dialog({
                 height: 200,
                 width: 500,
                 modal: true,
                 buttons: {
                     "Confirm": function () {
                         MailNow();
                         $(this).dialog("close");
                     }
                 }
             });
         }

In the MailNow() function I try to access the textbox value with this line of jQuery:

 var RespondantEmail = $("#txtRespondantEmail").val();

Buy now handler:

   $(function () {
             $("#btnBuyNow").click(function () {
                 var email = $("#email").text();
                 alert(email);
                 if (email) {
                     EmailDialog();
                 } else {
                     FacebookLink();
                 }
             });
         });

The textbox is in the EmailDialog().

4

2 に答える 2

0

モデルがどのように見えるかはわかりませんが、テキストボックスのIDにアタッチできるように、一意の部分ビューに渡すモデルに何かがありますか?

たとえば、 m.ID のようなものがあり、試すことができます

@Html.TextBox("txtRespondantEmail", "", new { id = "txtRespondantEmail" + m.ID })

私は現在VSを持っていないので、これをテストできませんでしたが、試してみてください。

$('[id^="txtRespondantEmail"]') - 'txtRespondantEmail' で始まるすべてのテキスト ボックスを返します

于 2013-04-02T22:34:40.353 に答える