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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:58:32+00:00 2026-06-05T21:58:32+00:00

For reference this is in a C# MVC2 website. I’m looking to save a

  • 0

For reference this is in a C# MVC2 website.

I’m looking to save a data using a Model in my database, but I need to do it with custom data rather than the FormCollection I am used to. Here is how I typically do it:

TryUpdateModel(userToUpdate, new string[] { "ID", "Name", "Age", "Gender" }, form.ToValueProvider());

// Model Validation is here, no need to see it so removed to save space

if (ModelState.IsValid)
{
     dbu.SaveChanges();
}

How do I go about replacing the form.ToValueProvider() with custom data? How should it be created/formatted?

  • 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-06-05T21:58:34+00:00Added an answer on June 5, 2026 at 9:58 pm

    You can create your own source by creating a NameValueCollection with your values, then using that to create a FormCollection, and then you can use that form collection as the value provider directly.

    Also, FormCollection has an Add method, where you can just add values directly.

    var values = new NameValueCollection { { "ID", "1" }, {"Name": "Bob"} }; // etc.
    var collection = new FormCollection(values);
    
    // or...
    // var collection = new FormCollection();
    // collection.Add("ID", "1");
    // collection.Add("Name", "Bob");
    // etc.
    
    TryUpdateModel(userToUpdate, new string[] { "ID", "Name", "Age", "Gender" }, form);
    

    If you are binding a flat model (say, User in this example), the above sample will be enough. However, if your fields have a prefix (this might be the case if you are doing a deep model binding), separate the prefix with a dot:

    var collection = new FormCollection();
    collection.Add("User.ID", "1");
    collection.Add("User.Name", "Bob");
    
    // Binds to fields with the prefix "User"
    TryUpdateModel(userToUpdate, "User", new string[] { "ID", "Name", "Age", "Gender" }, null, form);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to find any reference for this function, but I haven't found anything.
I have upgrade my MVC2 project to MVC3 using this tool: http://blogs.msdn.com/b/marcinon/archive/2011/01/13/mvc-3-project-upgrade-tool.aspx Which is
I am using asp.net mvc2 and mef to create pluggable architecture from this article
Is it OK to reference this when initializing a field? public class MainClass {
In reference to this answer to a Stack Overflow question : what is bench-testing
an svn:externals reference like this dojo -r 21434 http://svn.dojotoolkit.org/src/dojo/trunk/ is happily accepted by subversive
With the reference to this question:- JavaScript -Change CSS color for 5 seconds Working
I get a null - reference in this Code-line: **DataRow dr = tableSelectedItems.NewRow();** I
I am doing pass-by-reference like this: use strict; use warnings; sub repl { local
I'm not interested in call or apply to change the this reference. Just for

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.