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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:42:35+00:00 2026-05-15T09:42:35+00:00

I have a view where I want to perform different actions on the items

  • 0

I have a view where I want to perform different actions on the items in each row in a table, similar to this (in, say, ~/Views/Thing/Manage.aspx):

<table>
  <% foreach (thing in Model) { %>
    <tr>
      <td><%: thing.x %></td>
      <td>
        <% using (Html.BeginForm("SetEnabled", "Thing")) { %> 
          <%: Html.Hidden("x", thing.x) %>
          <%: Html.Hidden("enable", !thing.Enabled) %>
          <input type="submit"  
                 value="<%: thing.Enabled ? "Disable" : "Enable" %>" />
        <% } %>
      </td>    
      <!-- more tds with similar action forms here, a few per table row -->     
   </tr>
  <% } %>

In my ThingController, I have functions similar to the following:

public ActionResult Manage() {
  return View(ThingService.GetThings());
}

[HttpPost]
public ActionResult SetEnabled(string x, bool enable) {
  try {
    ThingService.SetEnabled(x, enable);
  } catch (Exception ex) {
    ModelState.AddModelError("", ex.Message); // I know this is wrong...
  }
  return RedirectToAction("Manage");
}

In the most part, this is working fine. The problem is that if ThingService.SetEnabled throws an error, I want to be able to display the error at the top of the table. I’ve tried a few things with Html.ValidationSummary() in the page but I can’t get it to work.

Note that I don’t want to send the user to a separate page to do this, and I’m trying to do it without using any javascript.

Am I going about displaying my table in the best way? How do I get the errors displayed in the way I want them to? I will end up with perhaps 40 small forms on the page. This approach comes largely from this article, but it doesn’t handle the errors in the way I need to.

Any takers?


Solved thanks to @Shaharyar:

public ActionResult Manage() {
  if (TempData["Error"] != null)
    ModelState.AddModelError("", TempData["Error"] as string);
  return View(ThingService.GetThings());
}

[HttpPost]
public ActionResult SetEnabled(string x, bool enable) {
  try {
    ThingService.SetEnabled(x, enable);
  } catch (Exception ex) {
    TempData["Error"] = ex.Message;
  }
  return RedirectToAction("Manage");
}

Then just a small form for the ValidationSummary at the top of my table.

<% using (Html.BeginForm()) { %>
  <%: Html.ValidationSummary(false) %>
<% } %>

Thanks!

  • 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-05-15T09:42:35+00:00Added an answer on May 15, 2026 at 9:42 am

    Let’s try…

    There is a TempData dictionary available for you to do this kind of things.

    And you might have to rely on another page to handle the errors for you.

    Because an exception will be thrown as soon as the ViewModel can’t be passed to the View.

    But if the model has some problems, you could do the following (just pass an empty model to the view):

    public SetEnabled(string x, bool enable) {
      try {
        ThingService.SetEnabled(x, enable);
        return View(viewModel);
      } catch {
        TempData["GetThingsError"] = "Oops! Some error happened while processing your request!"
        return View(); 
        /*
         * Note that you can also pass the initial model
         * back to the view -> this will do the model validation 
         * (specified in your model) 
         */
      }
      return RedirectToAction("Manage");
    }
    

    The TempData message is only available for the current request and will be gone after a refresh.

    It might need some further tweaking but that would be the direction to do that kind of error reporting to the user/customer.

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

Sidebar

Related Questions

I have a grid view will two different button columns. I want to perform
Let's say I have a split view, and I want to fill half of
I have a view that I want to add some custom drawing to. I
I want to have a abstract view for any type of UI (web or
I have a view user control that can post form. This control can be
I have an iPhone app, it has two separate MKMapView components in different views,
We want to perform some calculations on some values in a view...so when the
I have a view that has a list of jobs in it, with data
I have a view using a master page that contains some javascript that needs
We have a View (call it X) that is the base view called by

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.