1

I have a model that looks like this:

 public class WorksheetViewModel() {
     public Worksheet Worksheet {get; set;}
     // ... more properties
 }

 public class Worksheet() {
     public List<LineItem> LineItems {get; set;}
     public Loan Loan {get; set; }
     // ... more properties
 }

 public class LineItem() {
     // ... some properties
 }

 public class Loan() {
     public List<LoanField> Fields {get; set;}
     // ... more properties
 }

 public class LoanField() {
     // ... some properties
 }

In my view I have the following:

 @Html.DisplayFor(m => m.Worksheet.LineItems)   // this works great
 @Html.DisplayFor(m => m.Worksheet.Loan.Fields) // this throws exception

I'm getting an exception that looks like this:

 The model item passed into the dictionary is of type 
 'System.Collections.Generic.List`1[DataContracts.LoanField]',
 but this dictionary requires a model item of type
 'DataContracts.LoanField'.

The display templates for both collections are similar in that both reference on the model they are are showing:

Working template:

 @model DataContracts.LineItem
 @Html.Label(Model.ItemType.Description) : @Model.RecipientEmployee.FullName<br />

Not working template:

 @model DataContracts.LoanField
 @Html.Label(Model.FieldName) : @Model.FieldValue<br />

Since the first template correctly accepts a List of LineItem objects then it would seem that the second template would behave the same way, but clearly its not in this case.

Anyone have an idea why the template is complaining about the list and a way to get around it?


wouldn't this work?

function filter_ptags_on_images($content){
   return preg_replace('/\<p\b.*?\>(.+)\<\/p\>/i', '$1', $content);
}

edit:

my bad, sorry I wasn't reading carefully

function filter_ptags_on_images($content){
   if (!preg_match_all('/\<p\b.*?\>(.+?)\<\/p\>/is',$content,$ps)) {
      return $content;
   }

   $new_content = $content;

   foreach ($ps[0] as &$p) {
      if (strpos($p,"<img ")) {
         $p_stripped_chunk = preg_replace('/\<p\b.*?\>(.*?\<img\b.+\>.*?)\<\/p\>/is', '$1', $p);
         $new_content = str_replace($p,$p_stripped_chunk,$new_content);
      }
   }

   return $new_content;
}

edit:

this is another better version I think:

function filter_ptags_on_images($content){
   if (!preg_match_all('/\<p\b.*?\>(.*?)\<\/p\>/is',$content,$ps_with_image)) {
      return $content;
   }

   foreach ($ps_with_image[0] as $match_x => $p) {
      if (!stripos($p,'<img')) {
         unset($ps_with_image[0][$match_x],$ps_with_image[1][$match_x]);
      }
   }

   return str_replace($ps_with_image[0], $ps_with_image[1], $content);
}

edit:

this is much much better version of this:

function filter_ptags_on_images($content){
   if (!preg_match_all('/\<p\b[^\>]*?\>(([^\<]|\<(?!\/p))*?\<img\b.+?\>.*?)\<\/p\>/is',$content,$ps_with_image)) {
      return $content;
   }
   return str_replace($ps_with_image[0], $ps_with_image[1], $content);
}
4

1 回答 1

1

在尝试了许多不太可能奏效的古怪技巧后,我决定尝试更改模板的名称……它奏效了。

问题是对所遵循的公约的最初误解。当我第一次创建一个显示模板时,我在显示属性名称后命名它......在本例中为“字段”。

愚蠢的我......我认为 MVC 会足够聪明,可以使用 @model 来解析类型。当我看到您可以将模板名称作为 Html.DisplayFor() 中的参数之一传递时,我认为这个假设得到了证实。

然而,正如你们大多数人现在可能猜到的那样,模板的名称必须与其显示的数据类型相同(在@model 语句中清楚地说明了相同的类型)。一旦我更改了模板的名称,一切正常。

于 2013-08-22T17:22:10.873 回答