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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:01:07+00:00 2026-05-24T19:01:07+00:00

I want to change form value before pass it to action of controller. but

  • 0

I want to change form value before pass it to action of controller. but it throws Collection is read-only.

public class PersonController : Controller
{
    public ActionResult Add()
    {
        return View();
    }

    [HttpPost]
    [PersianDateConvertor("birthday")]
    public ActionResult Add(FormCollection collection)
    {
        string firstName = collection["firstName"];
        string lastName = collection["lastName"];
        string birthday = collection["birthday"];

        return View();
    }
}
public class PersianDateConvertorAttribute : ActionFilterAttribute
{
    string[] fields;
    public PersianDateConvertorAttribute(params string[] persianDateFieldNames)
    {
        if (persianDateFieldNames == null)
            fields = new string[] { };
        else
            fields = persianDateFieldNames;
    }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        foreach (var field in fields)
        {
            string value = filterContext.HttpContext.Request.Form[field];
            filterContext.HttpContext.Request.Form.Remove(field); //throws Collection is read-only
            filterContext.HttpContext.Request.Form.Add(field, ConvertToGregorian(value));
            // or filterContext.HttpContext.Request.Form[field] = ConvertToGregorian(value);
        }

        base.OnActionExecuting(filterContext);
    }
}
  • 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-24T19:01:08+00:00Added an answer on May 24, 2026 at 7:01 pm

    If I understand correctly, you want to modify the behaviour of the DateTime during the binding process. Instead of using an attribute, I would use a ModelBinder to change the format of the date string.

    I did something similar for a problem while converting decimal values from multiple cultures: (the code is taken from a blog post, it’s not mine, but I don’t remember the source. Sorry)

    using System;
    using System.Globalization;
    using System.Web.Mvc;
    
    public class DecimalModelBinder : IModelBinder
    {
      public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
      {
        ValueProviderResult valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
        ModelState modelState = new ModelState { Value = valueResult };
        object actualValue = null;
        try
        {
          actualValue = Convert.ToDecimal(valueResult.AttemptedValue, CultureInfo.CurrentCulture);
        }
        catch (FormatException e)
        {
          modelState.Errors.Add(e);
        }
        bindingContext.ModelState.Add(bindingContext.ModelName, modelState);
        return actualValue;
      }
    }
    

    in global.asax you register the binder

    protected void Application_Start()
    {
      AreaRegistration.RegisterAllAreas();
      RegisterGlobalFilters(GlobalFilters.Filters);
      RegisterRoutes(RouteTable.Routes);
    
      ModelBinders.Binders.Add(typeof(decimal), new DecimalModelBinder());
    
    }
    

    Imo this is a better approach and you don’t have to put an attribute to every action

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

Sidebar

Related Questions

I want to cange the lables in my form according to particular value in
I want to change form attributes with JQuery. In other browsers it works fine,
I want to change out my previous main form for a new one. According
i have a web form and i want to change its titles for each
I want a form where user can change password. I am able to encrypt
I have a label in my form. I want this label to dynamically change
I have a situation when I want to change the selected value of a
Let's say I want to create a collection class that is thread-safe by default.
I have such a problem - I want to change value of an element
i want change the content of a UITableView when i change a button, how

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.