How to get descendants from current page in Umbraco 7? - asp.net-mvc

I have a document type with alias dArticlesMain, and for this page I have the following structure.
dArticlesMain
dArticlesCategory1
Article1
Article2
Article3
dArticlesCategory2
Article1
Article2
Article3
dArticlesCategory3
Article1
Article2
Article3
I'm on dArticlesMain and i want to display all the descendants (Articles) and skip it's childrens (dArticlesCategory)
I have this code which display all the childrens (dArticlesCategory) and the descendants (Articles) also when i use the Article properties it's through an error.
<ul>
#foreach(var page in Model.Content.Descendants())
{
<li>#page.Name</li>
}
</ul>
I have got this code but i can't display by Article properties like articleText or articleImage.
<ul>
#foreach(var page in Model.Content.DescendantsOrSelf().OfTypes("Article"))
{
<li>#page.Name</li>
}
</ul>

I have figured it out, and here's my code...
#{
var rootNode = CurrentPage.AncestorOrSelf(1);
var articlesParent = rootNode.Children("dArticlesMain").FirstOrDefault();
<div class="row">
#foreach (var article in articlesParent.Descendants("article").Where("Visible").RandomOrder())
{
<div class="col-sm-6 col-md-3">
<div class="thumbnail">
<a href="#article.Url">
<img src="#article.articlePhoto" alt="#article.articleName" />
</a>
<div class="caption">
<a class="h4" href="#article.Url">#article.articleName</a>
#Umbraco.Truncate(article.articleFullText, 100)
</div>
</div>
</div>
}
</div>
}

Related

How to print data of join query which are stored in Viewbag in ASP.NET MVC with Entity Framework?

I want to display data from 2 different tables which are stored in ViewBag at view side but I'm unable to print the data, I get an error
'object' does not contain a definition for 'Name'
The code at controller side
public ActionResult Packages()
{
int Prov_id = 1;
using (var db = new DataContex())
{
ViewBag.pack = db.Packages.Where(x => x.ProviderId == Prov_id).ToList();
ViewBag.service = db.GroupServices.Join(db.PackageServices,
gs => gs.ServiceId,
ps => ps.ServiceId,
(gs, ps) => new
{
name = gs.ServiceName
})
.ToList();
return View();
}
View markup:
#foreach (var item in ViewBag.pack)
{
<div class="col-md-6 bg-white mt-3">
<div class="card border package-grid p-3">
<div class="row">
<div class="col-md-8">
<input type="hidden" />
<h4 class="font-weight-bold py-2">#item.PackageName</h4>
#foreach (var item1 in ViewBag.service)
{
<h6 class="py-1">#item1.Name</h6>
}
<p class="py-3">#item.PackageDescription</p>
</div>
</div>
<div class="row">
<div class="col-md-12 mt-3 d-flex justify-content-between flex-wrap">
<p><strike> Rs. #item.PackagePrice</strike> <span>Rs. #item.PackageOfferPrice</span> </p>
<span>Remove</span>
</div>
</div>
</div>
</div>
}
That is because when you created ViewBag.service you have created anonymous object with property as name with n as LowerCase and at view side you are trying to use #item.Name as N as UpperCase.
Correct the property name and it will work.

How to display only one image in the view from multiple images on the database? (ASP.NET MVC)

I have multiple images for one product on the database. I created two tables. One for Products and another for Photos of products. And I want to show only one photo on the page. My code is below. I have tried to declare variable in the view. But there is error:
PhotoProduct' is a type, which is not valid in the given context
Is there other way for that?
Here is my code:
#foreach (Product prd in Model.Prod.Where(i => i.userid == Model.userr.ID))
{
<div class="col-md-4">
<figure class="card card-product mehsul">
#PhotoProduct prphoto = Model.Photopr.Where(ph => ph.Product.id == ph.id).Take(1).ToList()
{
<div class="img-wrap"> <img class="img-fluid mehsulimg" src="#prphoto.Photo" alt=""> </div>
}
<div class="handhover">
<img class="img-fluid" src="~/PublicFront/images/serv2b712.jpg" alt="">
</div>
<figcaption class="info-wrap">
<h4 class="title">#prd.ProdName</h4>
<p class="#prd.Price"></p>
</figcaption>
<div class="bottom-wrap">
Order Now
<div class="price-wrap h5">
<span class="price-new">$1280</span>
<del class="price-old">$1980</del>
</div> <!-- price-wrap.// -->
</div> <!-- bottom-wrap.// -->
</figure>
</div> <!-- col // -->
}
I saw your models in this question before Error: Value cannot be null. Parameter name: source.
In your Photo Model you are already declaring PhotoProducts as virtual;
public virtual ICollection<PhotoProduct> PhotoProducts { get; set; }
Using virtual in EntityFramework means that you are "lazy-loading" that entity or that entity gets automatically loaded.
Since it is automatically loaded, you could do;
#foreach (Product prd in Model.Prod.Where(i=>i.userid==Model.userr.ID))
{
// ...
#if(prd.PhotoProducts.FirstOrDefault() != null){
<div class="img-wrap">
<img class="img-fluid mehsulimg" src="#prd.PhotoProducts.First().Photo" alt="">
</div>
}else{
<div class="img-wrap">
<img class="img-fluid mehsulimg" alt="No Photo Available">
</div>
}
// ...
}

How to keep navbar on ProductList Page

I am working on my MVCOnlineShop, i made on the homepage on the navbar a dropdownlist with categories as dropdown button and products as dropdowncontent, i want to keep this on the ProductList View:
this is my CategoryLayout.cshtml(PartialView):
#model IEnumerable<MVCOnlineShop.Models.Category>
#{
ViewBag.Title = "CategoryLayout";
}
#foreach (var Category in Model)
{
<li>
<div class="dropdown">
<button class="dropbtn">
#Html.ActionLink(Category.CategoryName,
"ProductList", new { Category = Category.CategoryID }, new { #style = "color:#1ABC9C;text-decoration:none;" })
</button>
<div class="dropdown-content">
#foreach (var Product in Category.Products)
{
#Html.ActionLink(Product.ProductName,
"Details", new { id = Product.CategoryID }, new { style = "text-decoration:none;" })
}
</div>
</div>
</li>
}
this is my ProductList.cshtml 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="#Product.ImageID" alt="">
</a>
<h3>
#Html.ActionLink(Product.ProductName, "Details", new { id = Product.CategoryID })
</h3>
</div>
}
</div>
</div>
and this is how i rendered the CategoryLayout.cshtml in the _Layout.cshtml to show the dropdownlist on the homepage:
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
#Html.Partial("CategoryLayout")
</ul>
</div>
</div>
</div>
Question: how to show this navbar on the ProductList too?
Thanks in advance!
Create a child action:
[ChildActionOnly]
public ActionResult Navbar()
{
var categories = // get categories;
return PartialView("_Navbar", categories);
}
Then, create a partial view to render the navbar (_Navbar.cshtml):
#model IEnumerable<MVCOnlineShop.Models.Category>
#foreach (var Category in Model)
{
<li>
<div class="dropdown">
<button class="dropbtn">
#Html.ActionLink(Category.CategoryName, "ProductList", new { Category = Category.CategoryID }, new { #style = "color:#1ABC9C;text-decoration:none;" })
</button>
<div class="dropdown-content">
#foreach (var Product in Category.Products)
{
#Html.ActionLink(Product.ProductName, "Details", new { id = Product.CategoryID }, new { style = "text-decoration:none;" })
}
</div>
</div>
</li>
}
Then, call the following where you want the navbar to appear:
#Html.Action("Navbar", "Foo")
Where "Foo" is the name of the controller where you added the child action. Then, the view can use whatever model it needs to you and you can still render your navbar with its own model.
If I understood I think what you are looking for is a master page
MSDN says :
ASP.NET master pages allow you to create a consistent layout for the pages in your application. A single master page defines the look and feel and standard behavior that you want for all of the pages (or a group of pages) in your application.
Notice the part saying : standard behavior that you want for all of the pages ...in your application
So what you'll do is rethink your approach. Every element that must appear on every page is to be defined in the master page view.
Please read this MSDN article about creating a master page and child pages
Simply, this is how your master page should be :
<%# Master Language="C#" AutoEventWireup="true" CodeFile="you_file_name.cs" Inherits="Site" %>
<html>
<head>
<asp:ContentPlaceHolder ID="head" runat="server"></asp:ContentPlaceHolder>
<!-- some other elements like scripts, css, etc...-->
</head>
<body>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">#Html.Partial("CategoryLayout")</ul>
</div>
<!-- Here add the code for rendering any other view at will ; for instance ProductList -->
</body>
</html>
Becarefull though : ALL other views that you want to render inside the master page will have to be created accordingly (create view content with master page... and select the appropriate master page on view creation dialog)

How to call a controller method inside the view?

My problem is when i'm trying to get the best 4 posts who's match the same author name , as you see in the following image :
this is the controller :
Controller Code
and here is the view :
View code
so how i can call the controller method from the view , what's the code will looks like ? and thanks ..
also here is the controller method :
public ActionResult SameAuthor(string author)
{
var result = db.Books.Where(b => b.Author_name == author).Take(4).ToList();
return View(result);
}
and the view code (book page , the following code is a part of book page , that show the book details and info , so this code for show under the post details the most 4 post for same author) is :
//DISPLAYING 4 BOOKS FOR SAME AUTHOR
<div class="well">
<div class="row">
#foreach (var item in Model)
{
<div class="col-sm-6 col-md-3">
<div class="thumbnail">
<img src="/Books/RetrieveImage/#item.Book_id" alt="Generic placeholder thumbnail">
</div>
<div class="caption">
<h3>#item.Book_name</h3>
<p>Some sample text. Some sample text.</p>
<p>
<a href="#" class="btn btn-primary" role="button">
Button
</a>
<a href="#" class="btn btn-default" role="button">
Button
</a>
</p>
</div>
</div>
}
</div>
</div>
and thanks
If you are trying to show the result of your SameAuthor action method inside another view, you may use Html.Action helper method to do so.
So in your some other razor view, you can include the below line
#Html.Action("SameAuthor","Book",new {author="Author name here"})

how to show video file in mvc when i have a physical path in my table

i have physical path of video in my database table how can i show those video files in my mvc view. can anyone give me an idea.
here is my service layer in which i am getting all the values in a list
public IEnumerable<Marketing> GetMarketingContents()
{
return fDbContext.Marketings.ToList();
}
view model
public class ViewModelMarketing
{
public List<Marketing> MarketVideo { get; set; }
}
here is my controller in which i am filtering the videos and passing it to the partial view.
public ActionResult MarketingListVideos()
{
ViewModelMarketing viewModelMarketing = new ViewModelMarketing();
var debs = from s in iMarketingService.GetMarketingContents()
select s;
viewModelMarketing.MarketVideo = debs.Where(t => t.MarketingType.Equals("v") && t.Active != false).ToList();
return PartialView("_GetVideos", viewModelMarketing);
}
here is my partial view named _GetVideos
#model PreFlight.ViewModels.ViewModelMarketing
#foreach (var item in Model.MarketVideo)
{
<div class="list-group">
<div class="row">
<div class="col-md-1 col-xs-1">
<i class="glyphicon glyphicon-facetime-video" style="color: black"></i>
</div>
<div class="col-md-5 col-xs-5">
#item.Content
</div>
<div class="col-md-5 col-xs-5">
#item.Active
</div>
</div>
</div>
}
here is my main view in which i am rendering this partial view
#model PreFlight.ViewModels.ViewModelMarketing
<div class="panel-body">
<div class="list-group">
#Html.Action("MarketingListVideos", "Marketing")
</div>
</div>
now i am getting the list of videos in my view. how can i show it as a video files. can any one help me to get this. any help will be appreciated.
You can use HTML5 :
#foreach (var item in Model.MarketVideo)
{
<div class="list-group">
<div class="row">
<div class="col-md-1 col-xs-1">
<i class="glyphicon glyphicon-facetime-video" style="color: black"></i>
</div>
<div class="col-md-5 col-xs-5">
#item.Content
</div>
<div class="col-md-5 col-xs-5">
#item.Active
</div>
<video controls="controls">
<source src="#item.YourVideoPath" type="video/mp4" />
</video>
</div>
</div>
}
And for best pratices you should espeficy the correct extension:
<video controls="controls">
<source src="#item.YourVideoPath" type="video/#item.VideoExtension" />
</video>
Here some referece:
http://www.w3schools.com/html/html5_video.asp
Note: just remember that not all browsers supports the video tag, but a good amount does

Resources