Note that there are some explanatory texts on larger screens.

plurals
  1. POAjax.BeginForm Update unique div id in For Loop
    primarykey
    data
    text
    <p><strong>Update</strong> I ended up just using Session Variables to pass in the values to the Ajax.BeginForm block. It's a hack that I didn't want to use, but I just couldn't find another way...</p> <p><strong>Original Post</strong> I am using a For Loop that assigns each comment div in the loop a unique id like:</p> <pre><code>&lt;div id="comment@(feedModel.PostID)"&gt;&lt;/div&gt; </code></pre> <p>EX:</p> <p>comment1</p> <p>comment2</p> <p>comment3</p> <p>Each one of these div's has an Ajax.BeginForm with a input submit button. I want to set the UpdateTargetId AjaxOption, but I can't because each id is unique. So UpdateTargetId = "div" won't work. I can't just pass in the model or TempData becasue this is Ajax.BeginForm. So how do I pass in the feedModel.PostID?</p> <p>Code:</p> <pre><code>@foreach (Website.Models.FeedModel feedModel in Model) { &lt;span id="commentspan@(feedModel.PostID)" style="margin-top:5px; display:block; font-weight:bold"&gt;&lt;a href="#" onclick="loadPartialView(@feedModel.PostID); return false;"&gt;Comments(@feedModel.Replies)&lt;/a&gt;&lt;/span&gt; &lt;div id="comments@(feedModel.PostID)"&gt;&lt;/div&gt; </code></pre> <p>}</p> <p>The Javascript that is called:</p> <pre><code> function loadPartialView(context) { method HomeController._Comments $('#comments' + context).load('Home/_Comments/' + context); } </code></pre> <p>Inside that PartialView</p> <pre><code>@if (Request.IsAuthenticated) { &lt;div class="childpost"&gt; @using (Ajax.BeginForm("SubmitChildPost", "Home", new AjaxOptions { UpdateTargetId = "comments@TempData['feedModelID'].ToString()" }, new { id = @TempData["feedModelID"].ToString() })) { @Html.TextArea("MessageBox", "", 2, 50, null) &lt;br /&gt; &lt;p&gt; @Html.AntiForgeryToken() &lt;input type="submit" value="Reply" class="replybutton" /&gt; &lt;/p&gt; } &lt;/div&gt; } </code></pre> <p>Which posts to the Action. The string id is null because you can't pass Model values or Temp Data to the Ajax.BeginForm so I'm not sure what to do...</p> <pre><code>[ValidateAntiForgeryToken] public ActionResult SubmitChildPost(FormCollection formValue, string id) { string message = formValue["MessageBox"].ToString(); if (message != "") { MessageRepository.UpdatePostReply(Convert.ToInt32(id)); MessageRepository.SaveChildPost(message, id, User.Identity.Name); } return RedirectToAction("_Comments", "Home", new { ID = id}); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload