Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9194077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:19:09+00:00 2026-06-17T21:19:09+00:00

I’m trying to figure out how to have an edit function and a delete

  • 0

I’m trying to figure out how to have an edit function and a delete function from the same page in my application. At the moment my controller looks like this:

    [HttpPost]
    public ActionResult Edit(CodeTagViewModel codeTagViewModel, FormCollection collection)
    {
        if (ModelState.IsValid)
        {

            List<Tag> tagsToAdd = new List<Tag>();

            codeTagViewModel.Tags = db.Tags.ToList();
            foreach (Tag tag in codeTagViewModel.Tags)
            {
                if (collection[tag.TagID.ToString()].ToString().Contains("true"))
                {
                    tagsToAdd.Add(tag);
                }
            }

            codeTagViewModel.SelectedTags = tagsToAdd;

            Code code = db.Code.Find(codeTagViewModel.CodeID);
            MapModelToCode(codeTagViewModel, code);

            db.Entry(code).State = EntityState.Modified;
            db.SaveChanges();
            return View(codeTagViewModel);
        }
        return RedirectToAction("Index");
    }

    [HttpPost, ActionName("Delete")]
    public ActionResult Edit(CodeTagViewModel codeTagViewModel, FormCollection collection)
    {
        if (ModelState.IsValid)
        {

            List<Tag> tagsToAdd = new List<Tag>();

            codeTagViewModel.Tags = db.Tags.ToList();
            foreach (Tag tag in codeTagViewModel.Tags)
            {
                if (collection[tag.TagID.ToString()].ToString().Contains("true"))
                {
                    tagsToAdd.Add(tag);
                }
            }

            codeTagViewModel.SelectedTags = tagsToAdd;

            Code code = db.Code.Find(codeTagViewModel.CodeID);
            MapModelToCode(codeTagViewModel, code);

            db.Code.Remove(code);
            db.SaveChanges();
            return View(codeTagViewModel);
        }
        return RedirectToAction("Index");
    }

And I’ve got the two buttons in my Edit view:

    <p>
        <input type="submit" value="Save" />
    </p>
    <p>
        <input type="submit" value="Delete" />
    </p>

And my view model looks like this:

public class CodeTagViewModel
{
    public List<Tag> Tags { get; set; }
    public List<Tag> SelectedTags { get; set; }        

    public int CodeID { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public DateTime DateAdded { get; set; }
    public DateTime LastUpdated { get; set; }
    public string Project { get; set; }
    public string CMS { get; set; }
    public int DotNetVersion { get; set; }
    public string Dependencies { get; set; }
    public string Author { get; set; }
    public string CodeFile { get; set; }
    public string TFSLocation { get; set; }

}

At the moment the delete button is doing nothing, it’s not hitting the code at all.

I’m new to MVC and trying to figure out how it handles all of the background stuff so any help would be greatly appreciated.

EDIT: I’ve also looked into the routing aspect and put in:

    //
    // POST: /Code/Edit/5

Above the delete function (it’s the same as the one for the edit) and still nothing.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:19:11+00:00Added an answer on June 17, 2026 at 9:19 pm

    That’s because both the Edit and Delete button both do the same thing, they simply post the form. So they will always post to the same place.

    You should change your delete button to be an ActionLink, then change your Delete method to be a HttpGet, pass in the ID of the item you want to delete and then retrieve and delete it.

    Like this:

    @Html.ActionLink("Delete", "Delete", "Code", new { CodeID = Model.CodeID })
    

    Then in your action method:

    [HttpGet]
    public ActionResult Delete (int CodeID)
    {
       //perform deletion
    }
    

    Otherwise, you’d have to use Javascript to automatically change the post URL of the form depending on which button was clicked, which is nasty.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Basically, what I'm trying to create is a page of div tags, each has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I have an autohotkey script which looks up a word in a bilingual dictionary
I have a text area in my form which accepts all possible characters from
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a view passing on information from a database: def serve_article(request, id): served_article
I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.