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 8213741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:13:21+00:00 2026-06-07T11:13:21+00:00

I use MVC 3, I have a View to Delete an element in my

  • 0

I use MVC 3, I have a View to Delete an element in my DB. A Service Layer check that all the logic is in place before authorizing the operation. In case of a Role violation a Message appear in the Html.Summary so the delete operation is abort.

My problem is when the message in the Html.Summary from the Service Layer arrive the View lost the data for the various fields, so you cannot see the ID, Name and Description for the item.

Could you point me out in the right direction? What I’m doing wrong? Thanks

public ActionResult Delete(int id)
    {
        Typology typology = _typologytService.FindById(id);
        TypologyDeleteViewModel deleteViewModel = Mapper.Map<Typology, TypologyDeleteViewModel>(typology);
        return View(deleteViewModel);
    }

    //
    // POST: /ManageTypologies/Delete/5

    [HttpPost]
    public ActionResult Delete(TypologyDeleteViewModel deleteViewModel)
    {
        if (ModelState.IsValid)
        {
            Typology typology = Mapper.Map<TypologyDeleteViewModel, Typology>(deleteViewModel);
            _typologytService.Delete(typology.TypologyId);
            if (ModelState.IsValid)
                return RedirectToAction("Index");
        }
        return View();         
    }

@model xxx.ViewModels.TypologyDeleteViewModel
@{
    ViewBag.Title = "Delete";
}
<h2>
    Delete</h2>
<h3>
    Are you sure you want to delete this?</h3>
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    @Html.HiddenFor(model => model.TypologyId)
    <fieldset>
        <legend>TypologyDeleteViewModel</legend>
        <div class="display-label">
            TypologyId</div>
        <div class="display-field">
            @Html.DisplayFor(model => model.TypologyId)
        </div>
        <div class="display-label">
            Name</div>
        <div class="display-field">
            @Html.DisplayFor(model => model.Name)
        </div>
        <div class="display-label">
            Description</div>
        <div class="display-field">
            @Html.DisplayFor(model => model.Description)
        </div>
        <p>
            <input type="submit" value="Delete" />
        </p>
    </fieldset>
}
<div>
    @Html.ActionLink("Back to List", "Index")
</div>
  • 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-07T11:13:23+00:00Added an answer on June 7, 2026 at 11:13 am

    Change your post method to:

    [HttpPost]
        public ActionResult Delete(TypologyDeleteViewModel deleteViewModel)
        {
            if (ModelState.IsValid)
            {
                Typology typology = Mapper.Map<TypologyDeleteViewModel, Typology>(deleteViewModel);
                _typologytService.Delete(typology.TypologyId);
                if (ModelState.IsValid)
                    return RedirectToAction("Index");
            }
            return View(deleteViewModel);         
        }
    

    You are not passing the model back to the view when you return an invalid model state. Also, is there a reason that you check the ModelState.IsValid property twice in the above code? I am not seeing where you might be adding a ModelStateError and there is no path out of the second if statement.

    Lastly, if your deleteViewModel contains any dropdown lists, etc (which in this case you do not appear to)…you will need to repopulate them before returning the view.

    EDIT

    In your case, the above would not work since you are not using EditorFor HTML helper for your other properties. Your view is spitting out the equivalent of <span> tags for description, etc which are not posted back to the server. The only thing coming back is your Id property because it is in a <input type="hidden" field.

    So – in your case, you would need to update your action method to:

       [HttpPost]
        public ActionResult Delete(TypologyDeleteViewModel deleteViewModel)
        {
            if (ModelState.IsValid)
            {
                Typology typology = Mapper.Map<TypologyDeleteViewModel, Typology>(deleteViewModel);
                _typologytService.Delete(typology.TypologyId);
                if (ModelState.IsValid)
                    return RedirectToAction("Index");
            }
            Typology typology = _typologytService.FindById(typology.TypologyId);
            TypologyDeleteViewModel deleteViewModel = Mapper.Map<Typology, TypologyDeleteViewModel>(typology);
            return View(deleteViewModel);         
        }
    

    or, store your values in hidden input fields like you are doing with your ID so that all values are posted back to the server and you do not have to make a database call again.

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

Sidebar

Related Questions

I have an mvc application that has been coded to use Windows authentication and
I have an ASP.NET MVC 3 site that makes use of a ClientDependency framework
I have two radio buttons on my MVC form that I use to hide
I have 2 attributes that I use within the controllers of my MVC application.
I have a view that that nedds to use an external static content provide.
I use Spring MVC 3.0 and JSP. I have an object: public class ObjectWrapper
I used to use Backbone.js as an easy MVC framework which have build-in routing
I have to use Sitecore 6.5 with MVC framework and as per the reference
I have a Spring MVC application trying to use a rich domain model, with
I have started a new mvc project and clicked on project\properties\web - use local

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.