本文介绍了从类型'System.String'的参数转换为键入'T'失败,因为没有类型转换器可以将这些类型之间的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:所有code这里转述为例,作为我不能显示真正的code

NOTE: All of the code here is paraphrased as an example, as I can't show real code.

我有一个看起来像

public SearchViewModel
{
   public string Term
   {
     get; set;
   }

   public SearchResult Result
   {
     get; set;
   }

   public List<Filter> Filters
   {
     get; set;
   }
}

这势必会定期控制器,一切工作正常:

This is bound to a regular controller and everything works fine:

 public ActionResult Search (SearchViewModel model)
 {
      if (ModelState.IsValid)
      {
           model.Result = _searchService.Search(model.Term);
           return View(model);
      }
 }

我也有一个处理一个POST从包含复选框形式采取其他操作。该控制器用来处理创建过滤器类和重定向到搜寻行动。

I also have another action that handles taking in a POST from a form that contains checkboxes. This controller handles creating Filter classes and redirects to the Search action.

是这样的:

    public ActionResult Filter(FormCollection formParams)
    {
        return RedirectToAction("Search", new SearchViewModel
        {
            Term = formParams["Term"],
            Filters = 
                formParams.Keys
                    .Cast<String>()
                    .Where(k => k.Contains("filter"))
                    .Select(k => Filter.Build(k, formParams[k]))
                    .ToList()                        
        });                                        
    }

这传递与List集合填充回搜寻行动的视图模型。

This passes a ViewModel with the List collection populated back to the Search action.

然而,在搜索行动,ModelState.IsValid现在返回false。这是因为模型绑定抛出此异常:

However, in the search action, ModelState.IsValid now returns false. This is because the model binder throws this exception:

这类型的参数转换
  'System.String'输入'过滤器'
  失败,因为没有类型转换器可
  这些类型之间的转换。

综观ModelState中的过滤器节目的原始值是一个字符串:

Looking at the raw value in ModelState for "Filters" shows that is a string:

   System.Collections.Generic.List`1[Filter]

这似乎是表的实际内容动作之间的过渡过程中丢失,可能是因为它只是呼吁财产成员的ToString()。

It seems like the actual contents of the List is lost during the transition between actions, likely because it only called ToString() on the property members.

在这一点上,我为什么失败的一个模糊的概念,和我的数字,但是我可以写一个自定义的模型绑定或类型转换器,使其工作,我有一种感觉,这种做法的气味,这是大概的东西是小事,我只是接近它错了。

At this point, I have a vague idea of why this is failing, and I figure I could write a custom model binder or type converters to make it work, however, I have a feeling that this approach smells, and this is probably something that is trivial, I'm just approaching it wrong.

那么,什么是正确的ASP.NET MVC 3,从一个动作传递复杂类型的集合到另一个?

So, what is the proper ASP.NET MVC 3 to pass a collection of complex types from one action to another?

推荐答案

一个导致此错误的常见原因是使用某种保留字行动,注释,过滤器等。的。所以,用另一个名字改变了动作名称的过滤器的即的在applyFilter的可能会解决这个问题。希望这有助于...

One of the common reason causing this error is using the some kind of reserve words such as "Action, Comment, Filter, etc.". So, changing the Action name "Filter" with another name i.e. "ApplyFilter" will probably solve the problem. Hope this helps...

这篇关于从类型'System.String'的参数转换为键入'T'失败,因为没有类型转换器可以将这些类型之间的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 23:23