c# HtmlAgilityPack HTML parsing issue - html-parsing

I have this html
<div class="postrow firs">
<h2 class="title icon">
This is the title
</h2>
<div class="content">
<div id="post_message_1668079">
<blockquote class="postcontent restore ">
<div>Category</div>
<div>Authour: Kim</div>
line 1<br /> line2
</blockquote>
</div>
</div>
</div> <div class="postrow">
<h2 class="title icon">
This is the title
</h2>
<div class="content">
<div id="post_message_1668079">
<blockquote class="postcontent restore ">
<div>Category</div>
line 1<br /> line2
</blockquote>
</div>
</div>
</div>
I want to extract the following things from each div having class "postrow" and may also have another classes like <div class="postrow first">. So the class "first" is not my concern, just need to have "postrow" in the beginning.
The content inside the tag with class title
the HTML from the "blockquote" tag. But not any div withing this
tag.
Code I tried:
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml("http://localhost/vanilla/");
List<string> facts = new List<string>();
foreach (HtmlNode li in doc.DocumentNode.SelectNodes("//div[#class='postrow']"))
{
facts.Add(li.InnerHtml);
foreach (String s in facts)
{
textBox1.Text += s + "/n";
}
}

Your code has issue you have to give html as string not the path
doc.LoadHtml("http://localhost/vanilla/");
instead
var request = (HttpWebRequest)WebRequest.Create("http://localhost/vanilla/");
String response = request.GetResponse();
doc.loadHtml(response);
now iterate the parsed html

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.

Remove records from database using Entity Framework

I am trying to remove records from database using Entity Framework.
This is the code:
Controller:
[HttpPost]
public ActionResult DeleteProduct(int?id)
{
Product prd = db.Products.Find(id);
db.Products.Remove(prd);
db.SaveChanges();
return View();
}
View:
<form method="post" enctype="multipart/form-data">
<div class="row ">
#foreach (var product in Model)
{
<div class="col-md-6 col-lg-4 Products">
<figure class="card card-product mehsul">
<div class="img-wrap"> <img class="img-fluid mehsulimg" src="#product.PhotoProducts.First().ImageName" alt=""> </div>
<div class="handhover">
<img class="img-fluid" src="#product.PhotoProducts.Last().ImageName" alt="">
</div>
<figcaption class="info-wrap">
#product.ProdName
<p class="desc">Some small description goes here</p>
</figcaption>
<div class="bottom-wrap">
Paylash
<a id="DelProd" href="/ProductAd/DeleteProduct/#product.id" class="m-2 btn btn-sm btn-primary float-right">Sil</a>
<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 // -->
}
</div>
</form>
But I am getting this error:
Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /ProductAd/DeleteProduct/1
If You are sending a delete request, it is not a [HTTPPost] and you are passing the whole model to the controller not only the key, so if you need to add id in your request, you should do something like this.
API Version:
[HttpDelete("{id}")]
public ActionResult DeleteProduct(int?id)
{
Product prd = db.Products.Find(id);
db.Products.Remove(prd);
db.SaveChanges();
return View();
}
Or using something like this for MVC Version with full model
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Delete(YourViewModel viewModel)
{
var id = viewModel.id;
Product prd = db.Products.Find(id);
db.Products.Remove(prd);
db.SaveChanges();
return View();
}
Since your action has the "HttpPost" attribute, you need to do an HTTP POST. Your a tag would only do a GET, and the server would reject it as a page not found. Assuming everything else is correct, then I think it would be something more like this:
<div class="row ">
#foreach (var product in Model)
{
<div class="col-md-6 col-lg-4 Products">
<figure class="card card-product mehsul">
<div class="img-wrap"> <img class="img-fluid mehsulimg" src="#product.PhotoProducts.First().ImageName" alt=""> </div>
<div class="handhover">
<img class="img-fluid" src="#product.PhotoProducts.Last().ImageName" alt="">
</div>
<figcaption class="info-wrap">
#product.ProdName
<p class="desc">Some small description goes here</p>
</figcaption>
<div class="bottom-wrap">
Paylash
<form method="post" action="#Url.Action("DeleteProduct","ProductAd", new {id=productid})">
<button type="submit" class="m-2 btn btn-sm btn-primary float-right">Sil</a>
</form>
// or
#using(Html.BeginForm("DeleteProduct","ProductAd",new {id=product.id}))
{
<button type="submit" class="m-2 btn btn-sm btn-primary float-right">Sil</a>
}
<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 // -->
}
</div>

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 get descendants from current page in Umbraco 7?

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>
}

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