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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:50:24+00:00 2026-05-17T02:50:24+00:00

ASP.NET MVC Model Binding is still new to me and I’m trying to understand

  • 0

ASP.NET MVC Model Binding is still new to me and I’m trying to understand exactly how it works. Right now, I appear to be having problems with a feature of Html.Textbox()

Specifically, I have a View where I set Html.Textbox to a value both in the “Get” and the “Post”. It sets fine in the “Get”, but after the user submits a value during the “Post”, I have the class change one of the values internally based on the other value submitted.

(I’m basically validating one value based on the other… I’m not sure if this is the right way to do this…)

Tracing through, I can see that the value has actually changed as expected both in the Model and in the View, but when it displays on my screen after the “Post”, the value does not display as it was changed. Instead it is what it was set to originally.

Here’s my simplified example:

The View shows a:

  • Drop-down with items from a SelectList (pre-selected as “Other”)
  • a Read-only Text Box (with a pre-loaded value of 0)
  • Submit Button

User should pick a new value from the Drop-Down and click submit. The “Post” method in the controller picks up the new value from the Drop-Down and changes the Value in the Read-only text-box and re-displays.

(Yes, I’ll eventually be doing this with JQuery, too…)

Here’s my sample Model class:

public class SampleSubmission
{
    public string Name { get; set; }
    public int Volume { get; set; }
    public readonly SortedList<string, int> NameVolumeList = new SortedList<string, int>();
    // Standard Constructor
    public SampleSubmission()
    {
        NameVolumeList.Add("Sample1", 10);
        NameVolumeList.Add("Sample2", 20);
        NameVolumeList.Add("Sample3", 50);
        NameVolumeList.Add("Other", 0);
        this.Name = NameVolumeList.Keys[0];
        this.Volume = NameVolumeList[Name];
    }
    // Copy Constructor
    public SampleSubmission(SampleSubmission samSub) : this()
    {
        this.Name = samSub.Name;
        this.Volume = NameVolumeList[Name];
    }
}

Here’s the Controller:

public class SampleSubmissionController : Controller
{
    [AcceptVerbs(HttpVerbs.Get)]
    public ActionResult Index()
    {
        SampleSubmission sampleSub = new SampleSubmission();
        return View(sampleSub);
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Index(SampleSubmission sampleSub)
    {
        SampleSubmission samSub = new SampleSubmission(sampleSub);
        return View(samSub);
    }
}

Here’s the View:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" 
Inherits="System.Web.Mvc.ViewPage<MvcModelBindTest.Models.SampleSubmission>" %>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% using (Html.BeginForm()) {  %>
<%= Html.DropDownList("Name", new SelectList(Model.NameVolumeList.Keys.ToList())) %>
<%= Html.TextBox("Volume",Model.Volume) %>
<input type="submit" name="pick" id="pick" value="Pick" /> <% } %>
</asp:Content>

Any ideas as to why the new value does not display?

EDIT:

In order to fix the problem, I read the link given by “jfar” and made a 1-line change.

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Index(SampleSubmission sampleSub)
    {
        SampleSubmission samSub = new SampleSubmission(sampleSub);
        // Reset Model Value
        ModelState.SetModelValue("Volume", new ValueProviderResult(
           samSub.Volume, "", System.Globalization.CultureInfo.CurrentCulture));
        return View(samSub);
    }

This definitely works. Unfortunately, this feels like a gross hack to me. What if I had to update the values of multiple fields? There must be a better (simpler?) way of doing this.

EDIT2: Found my answer. See below…

  • 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-17T02:50:24+00:00Added an answer on May 17, 2026 at 2:50 am

    I figured out the answer to my own question when I stumbled upon another variable that needed to be reset. As I was looking at the data structure, I realized what I wanted was the pristine state where there were no Keys in the ModelState.

            ModelState.Remove(key);
    

    Where “key” is the value you’re trying to reset.

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

Sidebar

Related Questions

Has the asp.net mvc team implemented a default model binding for enums? One that
Does ASP.NET MVC offer any simple way to get model binding to work when
ASP.NET MVC 2.0: Simple Model Binding not working/binding as it should i have a
I'm building an ASP.NET MVC 2 website and right now, I have ugly spaghetti
Asp.net-MVC now allows for implicit binding of DateTime objects. I have an action along
I have an ASP.NET MVC action method which uses model binding to accept a
I've been trying to use the default ASP.NET MVC model binders but I'm having
I have read Scott Guthrie's Blog - ASP.NET MVC 3: New @model keyword in
I would like to inject a dependency into an ASP.NET MVC model, but I
How can I access metadata (dataannotations attributes) in my asp.net mvc model class from

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.