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