Beispiel #1
0
        // ############################################################

        // HTML Form, using the preferred way, a strongly-typed view model

        // We ALWAYS use two methods with an HTML Form
        // 1. One method displays the form
        // 2. The other method handles the form's posted data

        public ActionResult ViewModel()
        {
            // Fetch from session state
            List <Person> persons;

            persons = (List <Person>)Session["persons"] == null ?
                      new List <Person>() :
                      (List <Person>)Session["persons"];

            // Create person collection object
            var personcollection =
                new PersonCollection()
            {
                Persons = persons
                          .OrderBy(ln => ln.LastName)
                          .ThenBy(gn => gn.GivenNames)
                          .ToList()
            };

            // Add a status message
            switch (persons.Count)
            {
            case 0:
                personcollection.StatusMessage = "(no persons on the list yet)";
                break;

            case 1:
                personcollection.StatusMessage = "There is 1 person on the list";
                break;

            default:
                personcollection.StatusMessage =
                    string.Format("There are {0} persons on the list", persons.Count);
                break;
            }

            return(View(personcollection));
        }
        // ############################################################
        // HTML Form, using the preferred way, a strongly-typed view model
        // We ALWAYS use two methods with an HTML Form
        // 1. One method displays the form
        // 2. The other method handles the form's posted data
        public ActionResult ViewModel()
        {
            // Fetch from session state
            List<Person> persons;
            persons = (List<Person>)Session["persons"] == null ?
                new List<Person>() :
                (List<Person>)Session["persons"];

            // Create person collection object
            var personcollection =
                new PersonCollection()
                    {
                        Persons = persons
                          .OrderBy(ln => ln.LastName)
                          .ThenBy(gn => gn.GivenNames)
                          .ToList()
                    };
            // Add a status message
            switch (persons.Count)
            {
                case 0:
                    personcollection.StatusMessage = "(no persons on the list yet)";
                    break;
                case 1:
                    personcollection.StatusMessage = "There is 1 person on the list";
                    break;
                default:
                    personcollection.StatusMessage =
                        string.Format("There are {0} persons on the list", persons.Count);
                    break;
            }

            return View(personcollection);
        }