We help IT Professionals succeed at work.

MVC: Sending an ID from a view to a controller and doing something with it

279 Views
Last Modified: 2018-03-27
I have a button on a view located at: localhost:99998/MyController/Edit/5017

 <input type="button" value="Delete" class="btn btn-danger" onclick="location.href='@Url.Action("DelRec", "MyController")'" />

Open in new window


I want to take the ID (in this case 5017)
send it to the controller action (DelRec)
then update the correlating record (5017)

How do I do this?
Comment
Watch Question

Prakash SamariyaIT Professional
CERTIFIED EXPERT
Top Expert 2015

Commented:
Hope your controller action method "DelRec" has parameter to accept ID like:
public ActionResult DelRec(int id)
{...}

Open in new window

To get id from URL (as your current url is : localhost:99998/MyController/Edit/5017)
Store into variable [myId]
@{ ... 
var myId = Url.RequestContext.RouteData.Values["id"]; 
...}

Open in new window

To pass parameter (here id) use below code (use variable [myId])
 <input type="button" value="Delete" class="btn btn-danger" 
onclick="location.href='@Url.Action("DelRec", "MyController", new {id=myId})'" />

Open in new window

Author

Commented:
Seems like this way is trying to redirect to a View called DelRec, unless I'm doing something wrong. All I want to do is run some code.
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
Prakash SamariyaIT Professional
CERTIFIED EXPERT
Top Expert 2015

Commented:
@Mike, it is not necessary to create form just to send your id to some other action method, If you have other work to manipulate and send, then it is fine!

Just for sending id to other action method, I guess my solution will work #a42061177 !

Author

Commented:
I was not successful in getting your solution to work. As I stated earlier, it was directing my browser to a view called DelRec/ID
Furthermore, my initial request was to update a record not "Just for sending id to other action method"

I would love to know how to do this correctly as while this "add another form" method works for me now, I see this as being problematic most other times.
Prakash SamariyaIT Professional
CERTIFIED EXPERT
Top Expert 2015

Commented:
<input type="button" value="Delete" class="btn btn-danger" onclick="location.href='@Url.Action("DelRec", "MyController")'" />
that's what your question stated!
I want to take the ID (in this case 5017)
send it to the controller action (DelRec)
You were needed take id from edit url, and then to pass id to DelRec!

Anyways, best of luck!

Author

Commented:
No other relevant solutions provided...figured it out myself

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions