Allensb
Allensb

Reputation: 260

Ajax.BeginForm Update unique div id in For Loop

Update 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...

Original Post I am using a For Loop that assigns each comment div in the loop a unique id like:

<div id="comment@(feedModel.PostID)"></div>

EX:

comment1

comment2

comment3

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?

Code:

@foreach (Website.Models.FeedModel feedModel in Model)
{
    <span id="commentspan@(feedModel.PostID)" style="margin-top:5px;    display:block; 
        font-weight:bold"><a href="#" onclick="loadPartialView(@feedModel.PostID);   
        return false;">Comments(@feedModel.Replies)</a></span>
    <div id="comments@(feedModel.PostID)"></div>

}

The Javascript that is called:

 function loadPartialView(context) {
 method HomeController._Comments
 $('#comments' + context).load('Home/_Comments/' + context);
}

Inside that PartialView

@if (Request.IsAuthenticated)
{   
<div class="childpost">
@using (Ajax.BeginForm("SubmitChildPost", "Home", new AjaxOptions { UpdateTargetId = 
"comments@TempData['feedModelID'].ToString()" }, new { id = 
@TempData["feedModelID"].ToString() }))
{
    @Html.TextArea("MessageBox", "", 2, 50, null)
    <br />
    <p>
        @Html.AntiForgeryToken()
        <input type="submit" value="Reply" class="replybutton" />
    </p>
}
</div>
}

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...

[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});
    }

Upvotes: 1

Views: 1442

Answers (1)

counsellorben
counsellorben

Reputation: 10924

Assuming that the Ajax.BeginForm is inside of the For loop, you can give the target div a unique id using the same method you already are using, like so:

@using (Ajax.BeginForm("Comment", "Comments", new AjaxOptions() 
    { HttpMethod = "Post", UpdateTargetId = "target" + feedModel.PostID }))

Then, in your loop, define the target div:

<div id="[email protected]" />

UPDATE

Based on the code you posted, making an assumption that the context value you are passing is being passed into the id tokenvalue in your route, then you can capture this value in the PartialView as follows:

@{
    var id = this.Request.RequestContext.RouteData.Values["id"].ToString();
}

Then, you can use id to assign your unique id.

Upvotes: 1

Related Questions