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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:44:44+00:00 2026-05-20T23:44:44+00:00

I am doing [this tutorial][1] over at www.asp.net. It is written for ASP.NET MVC

  • 0

I am doing [this tutorial][1] over at http://www.asp.net. It is written for ASP.NET MVC 1. Although most of the stuff works for MVC 3, I do have a question regarding this code:

ASP.NET MVC 1.0 (straight from the tutorial)

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Exclude = "Id")] Contact contactToCreate)
{
    if (!ModelState.IsValid)
        return View();

    try
    {
        _entities.AddToContactSet(contactToCreate);
        _entities.SaveChanges();
        return RedirectToAction("Index");
    }
    catch
    {
        return View();
    }
}

ASP.NET MVC 3 (default Create action generated by MVC 3 Project, except my _entities object)

[HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                _entities.AddToContacts(collection); // synax error
                _entities.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

So my question is why do we pass Contact contactToCreate in the first case, but FormCollection collection in the second?

I could easily change argument in the second example to just Contact Contact contactToCreate, but I am curious as to what is this FormCollection collection stuff? Why did MVC 3 generate it for us? If it’s possible to use FormCollection instead, how would the modified example look like?
[1]: http://www.asp.net/mvc/tutorials/iteration-1-create-the-application-cs

  • 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-20T23:44:44+00:00Added an answer on May 20, 2026 at 11:44 pm

    FormCollection is the values entered by the user and Contact is the enderlying entity that you are creating.

    Remember that this method is essentially called directly by the user as the result of a HTTP post and its bad practice to be blindly accepting user input without validating it. This and the fact that in real-world scenarios the values available to you in a HTTP post request don’t exactly match up to your data entity properties is probably the reason why its probably a good idea to accept a FormCollection (or other abstracted input representing your form values) rather than directly accepting an entity in your database.

    Personally my modified example would look a little like this:

    [HttpPost]
    public ActionResult Create(FormCollection collection)
    {
        try
        {
            var contactToCreate = validateAndCreateContact(collection);
            if (contactToCreate != null)
            {
                _entities.AddToContacts(collection);
                _entities.SaveChanges();
                return RedirectToAction("Index");
            }
        }
        catch (Exception ex)
        {
            // Logic to display error on the view goes here, e.g.
            base.ViewData["Exception"] = ex;
        }
        return View();
    }
    
    private Contact validateAndCreateContact(FormCollection collection)
    {
        // Your implementation here
    }
    

    The validateAndCreateContact should validate user input and return a suitable Contact object if the input is all OK. If not it should return null and handle displaying a suitable error to the user (for example by setting properties on the ViewData).

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

Sidebar

Related Questions

Working through a tutorial (Professional ASP.NET MVC - Nerd Dinner), I came across this
Sorry for stupid questions, I'm doing everything as described in this tutorial: http://www.functionx.com/visualc/howto/calldlgfromdlg.htm I
I'm doing this tutorial: http://www.phpeveryday.com/articles/Zend-Framework-Database-Creating-Input-Form-P494.html We are building a simple input form using POST
I have some code doing this : var changes = document.getElementsByName(from); for (var c=0;
I'm probably doing this all wrong. I have a text file full of data
I have been doing research on HTML canvas libraries and I came across this
I have a question that may have been answered over 9000 times before but
I am doing this tutorial that would return the GPS coordinates. When I run
I'm currently doing this tutorial , but the tutorial uses Glassfish as the server
I am trying to make my app localized. I have followed this procedure http://www.switchonthecode.com/tutorials/a-simple-localization-example-for-the-iphone

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.