How do I add a “model” dropdown in a IPagedlist model?

前端 未结 1 1003
一整个雨季
一整个雨季 2020-12-21 23:07

I have a page to display every log (message, time, type, customerId, Name) in a html table. Since the log is huge I am using a IPagedList in the Razor MVC and this

相关标签:
1条回答
  • 2020-12-21 23:36

    Create a view model with the properties you need in the view

    public class LogModelVM
    {
      public int SelectedCustomer { get; set; }
      public IEnumerable<SelectListItem> CustomerList { get; set; } // suggested name change
      public string Message { get; set; } // for the message search box?
      public IPagedList<NowasteWebPortalMVC.Models.LogModel> Logs { get; set; }
      .... // add properties for sortorder etc
    }
    

    Then in the controller method, initiaize a new LogModelVM and assign the values (e.g. model.Logs = logs;), and return the view model so that in the view you can use

    @model yourAssembly.LogModelVM
    ....
    @Html.DropDownListFor(m => m.SelectedCustomer, Model.CustomerList) // why change the id attribute?
    ....
    @Html.PagedListPager(Model.Logs, page => Url.Action(...`
    

    You should also consider adding the other properties such as sortOrder and currentfilter rather than using ViewBag

    Side note: Ensure that all associated views, including the main view also use @model yourAssembly.LogModelVM

    0 讨论(0)
提交回复
热议问题