HtmlAgilityPack and Xpath - xpath

I wanted to create a function on C# console application that able to click on html tag and store data from link,
firstofall I traverse html with htmlagilitypack(Xpath query) and find all of the tag,that name is "Contact details".
on that page there are 20 of "Contact Details" link,I Want Click on "Contact Details" one by one and store data on database and return to another "Contact Details" link.
pagelink:
http://www.alibaba.com/corporations/iranian/1.html
after that when there isnt any "Contact Details" for clicked I want to click to another link like:
http://www.alibaba.com/corporations/iranian/2.html
a part of program code for find "Contact Details"
Please help me.
public static void traverseAndAppend()
{
string url = "http://www.alibaba.com/corporations/iranian/1.html";
HtmlWeb web = new HtmlWeb();
HtmlDocument doc = web.Load(url);
string contactDetails = "//div[#class='company']/a[#class='cd']";
foreach (HtmlNode node doc.DocumentNode.SelectNodes(contactDetails))
{
Console.WriteLine(node.InnerText.Trim());
}
}

Related

IE 11 keeps showing the old populated value in the text box after the value has been recalculated and should be different

It seems to be happening only with IE. I'm using IE 11
Chrome and Firefox are fine.
What happens is:
1. I open the Item details page that shows Item Original Amount
Then I click on the button that opens the pop up window, where I need to update an amount field for that item.
The Database has the following data before an update:
I enter the amount int the text field and submit the form.
The amount is subtracted from original amount and data is updated with a new value in database.
Then, I navigate to the main page that lists all the items and I click on just updated item to open its details page. On that page the correct amount shown as in a database:
However, when opening pop up again, the controller method that gets new data is not getting hit and there is still old amount shown as $414.00 on the pop up window:
It seems to be happening only in IE.
Chrome and Firefox shows the correct amount of $314.00
This is the code that opens a Pop Up window
#Html.ActionLink("Create New Item", "OpenNewItem", "ItemDetail", new
{
itemId = Model.Item.ItemId
}, new { #class = "modal-link k-button k-primary", onclick = "$('#AssignedItem_validationMessage').css('display', 'none');$('#NewAmount_validationMessage').css('display', 'none')" })
There is an ajax request used as following when when creating a new amount:
#using (Ajax.BeginForm("Create", "ItemDetail",
new AjaxOptions
{
HttpMethod = "POST",
LoadingElementId = "loader",
UpdateTargetId = "Item",
InsertionMode = InsertionMode.Replace,
OnComplete = "handleComplete",
OnFailure = "CheckError"
}, new { id = "CreateItemForm" }))
{
<div class="modal-body">
<div id="Item">
#Html.Partial("_CreateNewItem", Model)
</div>
</div>
}
I'm not sure what is happening.
Has something like that has happened to anyone and what is the fix?
We the help of my coworker, we finally found the solution to the problem.
We needed to add Application_PreSendRequestHeaders method into a Global.asax.cs file like the following:
protected void Application_PreSendRequestHeaders(Object sender, EventArgs e)
{
Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);
}

mvc3 create link from controller

Is there a way to create a link from a controller and apppend it to a ViewBag ? I been trying but nothing is working
HyperLink hyperl = new HyperLink();
hyperl.NavigateUrl = "http://www.example.com/";
hyperl.Text = "Here is why..";
ViewBag.check ="You must register"+ hyperl;
You must register System.Web.UI.WebControls.HyperLink
I know that I am probably only missing 1 more line but i can't find it..
You could use the TagBuilder and do something like
public static TagBuilder CreateAnchor(string text, string url)
{
var anchor = new TagBuilder("a");
anchor.SetInnerText(text);
anchor.MergeAttribute("href", url);
return anchor;
}
Then pass to the ViewBag Like this
ViewBag.hyperl = CreateAnchor("Here is why..","http://www.example.com/");
You're mixing web forms with MVC. The result will be an abomination. Just send the URL through the view bag, and use it in the markup in a plain anchor tag:
Controller code:
ViewBag.LinkAddress = "...";
Markup:
Link
Try this
in Controller
HyperLink hyperl = new HyperLink();
hyperl.NavigateUrl = "http://www.example.com/";
hyperl.Text = "Here is why..";
ViewBag.check = hyperl.NavigateUrl;
ViewBag.Text = "You must register";
in View
<a href=#ViewBag.check>#ViewBag.text</a>
or
<a href=#ViewBag.check>You must register</a>

how to remember the return-to page when visiting a detailpage from different parent pages

i have a page which lists a collection of objects and a detail page for the object details.
There is a link for every row in the list to the details page.
At the bottom of the details page, i have a hardcoded [Go back] link, which will send you to the list page you just came from.
So far so good.
But now i also want to link to the details page from a, new, dashboard page.
What is a smart way to remember the list page when clicking on the list to go to the details page so i can return to the right list page?
I thought about the session, or maybe i can read the 'from' page in a controller-action-parameters format?
You could pass the go-back link as query string parameter to the details action in addition to the id of the object you are going to show the details about.
public ActionResult Details(int id, string returnUrl)
{
...
}
The returnUrl parameter could then be used in the corresponding view to generate the Go-Back link.
public ActionResult Details(int id, string returnUrl)
{
...
}
Set a hidden value with tag name as "returnurl" in the view. When the user clicks on button, it navigates to this page, you can find the value in returnurl.
Depending upon the value in returnurl, you can navigate to the required page.
Regards,
Pavan.G

What is a way to share a drop down inside a layout for use in all views?

I am becoming more familiar with MVC 3 and the RAZOR view engine. I have a question regarding layouts and shared controls on pages.
Let’s say I have a header section defined in my main layout. In that header is a dropdown I need to populate with project names. This dropdown will serve as a context for the entire site and is present on all pages. As an example, if the user selects “Project A” from the drop down, all of the views for the site will be based on “Project A”. Since this dropdown control is rather static and is used by the entire site, where is the best place to put the code to pull all the projects to display in the dropdown? In a Partial View? In a HTML helper? Another thought is, if a user selects a new value, they would be taken to a dashboard or similar page for that newly selected project. I am trying to figure out how to reuse this control on every page in the site without having to keep wiring it up in every possible controller.
You could use a child action along with the Html.Action helper. So you start by defining a view model:
public class ProjectViewModel
{
[DisplayName("Project name")]
public string ProjectId { get; set; }
public IEnumerable<SelectListItem> ProjectNames { get; set; }
}
then a controller:
public class ProjectsController: Controller
{
private readonly IProjectsRepository _repository;
public ProjectsController(IProjectsRepository repository)
{
_repository = repository;
}
public ActionResult Index(string projectId)
{
var projects = _repository.GetProjects();
var model = new ProjectViewModel
{
ProjectId = projectId,
ProjectNames = projects.Select(x => new SelectListItem
{
Value = x.Id,
Text = x.Name
})
};
return PartialView(model);
}
}
then the corresponding view (~/views/projects/index.cshtml):
#model ProjectViewModel
#Html.LabelFor(x => x.ProjectId)
#Html.DropDownListFor(
x => x.ProjectId,
Model.ProjectNames,
new {
id = "projects",
data_url = Url.Action("SomeAction", "SomeController")
}
)
Now all that's left is to render this widget inside the _Layout.cshtml:
#Html.Action("Index", "Products", new { projectid = Request["projectId"] })
And now we could put some javascript so that when the user decides to change the selection he is redirected to some other action:
$(function() {
$('#projects').change(function() {
var url = $(this).data('url');
var projectId = encodeURIComponent($(this).val());
window.location.href = url + '?projectid=' + projectId;
});
});
Another possibility is to put the dropdown inside an HTML form:
#model ProjectViewModel
#using (Html.BeginForm("SomeAction", "SomeController", FormMethod.Get))
{
#Html.LabelFor(x => x.ProjectId)
#Html.DropDownListFor(
x => x.ProjectId,
Model.ProjectNames,
new {
id = "projects",
}
)
}
so that inside the javascript we don't have to worry about building urls when the selection changes and simply trigger the containing form submission:
$(function() {
$('#projects').change(function() {
$(this).closest('form').submit();
});
});
We just did a similiar thing on a project.
First, you can't really put it in a section because you have to put that section on every view, you could put it in a partial but you would still have to call it from every view.
Second, you can't really put it in the Layout page because the layout page isn't passed any kind of model. So I created an html helper and referenced that in the layout page. There are lots of tutorials on creating html helpers so I won't put the code here. But essentially in your html helper you can make a database call to get all of your projects. Then you can create a select list using string builder in the html helper and return that to the layout page. We then used jquery to add an on change event to the select list. When the select list changed it loaded a new page. So for example, in your select list the value of each item could be the project id, then on change it redirects them to a page like /Projects/View?id=234 where 234 is your project id.
So things to research. 1. Creating HTML Helpers 2. JQUERY change event.
That should get you in the right direction. Let me know if you need any other help and I can post some code.

ASP.NET MVC3 - Pagination Only (No WebGrid to be Displayed)

I'm seeking to display my images with a short title in a flow from left to right as opposed to the gridview that is a sequential layout. I desire to use the pagination that the webgrid provides so as not to recreate the wheel, as it were.
I've reviewed Leek's blog post (http://msdn.microsoft.com/en-gb/magazine/hh288075.aspx) and Broer's (http://blog.bekijkhet.com/2011/03/mvc3-webgrid-html-helper-paging.html) to ensure I have a solid introduction to the use of the webgrid but I'm falling short on how to apply the pagination without using the traditional layout of the webgrid.
I am using the Razor layouts.
Ideas or thoughts?
My controller is currently:
public ActionResult Index(string cid)
{
Catalog item = new Catalog();
item.objConnection.Open();
OdbcDataReader reader = item.getCatalogItems(cid, 3);
List<Catalog> listItems = new List<Catalog>();
while (reader.Read())
{
Catalog i = new Catalog();
i.kitName = reader.GetValue(1).ToString();
i.catalogID = reader.GetValue(0).ToString();
ViewBag.CatalogName = reader.GetValue(0).ToString(); //TODO: change this to name once in place
listItems.Add(i);
}
ViewBag.ItemsPerCatagory = listItems.Count;
reader.Close();
item.objConnection.Close();
return View(listItems);
}
My View:
#model IEnumerable<MvcApplication1.Models.Catalog>
#{
ViewBag.Title = ViewBag.CatalogName;
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>#ViewBag.CatalogName (#ViewBag.ItemsPerCatagory) Items Available</h2>
<p>
Category will have a brief description here for seo purposes.
</p>
<p>
#foreach (var catalog in Model)
{
string imageUrl = "http://web3.naeir.org/images/Specials/" + #catalog.kitName + ".JPG";
<img src=#imageUrl height="150px" width="150px" /> #Html.ActionLink(#catalog.kitName, "Details", "Product", new { cid = #catalog.catalogID, itemid = #catalog.kitName }, null)
}
</p>
Upon much hacking at my code I found that a simple option exist and my solution was to use MVCPager MVC Pager Website
I was able to simply download it via VS Web Developer Express Package Manager NuGet, read the documentation and implemented the code. Pretty straight forward.

Resources