get route value in controller on post back from Ajax.BeginForm?

I have url with parameter name ‘id’?

In  my view i use Ajax.BeginForm which has a submit button.

On submit of the button, a method is envoked and in that method i want fetch the route value.

How to do?

string id = (string)this.RouteData.Values["id"];

abhi0410

On submit of the button, a method is envoked and in that method i want fetch the route value.

The method is an Action. And the routevalue is by putting into the action the 

abhi0410

parameter name ‘id’

 

CAn you please explain little extra on that?

@using (Ajax.BeginForm("Delete", "Author", new AjaxOptions() { UpdateTargetId = "result", HttpMethod = "Post", InsertionMode = InsertionMode.Replace }))
{

 <input id='delete' value='Delete this'/>
}


Controller. --- Author

public ActionResult Delete ()
{
   // how to get the route value from URL into this  method?
}

Give the input a name and make sure your Delete action has an delete param to match the name

<input id=‘delete’ name=’delete’
value=‘Delete this’/>

public ActionResult Delete(string delete)

I don’t want action name.That is taken care by Action  name in Ajax.BeginForm.

My url is like.

http://xyz.com/Author/AuthorDetail/5

Now i want the value 5 in method.

Give AuthorDetail an id paramater

AuthorDetail(string id)

This is really really basic MVC 101, you would be better going through some tutorials or the sample projects on this site so you can at least get an understanding of the basics.

Hi AidyF,

I think i didn’t make my question properly. I know for HttpGet, i just need to make AuthorDetail(string id).

Bu I am looking to get the route value on postback of button which is inside Ajax.BeginForm().

[HttpPost]
public ActionResult AuthorDetail(int id)
{
     ///
            
}

Now can you suggest me?

Leave a Reply