代码之家  ›  专栏  ›  技术社区  ›  Nifle Hassan Syed

trim()表单集合值

  •  1
  • Nifle Hassan Syed  · 技术社区  · 15 年前

    我在玩Nerddinner教程晚餐编辑控件。
    我得到一个formCollection作为参数之一,在使用updateModel()之前,我可以修剪其中的数据吗?

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection formValues)
        {
            Dinner dinner = dinnerRepository.GetDinner(id);
            try
            {
                UpdateModel(dinner);
                dinnerRepository.Save();
                return RedirectToAction("Details", new { id = dinner.DinnerID });
            }
            catch
            {
                foreach (var issue in dinner.GetRuleViolations())
                {
                    ModelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
                }
                return View(dinner);
            }
        }
    

    或者我必须通过迭代请求来手动完成这项工作。表单键?

    1 回复  |  直到 15 年前
        1
  •  2
  •   nikmd23    15 年前

    您可以使用模型绑定器绑定到自定义对象,而不是使用原始表单值。

    您可以通过实现IModelBinder接口创建自己的模型绑定器。在imodelbinder.bindmodel方法中,可以修剪或执行任何其他所需的字符串操作。

    完成此操作后,您的操作将按您想要的方式接收格式化的数据。

    欲了解更多信息, K Scott Allen 斯科特·汉塞尔曼有几篇关于iModelBinder的文章。