【问题标题】:Display same data again and again in asp.net mvc在 asp.net mvc 中一次又一次地显示相同的数据
【发布时间】:2017-02-16 21:47:48
【问题描述】:

这是我的 LINQ 代码:

    public JsonResult getfull()
    {
        var coursename = Session["course"].ToString();
        var location = Session["location"].ToString();
        var result = (from insti in db.Institute_Master
                      join course in db.Course_Detail on insti.InstituteId equals course.InstituteId
                      join coursemaster in db.Course_Master on course.CourseId equals coursemaster.CourseId
                      join  facility in db.Facility_Detail  on insti.InstituteId  equals facility.InstituteId
                      join  masterfacility in db.Facility_Master on facility.FacilityMasterID equals masterfacility.FacilityMasterId
                      where coursemaster.CourseName == coursename || insti.City == location
                      select new
                      {
                          //Id = insti.InstituteId,
                          name = insti.InstituteName,
                          university = insti.University,
                          Contact = insti.ContactNo,
                          address = insti.Address,
                          email = insti.EmailId,
                          zipcode = insti.Zipcode,
                          country = insti.Country,
                          state = insti.State,
                          city = insti.City,
                          start = insti.EstablishOn,
                          image = insti.ProfilePhoto,
                          fees = course.TotalFees,
                          mode = course.Mode,
                          coursename = coursemaster.CourseName,
                          duaration = coursemaster.duration,
                          discription = coursemaster.Discription,
                          eligibility = coursemaster.Eligibility,
                          recognization = coursemaster.Recognization,
                          facility  = masterfacility.FacilityName


                      }).Distinct();

        ViewBag.facilies = list;

        return Json(result, JsonRequestBehavior.AllowGet);
    }

这个方法是返回json结果,由angular js处理,我在下面提到了angularjs代码。

我正在使用 AngularJs 来显示数据,并且我的记录正在针对特定设施重复。

这是我的桌子:

这是为他的设施再次显示相同记录的问题:

这是我的 Angularjs 代码

    <h1>Show Institute</h1>
<div data-ng-app="myModule">
    <div data-ng-controller="myController">
<ul class="w3-ul w3-card-4" data-ng-repeat="detail in employees">
 <li class="w3-padding-16 institute-list">
    <span class="w3-xlarge">{{detail.name | uppercase}}</span>,<span>{{detail.address}}</span><br>
    <img alt="image" src="~/images/{{detail.image}}" class="w3-left  w3-margin-right img-institute-list" style="width:60px" />
    <span class="w3-xlarge">{{detail.coursename | uppercase}}</span>,<span>Course Duration {{detail.duaration}}</span>,<span>Course Mode  {{detail.mode}}</span>,<span>{{detail.discription}}</span><br>
    <span><b>Fees : </b>INR {{detail.fees}}</span><br>
    <span><b>{{detail.recognization | uppercase}}</b>  Recognised</span><br>


     <span>Facility {{detail.facility}}</span>
    <button class="w3-btn w3-blue" id="detail">More Details</button>


  </li>

      <script>
          $(document).ready(function () {
              $("button").click(function () {
                  $("#moredetail").toggle();
              });
          });
     </script>

    <li class="w3-padding-16 institute-list" id="moredetail" style="display:none">
        <span class="w3-xlarge">{{detail.contact}}</span>   <span class="w3-xlarge">{{detail.email}}</span><br />
         <span class="w3-xlarge" >{{detail.zipcode}}</span> <span class="w3-xlarge" >{{detail.country}}</span>  <br/>
         <span class="w3-xlarge" >{{detail.state}}</span> <span class="w3-xlarge" >{{detail.city}}</span>       <br />
          <span class="w3-xlarge" >{{detail.start}}</span>,
    </li>
</ul>

    </div>
    </div>
    <script>
        var app = angular
        .module("myModule", [])
        .controller("myController", function ($scope, $http) {

            $http.get("/home/getfull")
                 .then(function (response) {
                     $scope.employees = response.data;
                 });
        })
</script>  

    </div>
</body>
</html>

此代码用于在视图视图 json 方法中显示数据 任何人都可以帮助我吗?

【问题讨论】:

    标签: asp.net angularjs asp.net-mvc-3 asp.net-mvc-4


    【解决方案1】:

    它不断添加相同的记录,因为您有“选择新”。你需要的是这样的模型:

    public class CourseModel
    {
                              public int InstituteId {get; set;}
                             public string InstituteName {get; set;}
                              public string University {get; set;}
                              ETC...
    }
    

    然后在你的 MVC 控制器中:

     var result = (from insti in db.Institute_Master
                              join course in db.Course_Detail on insti.InstituteId equals course.InstituteId
                              join coursemaster in db.Course_Master on course.CourseId equals coursemaster.CourseId
                              join  facility in db.Facility_Detail  on insti.InstituteId  equals facility.InstituteId
                              join  masterfacility in db.Facility_Master on facility.FacilityMasterID equals masterfacility.FacilityMasterId
                              where coursemaster.CourseName == coursename || insti.City == location
    
    var courses = new List<CourseModel>();
    foreach(var c in result)
    {
    courses.Add(new CourseModel
    {
                            InstitueName = insti.InstituteName,
                            University = insti.University,
                            ETC...
    });
    }
    
    return Json(courses, JsonRequestBehavior.AllowGet;
    }
    

    我希望这会有所帮助!

    【讨论】:

    • 感谢您的回复,但您编写的代码会出现错误,例如查询正文以 select 或 group 结尾
    猜你喜欢
    • 2021-06-02
    • 1970-01-01
    • 2020-06-28
    • 1970-01-01
    • 2015-07-04
    • 2021-07-24
    • 2013-05-24
    • 2018-08-20
    • 1970-01-01
    相关资源
    最近更新 更多