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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:21:01+00:00 2026-05-28T06:21:01+00:00

I am working with MVC3 and using a custom ModelBinder to pass around the

  • 0

I am working with MVC3 and using a custom ModelBinder to pass around the ProducerListViewModel that I built.

Here is the controller code I am currently working with:

    Function Filter(user As UserModel, viewModel As ProducerListViewModel) As ActionResult

        If IsNothing(viewModel) Then
            viewModel = New ProducerListViewModel(user)
        End If

        Return View(viewModel)

    End Function

    <HttpPost()> _
    Function Filter(user As UserModel, viewModel As ProducerListViewModel, <Bind(Prefix:="Filter")> filterModel As ProducerFilterModel) As ActionResult
        'update the filter in the view model and send off to the list method
        viewModel.Filter = filterModel
        Return RedirectToAction("List")
    End Function

    Function List(user As UserModel, viewModel As ProducerListViewModel) As ActionResult

        Return Nothing

    End Function

This is the code for the model that is bound in the custom ModelBinder

<Serializable()> _
<ModelBinder(GetType(ProducerListViewBinder))> _
Public Class ProducerListViewModel

    <XmlIgnore()> _
    Public Property Producers As IEnumerable(Of ProducerModel)
    Public Property PagingInfo As New PagingInfoModel("Load More Producers")
    Public Property Filter As New ProducerFilterModel()

    Public Sub New(user As UserModel)
        Me.Filter = ProducerFilterBL.Retrieve(user)
    End Sub

End Class

Here is the code for the Binder:

Public Class ProducerListViewBinder
    Implements IModelBinder

    Private Const __sessionKey As String = "ProducerListView"

    Public Function BindModel(controllerContext As ControllerContext, bindingContext As ModelBindingContext) As Object Implements IModelBinder.BindModel

        Dim filter As ProducerListViewModel

        'check to see if the filter exists in session
        If IsNothing(controllerContext.HttpContext.Session(ProducerListViewBinder.__sessionKey)) Then
            'load existing filter for user and store for later retrieval
            Dim user As UserModel = UserBL.RetrieveUser()
            filter = New ProducerListViewModel(user)
            ProducerListViewBinder.SetItem(filter)
        Else
            filter = CType(controllerContext.HttpContext.Session(ProducerListViewBinder.__sessionKey), ProducerListViewModel)
        End If

        Return filter

    End Function

End Class

The flow goes like this:

  1. User navigates to filter page, hitting the first action (Filter)
  2. User makes changes to the filter and submits the page, hitting the second action (Filter w/ Post)
  3. The filter w/ Post action receives the updated filter using model binding (third parameter filterModel As ProducerFilterModel), updates the ProducerListFilterModel, and redirects to the List action

Everything works just fine, but here is my question:

Why does the List action have the updated version of the ProducerFilterModel inside of the ProducerListViewModel?

I love that it works so perfectly, I just want to know why it works.

  • 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-28T06:21:02+00:00Added an answer on May 28, 2026 at 6:21 am

    Now that you have shown the code for your model binder everything is very clear. This model binder stores the ProducerListViewModel instance into the session which is what allows it to survive the redirect.

    The first time the POST Filter action is hit, there’s nothing in the session, so your custom model binder does some database lookup or something to retrieve the value:

    Dim user As UserModel = UserBL.RetrieveUser()
    filter = New ProducerListViewModel(user)
    ProducerListViewBinder.SetItem(filter)
    

    and then stores this value into the session. I guess it is the ProducerListViewBinder.SetItem that does this job. Unfortunately you haven’t shown the code of it but I am ready to bet 5 bucks that it’s what it do.

    Then the Filter action executes, and at the end it redirects to the List action which takes a ProducerListViewModel as argument. So your custom model binder kicks in again but this time it finds the instance it previously stored into the session and it simply returns it from there.

    So there’s no magic here. It simply uses the ASP.NET session in order to persist the values between the redirects.

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

Sidebar

Related Questions

I've got a webpage built using MVC3 and .NET 4 Validation is working great
I am working on a mvc3.0 app using EF code first and mvc scaffolding.
I'm working in a MVC3 application using Custom Membership Provider. The problem is when
I'm working with ASP.NET MVC3 using EF and Code First. I'm writing a simple
I have the first large solution that I am working on using MVC3. I
I'm using MVC3 razor, and I'm trying to pass an object to a partial
I'm working on a new Asp.Mvc3 application that will go line about the same
I'm currently working on an MVC 3 project using Ninject as my DI, the
I am working on a MVC3 application that will be hosted on IIS7 with
I'm working on a large Asp.Net MVC3 application (>50 views) and we are currently

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.