0

ok i have a create/Index/23 view . the create method in the index controller is getting the value of the id when the dropdown event is triggered in the grid. no problem

but there is the search option in my page as well which has ajax.beginform as well. clicking the button there and calling the search method in the index controller is not having the id value which is present in the url . how would i get the id value in the controller . i hope that the question is clear.

Edit: this is the code snippet down here , clicking the button down here will trigger the Search method in the Controller ( which is not getting the id value )

Create.aspx

   <% using (Ajax.BeginForm("Search", new AjaxOptions { UpdateTargetId = "divGrid", OnComplete = "OnComp" }))     {%>

<%:  Html.DropDownList("SelectedsItem", JobHelper.JobSelectStatusDropDown() as IEnumerable<SelectListItem>)%>
<input type="submit" value="Search" />

<% } %>

 <% if (Model.Count() > 0)
 { %>
 <div id="divGrid"> 
    <% Html.RenderPartial("Manage", this.Model);  %>
 </div>
   <% } %>

Solved using the global.asax

1 Answer 1

1

You need a parameter in your controller action to catch the ID

something like this

public ActionResult Index(int id) {...}

For AJAX requests using jquery you can do this

public JsonResult Search(int id) {...}

In your jQuery

$.ajax({
    type: 'POST',
    url: '/Controller/Search/',
    data: "{'id': '" + yourIDGoesHere + "' }",
    contentType: "application/json;charset=utf-8",
    dataType: "json",
    success: function(data) {},
    error: function(e) {}
});

Check out this post with details on what I imagine you're trying to accomplish

http://davidhayden.com/blog/dave/archive/2009/05/19/ASPNETMVCAjaxBeginForm.aspx

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

4 Comments

i have done this i am getting the value in index but not in another search method which is being triggered on the same page
i am using ajax.beginform tag with click on the button to trigger the search method in the controller. is there no way that i can manually capture the id value in the controller which is present in the url? the id value is captured in the main index method when triggered.
i really don't want to persist the id through tempdate or session in the index method and then call it in the search method in the controller
Why don't you share some code in your question so it's obvious what it is you're trying to accomplish and your method for doing so

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.