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

  • Home
  • SEARCH
  • 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 7994817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:13:41+00:00 2026-06-04T14:13:41+00:00

I show the Delete View in a partial/dialog. Initially no items are selected. When

  • 0

I show the Delete View in a partial/dialog. Initially no items are selected.
When the user clicks the ok button the Delete Post action and its parameter selectedIds is NULL. Thats ok, but how can I show the user an error message?

When I reach the code “return PartialView” then I get an Exception in the View, that my Model.DisplayList ist empty.

I know I seem to mix 2 problems here but I guess one could solve the other…

Delete Actions:

[HttpGet]
        public ActionResult Delete()
        {
            var templates = _templateDataProvider.GetTemplates();
            var listViewModel = new ListViewModel<Template>();
            listViewModel.DisplayList = templates;
            return PartialView(listViewModel);
        }

        [HttpPost]
        public ActionResult Delete(int[] selectedIds)
        {
            if (selectedIds == null)
            {
                ModelState.AddModelError("Name", "Nothing selected");
            }

            if (ModelState.IsValid)
            {
                return Json(new { success = true });
            }

            return PartialView();
        }

Delete View:

@model ITMS.ViewModels.ListViewModel<ITMS.Models.Template>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm("Delete", "Template"))
{ 
    @Html.ValidationSummary(false)      
    @Html.ListBoxFor(x => x.SelectedIds, new SelectList(Model.DisplayList, "Id", "Name"),new { @class = "select"} )
}

When I return this:

return PartialView(new ListViewModel<Template> { DisplayList = _templateDataProvider.GetTemplates() });

then it works, but I do not want to access the database again for this.

  • 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-04T14:13:42+00:00Added an answer on June 4, 2026 at 2:13 pm

    In your Delete POST action when there is an exception and you redisplay the partial view you are not passing any model to this view. And yet your view needs a DisplayList property in order to load the list box. So make sure you are passing a model tha same way you did in your GET action:

    var templates = _templateDataProvider.GetTemplates();
    var listViewModel = new ListViewModel<Template>();
    listViewModel.DisplayList = templates;
    return PartialView(listViewModel);
    

    Another possibility is to directly have the Delete action take the view model:

    [HttpPost]
    public ActionResult Delete(ListViewModel<Template> model)
    {
        if (ModelState.IsValid)
        {
            return Json(new { success = true });
        }
    
        // Make sure you rebind the DisplayList property
        model.DisplayList = _templateDataProvider.GetTemplates();
        return PartialView(model);
    }
    

    Now as far as the validation is concerned you could decorate your SelectedIds property on the view model with for example the Required attribute:

    [Required]
    public IEnumerable<Template> SelectedIds { get; set; }
    

    UPDATE:

    A performance concern has been expressed in the comments section about accessing the database to load the list again in the POST action. To avoid this performance hit the values could be cached and reused.

    Like this:

    public IEnumerable<Template> GetTemplates()
    {
        var templates = HttpContext.Cache["templates"] as IEnumerable<Template>;
        if (templates == null)
        {
            templates = _templateDataProvider.GetTemplates();
            HttpContext.Cache["templates"] = templates;
        }
        return templates;
    }
    

    and then:

    public ActionResult Delete()
    {
        var listViewModel = new ListViewModel<Template>();
        listViewModel.DisplayList = GetTemplates();
        return PartialView(listViewModel);
    }
    
    [HttpPost]
    public ActionResult Delete(ListViewModel<Template> model)
    {
        if (ModelState.IsValid)
        {
            return Json(new { success = true });
        }
    
        model.DisplayList = GetTemplates();
        return PartialView(model);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

...I want to Show the 'delete' button when user is an admin, and show
I have a view that contains a UITableView. I'm able to show the delete
link_to delete redirecting to show action instead of going to destroy action in rails
I am trying to delete checked items in my list view my app. The
I show to user a list of categories, he must choose one. How to
I show the data of a table (table1) in a QTableView. When the user
Some fancy websites show an error dialog when it is detected that an untrained
When I delete a file from my snapshot view, the next time I look
I am creating a web user control for showing Alert on AspxGridView Columns(Delete/Edit) click
My view is the users show page <%= link_to 'Cancel?', {:url => schedule_path, :id

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.