How to use an editor template in different views

Kenshou

I have a sidebar on my website that contains a textbox binded to a viewmodel and needs to be on all of the views of my website. First I created both the viewmodel class and the viewmodel cshtml file (both in the Shared folder):

Class:

namespace CaseLaw.Web.Models.Shared
{
    public class SidebarViewModel
    {
        public string SearchString { get; set; }
    }
}

EditorTemplate:

@model CaseLaw.Web.Models.Shared.SidebarViewModel

<div class="form-group">
    <div class="input-group">

        @Html.TextBoxFor(m => m.SearchString, new { @class = "form-control", placeholder = "Search the Database" })

        <span class="input-group-btn">
            <input type="submit" class="btn btn-default caselawMainbtn" value="Search" />
        </span>
    </div>
</div>

After that, I created a Partial View that calls the EditorTemplate:

@model CaseLaw.Web.Models.Shared.SidebarViewModel

<div class="row caselawSidebarImg">
    <h2 id="caselawTitle">Caselaw</h2>
    <h4 id="caselawSubTitle">The complete database.</h4>
</div>
<div class="row caselawSearchForm">
    <div class="col-sm-12">

        @using (Html.BeginForm("Index", "OpinionDocument", new { SearchString = Model.SearchString }, FormMethod.Get, new { @class = "form-inline" }))
        {
            @Html.EditorForModel()
        }

    </div>
</div>
<div class="row caselawSidebarMenu">
    <div class="col-sm-12">
        <ul>
            <li><a href="#">Dashboard</a></li>
            <li><a href="#">Your Notes</a></li>
            <li><a href="#">Your Bookmarked Cases</a></li>
            <li><a href="#">Your Projects</a></li>
            <li><a href="#">Recently Viewed</a></li>
        </ul>
    </div>
</div>

Then, i called the partial view on one of my views:

 <div class="col-sm-2 caselawSidebar">
       @{Html.RenderPartial("_Sidebar");}
 </div>

The problem, is that that view is binded to another model other than the model needed in the editortemplate inside the partialview, so the application throws an error. How can I send the correct model to the Partialview, and be able to use it throughout my website?

Thanks!

Kenshou

The solution was quite simple: I just needed to create a new object, like this:

 @{Html.RenderPartial("_Sidebar", new CaseLaw.Web.Models.Shared.SidebarViewModel());}

I tried this before and didn't work, the problem was that I forgot to add "new" before the model.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How do I Change window size on kendo grid editor template?

분류에서Dev

How to use PhpStorm Foundation template?

분류에서Dev

AngularJS - How to Use Service in Template

분류에서Dev

Use "if" in Backbone/Underscore template, how?

분류에서Dev

Using viewmodel to pass different views to partial views

분류에서Dev

How can I use custom colours in the Microsoft VBA editor?

분류에서Dev

How to use 'itextESC' when using ex as a text editor in command line?

분류에서Dev

How to pass a filtered object variable from views.py to a HTML template

분류에서Dev

How to set ng-show on a specific element through different views/controllers?

분류에서Dev

How to use the Ubuntu One client to sync different folders to different accounts

분류에서Dev

How to configure ssh to use different key?

분류에서Dev

How to use Query Builder when different conditions

분류에서Dev

How to use same entity in different relations in JPA?

분류에서Dev

How to use different colors of Circles using JSON

분류에서Dev

How to use templated class in constructor for a different class

분류에서Dev

How to use different directory to serve static assets?

분류에서Dev

How to use paste command for different lengths of columns

분류에서Dev

Lost views in template when change record of a route

분류에서Dev

Use template to apply a function template

분류에서Dev

How to use Template Literal Types as a return type property in TypeScript

분류에서Dev

How to use client template expressions in ajax binding of a mvc kendo grid?

분류에서Dev

How can we use a variable of the type of the template parameter?

분류에서Dev

How to use constant in class namespace as the array and template parameters?

분류에서Dev

How to use ng-class on a top level container when using nested views

분류에서Dev

MeteorJs Template conditions to display different template

분류에서Dev

Different compilers behavior for template specialization

분류에서Dev

creating template function with different parameters

분류에서Dev

Template instance in different translation units

분류에서Dev

Which WPF template to use?

Related 관련 기사

  1. 1

    How do I Change window size on kendo grid editor template?

  2. 2

    How to use PhpStorm Foundation template?

  3. 3

    AngularJS - How to Use Service in Template

  4. 4

    Use "if" in Backbone/Underscore template, how?

  5. 5

    Using viewmodel to pass different views to partial views

  6. 6

    How can I use custom colours in the Microsoft VBA editor?

  7. 7

    How to use 'itextESC' when using ex as a text editor in command line?

  8. 8

    How to pass a filtered object variable from views.py to a HTML template

  9. 9

    How to set ng-show on a specific element through different views/controllers?

  10. 10

    How to use the Ubuntu One client to sync different folders to different accounts

  11. 11

    How to configure ssh to use different key?

  12. 12

    How to use Query Builder when different conditions

  13. 13

    How to use same entity in different relations in JPA?

  14. 14

    How to use different colors of Circles using JSON

  15. 15

    How to use templated class in constructor for a different class

  16. 16

    How to use different directory to serve static assets?

  17. 17

    How to use paste command for different lengths of columns

  18. 18

    Lost views in template when change record of a route

  19. 19

    Use template to apply a function template

  20. 20

    How to use Template Literal Types as a return type property in TypeScript

  21. 21

    How to use client template expressions in ajax binding of a mvc kendo grid?

  22. 22

    How can we use a variable of the type of the template parameter?

  23. 23

    How to use constant in class namespace as the array and template parameters?

  24. 24

    How to use ng-class on a top level container when using nested views

  25. 25

    MeteorJs Template conditions to display different template

  26. 26

    Different compilers behavior for template specialization

  27. 27

    creating template function with different parameters

  28. 28

    Template instance in different translation units

  29. 29

    Which WPF template to use?

뜨겁다태그

보관