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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:42:48+00:00 2026-05-14T01:42:48+00:00

I have an ASP.Net MVC Controller with a ‘MapColumns’ action along with a corresponding

  • 0

I have an ASP.Net MVC Controller with a ‘MapColumns’ action along with a corresponding ViewModel and View.

I’m using the defaultModelBinder to bind a number of drop down lists to a Dictionary in the ViewModel. The view model also contains an IList field for both source and destination columns which are used to render the view.

My question is what to do when validation fails on the Post call to the MapColumns action?

Currently the MapColumns view is returned with the ViewModel resulting from the default binding. This contains the Dictionary values but not the two lists used to render the page. What is the best way to re-provide these to the view?

I can set them explicitly after failed validation, but if obtaining these values (via GetSourceColumns() and GetDestinationColumns() in the example) carries any overhead this doesn’t seem ideal. What I am looking for is a way to retain these lists when they are not bound to the model from the view.

Here is some code to illustrate:

public class TestViewModel
{
    public Dictionary<string, string> ColumnMappings { get; set; }

    public List<string> SourceColumns;
    public List<string> DestinationColumns;
}

public class TestController : Controller
{
    [AcceptVerbs(HttpVerbs.Get)]
    public ActionResult MapColumns()
    {
        var model = new TestViewModel;
        model.SourceColumns = GetSourceColumns();
        model.DestinationColumns = GetDestinationColumns();     
        return View(model);
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult MapColumns(TestViewModel model)
    {
        if( Validate(model) )
        {
            // Do something with model.ColumnMappings
            RedirectToAction("Index");
        }
        else
        {
             // Here model.SourceColumns and model.DestinationColumns are empty
            return View(model);
        }
    }

}

The relevant section of MapColumns.aspx:

<%
int columnCount = 0;
foreach(string column in Model.targetColumns)
{%>
        <tr>
            <td> 
                <input type="hidden" name="ColumnMappings[<%= columnCount %>].Value" value="<%=column %>" />
                <%= Html.DropDownList("ColumnMappings[" + columnCount + "].Key", Model.DestinationColumns.AsSelectItemList())%>
            </td>            
        </tr>
    <%
    columnCount++;
}%>
  • 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-14T01:42:49+00:00Added an answer on May 14, 2026 at 1:42 am

    You’ll have to rebind your model if validation fails. In your else statement just add the, model.SourceColumns = GetSourceColumns(); and model.DestinationColumns = GetDestinationColumns(); before returning the view again.

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

Sidebar

Related Questions

I have an ASP.NET MVC 3 controller action. That action is defined as follows:
ASP.Net MVC Controllers have several methods of forwarding control to another controller or action.
I have an ASP.net MVC controller called Designs that has an action with the
I have an ASP.NET MVC 3 Controller Action that accepts an object as follows:
I have ASP.NET MVC controller named dictionary with method ControlsLangJsFile. Method returns view of
In my ASP.NET MVC app, I have a view that's calling Html.Action(MyAction, this.Model) -
If I have an ASP.NET MVC controller action that is called from a jQuery
In my ASP.NET MVC controller classes I have action methods. In my views I
I have an Action method in an ASP.NET MVC Controller class that handles form
I am using ASP.NET MVC 2, .NET 4.0. I have a controller that disables

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.