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

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?
Mike MillerSoftware EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Prakash SamariyaIT ProfessionalCommented:
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

0
Mike MillerSoftware EngineerAuthor 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.
0
Mike MillerSoftware EngineerAuthor Commented:
Ok I got something that works. I don't necessarily like it and I'm certain there is a better way, but for now this will have to do

View (added second form)...
@using (Html.BeginForm("DelRec", "MyController", FormMethod.Post, new { @class = "form", id = "dr" }))
{
    <div class="form-horizontal">
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                @Html.HiddenFor(model => model.ID)
                <input type="submit" value="Delete request" style="border:0px;border-style:none;background-color:#fff;text-decoration:underline;padding:0px;" />
            </div>
        </div>
    </div>

Open in new window


Controller...
 [HttpPost]
        public ActionResult DelRec(MyController m-ctrl)
        {
            if (ModelState.IsValid)
            {
                try
                {
...stuff here
                }
                catch
                {
                    return RedirectToAction("errView");
                }                
            }
            return RedirectToAction("aView");
        }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Prakash SamariyaIT ProfessionalCommented:
@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 !
0
Mike MillerSoftware EngineerAuthor 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.
0
Prakash SamariyaIT ProfessionalCommented:
<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!
0
Mike MillerSoftware EngineerAuthor Commented:
No other relevant solutions provided...figured it out myself
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET MVC

From novice to tech pro — start learning today.