当前位置: 首页>>代码示例>>C#>>正文


C# FormCollection.ToValueProvider方法代码示例

本文整理汇总了C#中System.Web.Mvc.FormCollection.ToValueProvider方法的典型用法代码示例。如果您正苦于以下问题:C# FormCollection.ToValueProvider方法的具体用法?C# FormCollection.ToValueProvider怎么用?C# FormCollection.ToValueProvider使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Web.Mvc.FormCollection的用法示例。


在下文中一共展示了FormCollection.ToValueProvider方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Edit

        public ActionResult Edit(int id, FormCollection forms)
        {
            if (id == 0)
            {
                CmsMenu newMenu = new CmsMenu()
                                   {
                                       Title = forms.GetValue("Title").AttemptedValue,
                                       Description = forms.GetValue("Description").AttemptedValue,
                                       Type = forms.GetValue("Type").AttemptedValue
                                   };
                if (newMenu.IsValid)
                {
                    return this.CreateNewMenu(newMenu);
                }

                ModelState.AddModelErrors(newMenu.GetRuleViolations());
                return View(newMenu);
            }

            CmsMenu menu = this.menuService.LoadMenu(id);
            UpdateModel(menu, forms.ToValueProvider());
            this.menuService.SaveMenu(menu);
            TempData["SuccessResult"] = "Menu was successfully saved";
            return this.View(menu);
        }
开发者ID:MasroorKhalid,项目名称:AtomicCms,代码行数:25,代码来源:MenuController.cs

示例2: Create

 public ActionResult Create(FormCollection collection)
 {
     var model = new Role();
     TryUpdateModel(model, collection.ToValueProvider());
     if (!ModelState.IsValid)
     {
         return View(model);
     }
     using (var session = new SessionFactory().OpenSession())
     {
         if (session.Load<Role>(m => m.Name.Equals(model.Name)) != null)
         {
             FlashFailure("角色名称[{0}]已经存在,创建失败!", model.Name);
             return View(model);
         }
         model.CreatedAt = DateTime.Now;
         model.CreatedBy = CurrentAccountNo;
         ViewData.Model = model;
         if (session.Create(model))
         {
             FlashSuccess("角色[{0}]创建成功", model.Name);
             return Close();
         }
         FlashFailure("创建角色[{0}]失败!", model.Name);
         return View(model);
     }
 }
开发者ID:dalinhuang,项目名称:info_platform_i,代码行数:27,代码来源:RoleController.cs

示例3: Create

 public ActionResult Create(FormCollection collection, long id = 0)
 {
     var model = new TrainManagementItem { TrainManId = id };
     TryUpdateModel(model, collection.ToValueProvider());
     if (!ModelState.IsValid)
     {
         return View(model);
     }
     using (var session = new SessionFactory().OpenSession())
     {
         session.BeginTransaction();
         var trainMan = session.Load<TrainManagement>(id);
         if (trainMan == null)
         {
             FlashError("培训不存在,请联系管理员!");
             return Close();
         }
         model.Year = trainMan.Year;
         model.TrainManName = trainMan.Name;
         model.CreatedAt = DateTime.Now;
         model.CreatedBy = CurrentAccountNo;
         ViewData.Model = model;
         if (session.Create(model))
         {
             session.Commit();
             FlashSuccess("创建记录成功!");
             return Close();
         }
         session.Rollback();
         FlashFailure("创建记录失败!");
         return View();
     }
 }
开发者ID:dalinhuang,项目名称:info_platform_i,代码行数:33,代码来源:TrainManagementItemController.cs

示例4: posting_invalid_data_to_edit_returns_view_and_adds_modelerror

        public void posting_invalid_data_to_edit_returns_view_and_adds_modelerror()
        {
            //arrange
            var id = 1;
            var name = string.Empty;
            var subject = "Welcome to Springfield!";
            var text = "Lorem ipsum dolor sit amet.";
            var html = "<p>Lorem <b>ipsum</b> dolor sit amet.</p>";
            var form = new FormCollection{
                                         	{ "Name", name },
                                         	{ "Subject", subject },
                                         	{ "Text", text },
                                         	{ "Html", html }
                                         };
            controller.ValueProvider = form.ToValueProvider();
            var ex = new ValidationException(
                new List<ValidationError>{
                                         	new ValidationError( "Name", "Name is a required field." )
                                         }
                );
            service.Expect( x => x.Update( It.IsAny<Message>() ) ).Throws( ex );

            //act
            var result = controller.Edit_Post( id );

            //assert
            Assert.IsAssignableFrom( typeof(ViewResult), result );
            var viewResult = (ViewResult)result;
            Assert.AreEqual( "", viewResult.ViewName );
            Assert.IsFalse( viewResult.ViewData.ModelState.IsValid );
        }
开发者ID:scriptstar,项目名称:MVCBookApplication,代码行数:31,代码来源:MessageControllerTest.cs

示例5: Register

        public ActionResult Register(FormCollection form)
        {
            _db = new renoRatorDBEntities();
            var newUser = new RegisterModel();

            //temp
            newUser.userTypeID = 1;

            // Deserialize (Include white list!)
            TryUpdateModel(newUser, new string[] { "fname", "lname", "email", "password" }, form.ToValueProvider());

            // Validate
            if (String.IsNullOrEmpty(newUser.fname))
                ModelState.AddModelError("fname", "First name is required!");
            if (String.IsNullOrEmpty(newUser.lname))
                ModelState.AddModelError("lname", "Last name is required!");
            if (String.IsNullOrEmpty(newUser.email))
                ModelState.AddModelError("email", "Email is required!");
            if (String.IsNullOrEmpty(newUser.password))
                ModelState.AddModelError("password", "Password is required!");
            if (newUser.password != form["passwordConfirm"])
                ModelState.AddModelError("passwordConfirm", "Passwords don't match!");
            if (newUser.email != form["emailConfirm"])
                ModelState.AddModelError("emailConfirm", "Email addresses don't match!");

            // If valid, save movie to database
            if (ModelState.IsValid)
            {
                newUser.Save();
                return RedirectToAction("Home");
            }

            // Otherwise, reshow form
            return View(newUser);
        }
开发者ID:Phillita,项目名称:RenoRator,代码行数:35,代码来源:UserController.cs

示例6: Create

        public ActionResult Create(FormCollection collection)
        {
            var model = new SchoolDepartment();
            TryUpdateModel(model, collection.ToValueProvider());
            if (!ModelState.IsValid)
            {
                return View(model);
            }
            using (var session = new SessionFactory().OpenSession())
            {
                //model.ParentId
                //model.LeaderId
                model.LeaderName = model.LeaderId == 0 ? "" : session.Load<User>(model.LeaderId).Realname;

                if (session.Load<SchoolDepartment>(m => m.Name.Equals(model.Name)) != null)
                {
                    FlashFailure("部门[{0}]已经存在", model.Name);
                    return View(model);
                }
                model.CreatedAt = DateTime.Now;
                model.CreatedBy = CurrentAccountNo;
                ViewData.Model = model;
                if (session.Create(model))
                {
                    FlashSuccess("部门[{0}]创建成功", model.Name);
                    return Close();
                }
                FlashFailure("创建部门[{0}]失败!", model.Name);
                return View();
            }
        }
开发者ID:dalinhuang,项目名称:info_platform,代码行数:31,代码来源:SchoolDepartmentController.cs

示例7: Editar

        public ActionResult Editar(int id, FormCollection collection)
        {
            var model = repository.Clientes.First(p => p.ClienteId == id);
            try
            {

                TryUpdateModel(model, new string[] { "ClienteId", "Nombre", "Apellido", "Direccion", "Email", "Telefono" }, collection.ToValueProvider());

                // Validadte

                if (String.IsNullOrEmpty(model.Nombre.ToString()))
                    ModelState.AddModelError("Nombre", "El Nombre es Requerido");

                if (ModelState.IsValid)
                {
                    repository.SaveDB();
                    return RedirectToAction("Index");
                }

            }
            catch
            {
                return View();
            }

            return View(model);
        }
开发者ID:jeanabreu,项目名称:PuestoDeQuipes,代码行数:27,代码来源:ClientesController.cs

示例8: Create

 public ActionResult Create(FormCollection collection)
 {
     var movieToCreate = new Movie();
     this.UpdateModel(movieToCreate, collection.ToValueProvider());
     // Insert movie into database
     return RedirectToAction("Index");
 }
开发者ID:bjeverett,项目名称:asp-net-mvc-unleashed,代码行数:7,代码来源:Movie2Controller.cs

示例9: Create

        public ActionResult Create(FormCollection collection)
        {
            //var item = new User();
            var item = new User() { CreatedOn = DateTime.Now};

            //please don't omit this...
            var whiteList=new string[]{"UserName","OpenID","Friendly","LastLogin","ModifiedOn"};
            UpdateModel(item,whiteList,collection.ToValueProvider());

            var dummy = item;

            if(ModelState.IsValid){
                try
                {
                    _session.Add<User>(item);
                    _session.CommitChanges();
                    this.FlashInfo("User saved...");
                    return RedirectToAction("Index");
                }
                catch
                {
                    this.FlashError("There was an error saving this record");
                    return View(item);
                }
            }
            return View(item);
        }
开发者ID:dancoppock,项目名称:MVC3-Starter-Site,代码行数:27,代码来源:UserController.cs

示例10: Create

 public ActionResult Create(FormCollection collection)
 {
     var model = new TrainManagement();
     TryUpdateModel(model, collection.ToValueProvider());
     if (!ModelState.IsValid)
     {
         return View(model);
     }
     using (var session = new SessionFactory().OpenSession())
     {
         session.BeginTransaction();
         model.CreatedAt = DateTime.Now;
         model.CreatedBy = CurrentAccountNo;
         ViewData.Model = model;
         if (session.Create(model))
         {
             session.Commit();
             FlashSuccess("创建记录成功!");
             return Close();
         }
         session.Rollback();
         FlashFailure("创建记录失败!");
         return View();
     }
 }
开发者ID:dalinhuang,项目名称:info_platform_i,代码行数:25,代码来源:TrainManagementController.cs

示例11: Edit

 public ActionResult Edit(FormCollection collection, long[] ids)
 {
     if (ids.Length == 0)
     {
         FlashWarn("请选择要修改的记录。");
         return Close();
     }
     using (var session = new SessionFactory().OpenSession())
     {
         var model = session.Load<School>(m => m.Id.In(ids));
         if (model == null)
         {
             FlashInfo("你没有选择任何可以修改的学校。");
             return Close();
         }
         TryUpdateModel(model, collection.ToValueProvider());
         if (!ModelState.IsValid)
         {
             return View(model);
         }
         model.UpdatedAt = DateTime.Now;
         model.UpdatedBy = CurrentAccountNo;
         if (session.Update(model))
         {
             FlashSuccess("学校更改成功");
             return Close();
         }
         return View();
     }
 }
开发者ID:dalinhuang,项目名称:info_platform,代码行数:30,代码来源:SchoolController.cs

示例12: BinderShouldBindValues

        public void BinderShouldBindValues() {
            var controllerContext = new ControllerContext();

            var binders = new ModelBinderDictionary {
                { typeof(Foo), new DefaultModelBinder() } 
            };

            var input = new FormCollection { 
                { "fooInstance[Bar1].Value", "bar1value" }, 
                { "fooInstance[Bar2].Value", "bar2value" } 
            };

            var foos = new[] {
                new Foo {Name = "Bar1", Value = "uno"},
                new Foo {Name = "Bar2", Value = "dos"},
                new Foo {Name = "Bar3", Value = "tres"},
            };

            var providers = new EmptyModelMetadataProvider();

            var bindingContext = new ModelBindingContext {
                ModelMetadata = providers.GetMetadataForType(() => foos, foos.GetType()),
                ModelName = "fooInstance", 
                ValueProvider = input.ToValueProvider() 
            };

            var binder = new KeyedListModelBinder<Foo>(binders, providers, foo => foo.Name);

            var result = (IList<Foo>)binder.BindModel(controllerContext, bindingContext);

            Assert.That(result.Count, Is.EqualTo(3));
            Assert.That(result[0].Value, Is.EqualTo("bar1value"));
            Assert.That(result[1].Value, Is.EqualTo("bar2value"));
        }
开发者ID:mofashi2011,项目名称:orchardcms,代码行数:34,代码来源:KeyedListModelBinderTests.cs

示例13: Approve

        public ActionResult Approve(FormCollection collection, long[] ids, long id = 0)
        {
            if (ids.Length == 0)
            {
                FlashWarn("请选择要批阅的记录。");
                return Close();
            }
            using (var session = new SessionFactory().OpenSession())
            {
                var model = session.Load<TrainRecordFile>(m => m.Id.In(ids));
                if (model == null)
                {
                    FlashInfo("你没有选择任何可以批阅的记录。");
                    return Close();
                }
                TryUpdateModel(model, collection.ToValueProvider());
                model.UpdatedAt = DateTime.Now;
                model.UpdatedBy = CurrentAccountNo;
                if (session.Update(model))
                {
                    FlashSuccess("批阅记录成功");
                    return Close();
                }
                FlashFailure("批阅记录失败,请联系管理员!");
                return View();

            }
        }
开发者ID:dalinhuang,项目名称:info_platform_i,代码行数:28,代码来源:TrainRecordFilesController.cs

示例14: CreateIngredient

        public ActionResult CreateIngredient(Ingredient ingredient, FormCollection form)
        {
            //if (ModelState.IsValid)
            //{

            //Ingredient ingredient = new Ingredient();
            // Deserialize (Include white list!)
            bool isModelUpdated = TryUpdateModel(ingredient, new[] { "IngredientName" }, form.ToValueProvider());
            
            // Validate
            if (String.IsNullOrEmpty(ingredient.IngredientName))
                ModelState.AddModelError("IngredientName", "Ingredient Name is required!");

            if (ModelState.IsValid)
            {
                var newIngredient = _ingredientApplicationService.CreateIngredient(ingredient.IngredientName);

                if (newIngredient != null)
                {
                    return RedirectToAction("EditIngredient", new{id = newIngredient.Id});
                }
                ModelState.AddModelError("IngredientName", "Ingredient or AlternativeIngredientName already exists!");
            }

            return View(ingredient);
        }
开发者ID:consumentor,项目名称:Server,代码行数:26,代码来源:IngredientController.cs

示例15: Add

        public ActionResult Add(FormCollection form)
        {
            var individualToAdd = new Individual();

            // Deserialize (Include white list!)
            TryUpdateModel(individualToAdd, new string[] { "Name", "DateOfBirth" }, form.ToValueProvider());

            // Validate
            if (String.IsNullOrEmpty(individualToAdd.Name))
                ModelState.AddModelError("Name", "Name is required!");
            if (String.IsNullOrEmpty(individualToAdd.DateOfBirth))
                ModelState.AddModelError("DateOfBirth", "DateOfBirth is required!");

            var error = individualToAdd.ValidateDateOfBirth();
            if (!String.IsNullOrEmpty(error))
                ModelState.AddModelError("DateOfBirth", error);

            // If valid, save Individual to Database
            if (ModelState.IsValid)
            {
                _db.AddToIndividuals(individualToAdd);
                _db.SaveChanges();
                return RedirectToAction("Add");
            }

            // Otherwise, reshow form
            return View(individualToAdd);
        }
开发者ID:ravibeta,项目名称:NameDOB,代码行数:28,代码来源:HomeController.cs


注:本文中的System.Web.Mvc.FormCollection.ToValueProvider方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。