开发者

How to extract data from a HTML.TextArea contained in a foreach loop using JQuery or other means

I am new to jQuery. I have a Html.TextArea called "CommentPost" displayed for each item in a foreach loop. I am trying to return the data typed into my text area as a parameter in my ActionLink call. The first parameter item.CommentId get populated correctly but the second one item.CommentPosting evaluates as null. I thought that maybe a jQuery call - when the ActionLink gets selected - could sort开发者_开发知识库 this out for me. I have been trying to solve this for hours now but am not getting any closer. I know that $('#CommentPost').attr("value") gives the correct text but how to put it all together is beyond me. Please help

<% foreach (var item in Model.Comments)
   { %>
    <%= (item.UserName ) %>
<%= Html.TextArea("CommentPost", item.CommentPosting)%>
<%= Html.ActionLink("Save", "CommentPosting", new { commentId = item.CommentId , commentPost =item.CommentPosting})%>
<% }%>

The shell of my controller code looks as follows:

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult CommentPosting(int commentId, string commentPos)
{

}


Ok I solved it ...eventually(after praying for inspiration)!

This is what I did :

I modified my HTML.Actionlink by hardcoding the value "test" for my commentPost parameter and then hooked a new javaScript call GetData(link) to the onClick event. This javascript call changes the hardcoded value to the correct value:

<%= Html.ActionLink("Save", "CommentPosting", new { commentId = item.CommentId, commentPost = "test" }, new { onClick = "GetData(this);" })%>

Then I created my JavaScript function:

function GetData(link) {
    var temp = link.previousElementSibling;
    link.originalHref = link.originalHref || link.href;
    link.href = link.originalHref.replace("test", $(temp).attr("value"));
    return true;
}

Well Thank Goodness it all works now!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜