0

I've created a partial view named "_ColorWheel.cshtml" which a string that holds the HEX value of the color.

This is the base model

public class PageComponentModel
{    
    // .....
    public string BgColorVal { get; set; } 
}

This model inherits PageComponentModel

public class OneColumnComponentModel: PageComponentModel
{
    // ....
}

On their respective views, I use the same line @Html.Partial("_ColorWheel",Model.BgColorVal) to create the control. The view of OneColumnComponentModel works well, but PageComponentModel's throws

The model item passed into the dictionary is of type '.....Model.PageComponentModel', but this dictionary requires a model item of type 'System.String'.

I've tried multiple times to copy and paste the line just in case of typos or whatnot. Setting a breakpoint in razor shows that BgColorVal is string and has the correct value.

Liaoo
  • 425
  • 6
  • 21
  • 1
    Possible duplicate of [The model item passed into the dictionary is of type .. but this dictionary requires a model item of type](https://stackoverflow.com/questions/40373595/the-model-item-passed-into-the-dictionary-is-of-type-but-this-dictionary-requ) –  Jan 29 '18 at 06:29
  • 1
    See the section of the dupe titled _Passing the wrong model from a view to a partial view_ –  Jan 29 '18 at 06:30
  • And I assume you also have a typo - it would be capital `M` - `@Html.Partial("_ColorWheel", Model.BgColorVal)` –  Jan 29 '18 at 06:41
  • M is capitalized in the code. No problem there. I'm guessing that BgColorVal is null. But if is, then, why are the views with inherited models working. They go through the same process (is pipeline the correct term?) I'll gonna test the null bit out in a while – Liaoo Jan 29 '18 at 07:03
  • You have not shown either of the GET methods that are initializing your models and setting the value of `BgColorVal` so impossible to tell (but the error means that `BgColorVal` is `null`) –  Jan 29 '18 at 07:05
  • So BgColorVal was indeed null. Doesn't explain why inherited classes work though. I didn't initialize their BgColorVal neither. Weird. Anyway, made the necessary changes. Thanks! – Liaoo Jan 29 '18 at 07:07
  • You are passing a value in the `OneColumnComponentModel` view, or you would get a similar error (perhaps in a constructor for that class?). Suggest you accept the dupe to close this out. –  Jan 29 '18 at 07:34
  • I don't get an error from OneColumnComponentModel. BgColorVal isn't initialized either. – Liaoo Jan 29 '18 at 11:58
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/164086/discussion-between-stephen-muecke-and-andrew-liao). –  Jan 29 '18 at 12:02

0 Answers0