0

Good day,

I have a layout with a search box(with bootstrap and a self-made jquery typeahead).

    <div class="navbar navbar-inverse navbar-fixed-top">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            @Html.ActionLink("EbanTabs", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
        </div>

        <div class="navbar-collapse collapse">
            @Html.Partial("_TipoProyectoSearch") //<<my searchbox comes here
            @*<form class="navbar-form navbar-left">
                <div class="form-group">
                    <input type="text" class="form-control" placeholder="Search">
                </div>
                <button type="submit" class="btn btn-default">Submit</button>
            </form>*@
            <ul class="nav navbar-nav">
                @*<li>@Html.ActionLink("About", "About", "Home")</li>
        <li>@Html.ActionLink("Contact", "Contact", "Home")</li>*@
            </ul>
            @Html.Partial("_LoginPartial") 
        </div>

    </div>
</div>

you can see that my searchbox is a partialview that uses

//tipoproyectosearch partialview
@model MyProj.Models.Autocomplete
//start view
//...
//end view

This is the AutoComplete model

 public class Autocomplete
{
    public Guid Id { get; set; }

    public string Name { get; set; }
}

My problem started when I created my home index view. I used a new model there

@model  IEnumerable<MyProj.ViewModels.MyViewModel>
//here starts my view
//...
//end view

when I run my application I receive this error

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[MyProj.ViewModels.MyViewViewModel]', but this dictionary requires a model item of type 'EbanTabs.Models.Autocomplete'.

The error message tells me that the model is not right, when the partialview is rendered.

How can I do to avoid this problem? I could see that some answers *MVC 5 Multiple Models in a Single View * that use a complex view model with two classes.

I think that this approach would add more complexity to my solution, because I would have to create many complex models when rendering data to my view.

Is there a better approach than the mentioned?

thanks in advance.

J Smith
  • 35
  • 4
  • Use aview model, or use `@Html.Partial("_LoginPartial", new LoginModel())` or `@Html.Action(actionName, controllerName)` to call a `[ChildActionOnly]` controller method that returns a partial view –  Mar 30 '18 at 04:42
  • hi Stephen, can you write an example please? I am quite new with mvc – J Smith Mar 30 '18 at 04:43
  • But why are you generating a login form in your layout - a user only logs in once, so you are unnecessarily generating extra html. –  Mar 30 '18 at 04:43
  • sorry, I will correct it . the loginpartial is not the view I am having problems, is @Html.Partial("_TipoProyectoSearch") – J Smith Mar 30 '18 at 04:45
  • Then just use `@Html.Partial("_TipoProyectoSearch", new Autocomplete())` –  Mar 30 '18 at 04:48
  • sorry for my question, but, when I use @Html.Partial("_TipoProyectoSearch", new Autocomplete()) in my layout, it is missing the reference to Autocomplete – J Smith Mar 30 '18 at 04:51
  • If you need to initialize a new `Autocomplete ` and set its properties, then use `Html.Action()` as noted above (and in the dupe) –  Mar 30 '18 at 04:52
  • Refer [this answer](https://stackoverflow.com/questions/47176728/passing-a-viewmodel-to-a-partial-view-call-in-the-layout-cshtml/47176840#47176840) for another example –  Mar 30 '18 at 04:53

0 Answers0