MVC 4 Bootstrap carousel, images from a folder in the shared layout - database

I currently have a static slider implemented in a partial view under the shared folder:
#{
ViewBag.Title = "_SliderPartial";
}
#*slider*#
<div id="myCarousel" class="carousel slide">
<!-- Carousel items -->
<div class="carousel-inner">
<div class="active item">
<img src="~/Content/images/ads/banner1.jpg" />
</div>
<div class="item">
<img src="~/Content/images/ads/banner2.jpg" />
</div>
<div class="item">
<img src="~/Content/images/ads/banner3.jpg" />
</div>
<div class="item">
<img src="~/Content/images/ads/banner4.jpg" />
</div>
</div>
<!-- Carousel nav -> toont navigatie pijlen op slider-->
#* <a class="carousel-control left" href="#myCarousel" data-slide="prev">‹</a>
<a class="carousel-control right" href="#myCarousel" data-slide="next">›</a>*#
</div>
I render this view in the _BootstrapLayoutcshtml which can be found in
following pastebin: http://pastebin.com/AvxMeVQS (line 100) and it works perfectly fine.
My problem is that I want the slider to be dynamically filled with images stored in
the ads folder based on certain parameters (I only want to displays ads that are active,
they all have a start and end date, stored in a database). I know how to code this
but the issue is that I don't have any controller (because shared doesn`t need one I guess?) to gather the ads and send them.
I would be amazing if somebody could help me because I am fairly new to MVC, thanks in advance!
Solution explorer: http://i46.tinypic.com/2cwt02h.png

I did something similar, in order to follow a decent working practice you will need a Model and controller. You would probably want an Image view model for example (pseudo code below)
namespace Models
{
public class adsImage
{
public int ID {get; set;}
public string src {get; set;}
}
}
public class HomeController : Controller
{
public ActionResult RenderAds()
{
List<Models.adsImage> imgList = new List<adsImage>();
imgList.add(new adsImage({ src = ""});
return PartalView("YourPartialView",imgList);
}
}
}
Dont forget to bind model to view (add this to your partial view)
#model List
Last thing to do is render the partial view via your action method
#{ Html.RenderAction("RenderAds","Home"); }

Related

How to display image from SQL image table that has imagepath

I am working on my MVCOnlineShop Project, i made a product list page, now i want to show an image for each product, so i made an image table in SQL, in it imageID and imagepath, and in the product table i added imageID, i have searched on how to fetch the images from the database , but didn't understand that much, so can you please help me with my actionresult in my controller?
this is my productList PartialView:
#model MVCOnlineShop.Models.Category
#{
ViewBag.Title = "ProductList";
}
<script src="~/Scripts/Productjs/bootstrap.min.js"></script>
<script src="~/Scripts/Productjs/jquery.js"></script>
<link href="~/Content/Productcss/bootstrap.min.css" rel="stylesheet">
<link href="~/Content/Productcss/2-col-portfolio.css" rel="stylesheet">
<div class="container">
<!-- Page Header -->
<div class="row">
#foreach (var Product in Model.Products)
{
<div class="col-md-6 portfolio-item">
<a href="#">
<img class="img-responsive" src="http://placehold.it/700x400" alt="">
</a>
<h3>
#Html.ActionLink(Product.ProductName, "Details", new { id = Product.CategoryID })
</h3>
</div>
}
</div>
</div>
this view will show each product but with the same image, i want to change it to get the images in database.
Thanks in advance :)
Assuming every product has at least one image (otherwise you need to check for it's existence), and you use lazy loading in your EF model (otherwise you will need to "Include" the Images property) change this:
<img class="img-responsive" src="http://placehold.it/700x400" alt="">
into this:
<img class="img-responsive" src="#Product.Images.First().Imagepath" alt="">
You may need to change the spelling of the various properties, I had to guess at them.

Getting posts (poster name, photo, text body) into VF page from specific Chatter group - Struggling getting into VF

I am working off of some code I found on the Internet - I have only been on the ADM 201 course.
What I am doing is attempting to pull the information from a post and then put this into a VF page. I know I can use chatter:feed, but this is getting all of the information and functions, whereas I need only the text body, poster name, and picture (not essential).
What I have so far:
Apex Class:
public class Test_3 { //Class is
public ConnectApi.FeedElementPage feedElementPage{get;set;}
public Test_3() {
feedElementPage = ConnectApi.ChatterFeeds.getFeedElementsFromFeed('0F958000000TWvL', ConnectApi.FeedType.Record, 'showInternalOnly');
}
}
VF page:
<apex:page controller="Test_5">
<div class="posts">
<div class="row">
<div class="col-md-6">
<apex:repeat value="{!feed.elements}" var="feedElement">
<div class="media">
<a class="pull-left" href="{!feedElement.parent.id}">
<img class="media-object" src="{!feedElement.photoUrl}" alt="{!feedElement.actor.name}"/>
</a>
<div class="media-body">
<h4 class="media-heading">{!feedItem.actor.name}</h4>
// This creates a heading from the posts that is the poster's name
{!feedElement.body.text}
// This is the body of the post
</div>
</div>
</apex:repeat>
</div>
</div>
</div>
<footer>
<p>© Operations Updates - Please refresh your browser for the latest</p>
</footer>
</apex:page>
Current issues:
!feedElement.parent.id
!feedItem.actor.name
"Error: Unknown property 'ConnectApi.Feed.elements'"
Anyway, I can't work this out. I've been trying to make this for a lovely 5 hours now.
Any help would be much appreciated.
The code was modified from here and then I attempted to update it to Elements from Items.
Thanks
In your Apex class, the variable is called "feedElementPage" but in the Visualforce page, it says "feed". Please update feedElementPage to feed in Apex class or update repeat value="{!feed.elements}" to repeat value="{!feedElementPage.elements}"
In addition, I think you should use FeedItem instead of FeedElement. Please take a look at the document, and let me know if you have any other questions.

Identify individual element of ng-repeat

I am new to angular, but I know Jquery. I have done something like below :
I am getting the following data from the controller :
var data = [
{
id : 0,
name : 'Nemo',
path : 'img/fishBlow.jpg',
cover : 'img/cover.jpg',
flip : false
}, ... etc
In my view I want to access each of the individual card on click event;
How can I identify a particular card ?
In jquery I can find parent and do rest of the sutffs....
<div class='container' data-ng-controller="InitCtrl">
<h2>{{Message}}</h2>
<div class="wrapper" data-ng-repeat = "card in suffle()" >
<div class="card flipped" id={{card.id}}>
<figure class="front">
<img ng-src="{{card.path}}" />
</figure>
<figure class="back">
<img ng-src="{{card.cover}}" />
</figure>
</div>
</div>
</div>
Since you are new to angular I am not giving you the most technical details but a high level idea:
Inside your ng-repeat (AKA on the div.wrapper) you will have access to current card of the iteration. Thanks to that, you can do something like {{card.id}} or {{card.path}}. That means you have access to the card object to do what you want to do there. That also means you can pass that concrete card back to the controller via click like:
<button ng-click="selectCard(card)">Select</button>
Notice the parameter of the function, it is the concrete card of that iteration. Doing that, you can access the concrete card on a click.
Example: http://plnkr.co/edit/bc4cVNmgrA4Vdv4kcoTF?p=preview

How can I dynamically set the background-image on each DIV using ngRepeat and ngClass?

I would like to be able to set the background-image property of each div that is created during ng-repeat.
I tried to use ng-class to add the appropriate class to the appropriate div in the ng-repeat loop but ng-class seems to just be over writing the .tile class, which I need to be there.
Also, on a side note, why does all my AngularJS code show up in my source when I view it with Google Dev Tools? You can see all the logic in there. That can't be right, can it?
How can I get ng-class to add the right class to the right div with out overwriting the .tile class as well as showing up properly in the source?
my Controller in main.js
'use strict';
angular.module('mmApp')
.controller('MainCtrl', function ($scope) {
$scope.designTiles = [
{ bgImageClass : "best-show-premium", title : "Vance & Gary Unhinged", use : "Album Art" },
{ bgImageClass : "dam-funk", title : "Dam-Funk", use : "Logo" },
{ bgImageClass : "lemy-leopard-soh", title : "Lemy Leopard : Streets of House", use : "Album Art" },
{ bgImageClass : "culture-blind", title : "Max Mythic : Culture Blind", use : "Album Art" },
{ bgImageClass : "future-shock-logo", title : "Future Shock Logo", use : "Logo" },
{ bgImageClass : "fame-logo", title : "F.A.M.E. Logo", use : "Logo" },
{ bgImageClass : "lounge-coffee", title : "Loung Coffee", use : "Album Art" },
{ bgImageClass : "engine-yard-tshirt", title : "Engine Yard T-Shirt", use : "T-shirt Design" },
];
});
my design.html view
<section ng-controller="MainCtrl" class="ng-class: {tile.bgImageClass:true};" class="main">
<div ng-repeat="tile in designTiles" class="tile">
<h3 class="tile_title">{{tile.title}}</h3>
<h4 class="tile_use">{{tile.use}}</h4>
</div>
</section>
UPDATE
I added the changed that sh0ber suggested. Here is my new design.html view
<section ng-controller="MainCtrl" class="main">
<a ng-repeat="tile in designTiles" ng-href="{{tile.url}}" ng-class="['tile', tile.bgImageClass]">
<h3 class="tile_title">{{tile.title}}</h3>
<h4 class="tile_use">{{tile.use}}</h4>
</a>
</section>
but this does not actually apply tile.bgImageClass to the <a> element. Here is what I see in Developer Tools
If we take a look at the first <a> we see
<a ng-repeat="tile in designTiles" ng-href="#/design/vance-and-gary-unhinged" ng-class="['tile', 'tile.bgImageClass']" class="ng-scope tile tile.bgImageClass" href="#/design/vance-and-gary-unhinged">
<h3 class="tile_title ng-binding">Vance & Gary Unhinged</h3>
<h4 class="tile_use ng-binding">Album Art</h4>
</a>
Upon looking at the classes that are applied we see
class="ng-scope tile tile.bgImageClass"
For this specific <a> we should instead see
class="ng-scope tile vance-and-gary-unhinged"
The .vance-and-gary-unhinged class is not being applied right after .tile. Why is tile.bgImageClass being placed in there? It seems that AngularJS is not fetching the class. What am I doing wrong?
You can just put the binding in the normal class attribute:
<section ng-controller="MainCtrl" class="main">
<div ng-repeat="tile in designTiles" class="tile {{tile.bgImageClass}}">
<h3 class="tile_title">{{tile.title}}</h3>
<h4 class="tile_use">{{tile.use}}</h4>
</div>
</section>
Here's a demo of the above in action.
From the AngularJS ng-class doc:
The result of the evaluation can be a string representing space
delimited class names, an array, or a map of class names to boolean
values.
Here I'm using the attribute directive and creating an array of classes. The array contains two classes: one for the class name specified by the bgImageClass property and another for the string literal 'tile' for the tile class.
<section ng-controller="MainCtrl" class="main">
<div ng-repeat="tile in designTiles" ng-class="[tile.bgImageClass, 'tile']">
<h3 class="tile_title">{{tile.title}}</h3>
<h4 class="tile_use">{{tile.use}}</h4>
</div>
</section>

AngularJS - Change class on list when clicked

I am just getting started with Angular and am still trying to get a feel for how it works. I have 2 questions about a list with ng-repeat. I will post my code first.
HTML
<div class="row">
<div class="span2">
<ul class="nav nav-pills nav-stacked">
<li ng-repeat="class in classes | orderBy:orderProp" ng-class="activeClass">
<a ng-click="loadRoster(class)">{{class.class_name}}</a>
</li>
</ul>
</div>
<div class="span2">
<ul class="nav nav-pills nav-stacked">
<li ng-repeat="student in students | orderBy:orderProp">
<a ng-click="enterPassword(student)">{{student.student_name}}</a>
</li>
</ul>
</div>
</div>
Controller
function ClassListCtrl($scope, $http) {
$scope.loadedList=[];
$scope.students=[];
$scope.activeClass='';
$scope.orderProp = 'alphabetical';
$http.get('data/class.json').success(function(data) {
$scope.classes = data;
});
$scope.loadRoster=function(classlist){
$scope.selectedClass=classlist;
$scope.activeClass='active';
if($scope.loadedList[classlist.class_id]== undefined){
$http.get('data/class_'+classlist.class_id+'.json').success(function(data){
$scope.loadedList[classlist.class_id]=data;
$scope.students=data;
});
}
$scope.students=$scope.loadedList[classlist.class_id];
}
$scope.studentClick=function(student){
}
}
Ok, this pulls the data just fine. I can click on a class and it loads the students in that class. To cut down on AJAX calls, I store those that are clicked and if that class is clicked again, it will fetch it from memory instead of making the call. This is fine as class rosters are static for the most part.
I am trying to have it load a default class roster (the first alphabetically) and mark just that list item as active.
Secondly, when they click a list item, I want to change that list item to active, and change the class of the other ones back to nothing. I am not sure how to do that either. As it stands, none are active now, but when I click one, all become active.
in Html ng-class, if class_id matches $scope.activeClass set class of li to "active"
<li ng-repeat="class in classes | orderBy:orderProp" ng-class="{'active':class.class_id == activeClass}">
in Controller when $scope.loadRoster is called set $scope.activeClass to class_id of the class passed to function
$scope.loadRoster=function(classlist){
$scope.selectedClass=classlist;
$scope.activeClass=classlist.class_id;
for your default active class just set $scope.activeClass to class_id of the class

Resources