在一个视图中通过局部视图使用多个存储过程

时间:2019-04-13 09:34:03

标签: asp.net model-view-controller viewmodel

我有多个具有不同结果集的存储过程,我想通过使用局部视图从一个主视图中的多个存储过程中获得结果集。

我正在为此代码苦苦挣扎(省略了一些代码以节省空间)

控制器:

[HttpGet]
        public ActionResult FetchPosts()
        {
            var viewModel = new PostCommentViewModel();
            {
                viewModel.AllPosts = (db.ShowAllPosts());
                viewModel.FriendsSuggestion = (db.PeopleYouMayKnow(1));
            }
            return View(viewModel);
        }

ViewModel:

   public class PostCommentViewModel
    {
        [Key]
        public int id { get; set; }

        public IEnumerable<ShowAllPosts_Result> AllPosts { get; set; }

        public IEnumerable<PeopleYouMayKnow_Result> FriendsSuggestion { get; set; }
    }

模型1:

  public partial class PeopleYouMayKnow_Result
    {
        public string user_image { get; set; }
        [Key]
        public int user_id { get; set; }
        public string user_name { get; set; }
    }

模型2:

    public partial class ShowAllPosts_Result
    {
        public int post_id { get; set; }
        public string post_title { get; set; }
        public string post_description { get; set; }
        public byte[] post_image { get; set; }
        public string user_name { get; set; }
        public string user_image { get; set; }
    }

主视图:

@model IEnumerable<WebApp.Models.PostCommentViewModel>

@Html.Partial("PostSugistedFriend")

  @foreach (var item in Model.AllPosts)
                                {

                                 <h3>@item.user_name</h3>
    <span><img src="images/clock.png" alt="">3 min ago</span>
     </div>
      </div>
      <div class="ed-opts">
     <a href="#" title="" class="ed-opts-open"><i class="la la-ellipsis-v"></i></a>
      <ul class="ed-options">

        <li><h3>@item.post_title</h3></li>
        <p>@item.post_description <a href="#" title="">view more</a></p>
        <ul class="skill-tags">
        <li><img src="@item.post_image" height="300" width="500" alt=""></li>
</ul>

部分视图:

@model IEnumerable<WebApp.Models.PostCommentViewModel>

<div class="col-lg-3 col-md-4 pd-left-none no-pd">
    <div class="main-left-sidebar no-margin">
        <!--user-data end-->
        <div class="suggestions full-width">
            <div class="sd-title">
                <h3>Suggestions</h3>
                <i class="la la-ellipsis-v"></i>
            </div><!--sd-title end-->
            <div class="suggestions-list">
                @foreach (var item in Model.FriendsSuggestion)
                {
                    <div class="suggestion-usd">
                        <img src="@item.user_image" alt="">
                        <div class="sgt-text">
                            <h4>@item.user_name</h4>

我遇到此错误:

Compiler Error Message: CS1061: 'IEnumerable<PostCommentViewModel>' does not contain a definition for 'AllPosts' and no extension method 'AllPosts' accepting a first argument of type 'IEnumerable<PostCommentViewModel>' could be found (are you missing a using directive or an assembly reference?)

我对asp.net和MVC完全陌生,所以请原谅我的无知。

0 个答案:

没有答案
相关问题