0

How to pass Id from javascript to Controller action in mvc3 on ajax unobtrusive form submit

My script

   <script type="text/javascript">
    $(document).ready(function () {
        $("#tblclick td[id]").each(function (i, elem) {

            $(elem).click(function (e) {
                var ID = this.id;
                alert(ID);
               // var url = '@Url.Action("Listpage2", "Home")'; 
                var data = { Id: ID };
//                  $.post(url,data, function (result) {
//                 }); 
                e.preventDefault();
                $('form#myAjaxForm').submit();
            });
                       });
    });
</script>

the how to pass Id on using $('form#myAjaxForm').submit(); to controller instead of

$.post(url,data, function (result) {
    //                 }); 

My View

 @using (Ajax.BeginForm("Listpage2", "", new AjaxOptions
            {
                UpdateTargetId = "showpage"
            }, new { @id = "myAjaxForm" }))
            {

                <table id="tblclick">
                    @for (int i = 0; i < Model.names.Count; i++)
                    {
                        <tr>
                            <td id='@Model.names[i].Id'>
                                @Html.LabelFor(model => model.names[i].Name, Model.names[i].Name, new { @id = Model.names[i].Id })
                              <br />
                            </td>
                        </tr>
                    }
                </table>

            }
        </td>
        <td id="showpage">
        </td>
5
  • Could you shows the sign of your action? Commented Aug 27, 2012 at 14:08
  • does this "$('form#myAjaxForm').submit()" do anything special or different than redirecting to the controller? If you just need redirected to the action with the correct ID i can help Commented Aug 27, 2012 at 14:20
  • Yes i want that way @JohnSykor Commented Aug 27, 2012 at 14:22
  • ok I undeleted my method(answer) that I removed after seeing the changes made to the post because I was unsure if my answer would apply with the ajax but I know I currently use it in a redirect on click function and I was hoping you would find it useful for your situation Commented Aug 27, 2012 at 14:32
  • stackoverflow.com/questions/11405832/… Using this example you can send Id to Controller class Commented Aug 28, 2012 at 5:56

3 Answers 3

1

I would avoid using the Ajax Beginform helper method and do some pure handwritten and Clean javascript like this

<table id="tblclick">
  @foreach(var name in Model.names)
  {
    <tr>
     <td id="@name.Id">
           @Html.ActionLink(name.Name,"listpage","yourControllerName", 
                         new { @id = name.Id },new { @class="ajaxShow"})         
     </td>
    </tr>
   }
</table>

<script>
 $(function(){
    $(".ajaxShow")click(function(e){
       e.preventDefault();
       $("#showpage").load($(this).attr("href")); 
    });
 });
</script>

This will generate the markup of anchor tag in your for each loop like this.

<a href="/yourControllerName/listpage/12" class="ajaxShow" >John</a>
<a href="/yourControllerName/listpage/35" class="ajaxShow" >Mark</a>

And when user clicks on the link, it uses jQuery load function to load the response from thae listpage action method to the div with id showPage.

Assuming your listpage action method accepts an id parameter and returns something

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

Comments

1

I am not sure for $.post but I know window.location works great for me. Use this instead and hopefully you have good results :)

window.location = "@(Url.Action("Listpage2", "Home"))" + "Id=" + ID;

replace $('form#myAjaxForm').submit(); with this code and nothing looks blatantly wrong with your jscript.

1 Comment

place '?' sign before Id to make it parameter window.location = "@(Url.Action("Listpage2", "Home"))" + "?Id=" + ID;
0

Just use a text box helper with html attribute ID.

@Html.TextBox("ID")

You can do this too:

 var form = $('form#myAjaxForm');
 $.ajax({
       type: "post",
       async: false,
       url: form.attr("action"),
       data: form.serialize(),
       success: function (data) {
          // do something if successful
          }
 });

5 Comments

I want to send it from javascript which i obtained var ID =this.id;
Why don't you want to use $.post then?
I am submiting an unobstrusive ajax form where i dont want my view to postback
How to send and retrieve Id in Controller class
Create textbox like I said and in your javascript do $("form #ID").val(id);

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.