Passing a param to the controller is null.. from examples ive seen im using correct overload.
Any help much appriceated
#{
foreach (string str in ViewBag.ServerNames)
{
<ul>
<img src="../../Content/Images/my_computer.png" alt="Computer Name"/>
<li >#Html.ActionLink(linkText: str.ToString(),actionName: "Index",controllerName:"Customer",
routeValues:new{str = str.ToString()} , htmlAttributes: null)</li>
</ul>
}
}
public ActionResult Index(string conName)
{
Response.Write("con name = " + conName);
Response.End();
string con = ConfigurationManager.ConnectionStrings[conName].ConnectionString;
trakman_Entities db = new trakman_Entities(con);
return View(db.customers.ToList());
}
browser source code
<ul>
<img src="../../Content/Images/my_computer.png" alt="Computer Name"/>
<li >DefaultConnection</li>
</ul>
<ul>
<img src="../../Content/Images/my_computer.png" alt="Computer Name"/>
<li >trakman_Entities</li>
</ul>
<ul>
<img src="../../Content/Images/my_computer.png" alt="Computer Name"/>
<li >trakman_Entities1</li>
</ul>
You should provide correct parameter name in action link and there is no need to specify parameters here :)
#{
foreach (string str in ViewBag.ServerNames)
{
<ul>
<img src="../../Content/Images/my_computer.png" alt="Computer Name"/>
<li >#Html.ActionLink(str.ToString(),"Index","Customer",
new{conName= str.ToString()} , null)</li>
</ul>
}
If you are using MVC out of the box then it is only likely to work with a parameter called id that accepts an int. To make this work you need to explicitly define the parameter in the URL as below:
trakman_Entities
Related
suppose I have the following list:
<div id='page_competition_1_block_competition_left_tree_2'>
<div>
<ul>
<li>
<a href="#" />
<ul>
<li>
<a href="#">
</li>
</ul>
</li>
<li>
<a href="#" />
...
how can I get the first a tag for each li?
I tried using:
HtmlNodeCollection compsLi = doc.DocumentNode
.SelectNodes("div[#id='page_match_1_block_competition_left_tree_2']//div//ul/li[1]");
but this will return null
You need to specify a single / instead of //, so:
HtmlNodeCollection compsLi = doc.DocumentNode.SelectNodes("div[#id='page_match_1_block_competition_left_tree_2']//ul/li/a");
Essentially:
/: search for the current node.
//: search from the root document node.
You should be able to iterate through the compsLi object you retrieve. Additionally, I don't think you need the [1] in your selector. Once you get the <li> item you should be able to do something like this:
foreach(var node in compsLi)
{
var aNode = node.SelectSingleNode("./a");
...
}
You can take a look here for something similar.
I'm having a big trouble trying to parse these html contents with HtmlAgilityPack library.
In this piece of code, I would like to retrieve only the url (href) that reffers to uploaded.net, but I can't determine whether the url reffers to it.
<div class='downloads' id='download_block'>
<h5 style='text-align:center'>FREE DOWNLOAD LINKS</h5>
<h4>uploadable.ch</h4>
<ul class='parts'>
<li>
text here
</li>
</ul>
<h4>uploaded.net</h4>
<ul class='parts'>
<li>
text here
</li>
</ul>
<h4>novafile.com</h4>
<ul class='parts'>
<li>
text here
</li>
</ul>
</div>
This is how it looks on the webpage
And this is what I have:
nodes = myHrmlDoc.DocumentNode.SelectNodes(".//div[#class='downloads']/ul[#class='parts']")
I can't just use an array-index to determine the position like:
nodes(0) = uploadable.ch node
nodes(1) = uploaded.net node
nodes(2) = novafile.com node
...because they could change the amount of nodes and its hosting positions.
Note that also the urls will not contains the hosting names, are redirections like:
http://xxxxxx/r/YEHUgL44xONfQAnCNUVw_aYfY5JYAy0DT-i--
What could I do, in C# or else VB.Net?.
this should do, untested though:
doc.DocumentNode.SelectSingleNode("//h4[contains(text(),'uploaded.net')]/following-sibling::ul//a").Attributes["href"].Value
also use contains because you never know if the text contains spaces.
The only way I see this working is 2 fold approach. Sorry, I don't have HtmlAgilityPack at hand, but here is an example of using the standard XmlDocument. Even though you said you can't use array indexes to access, this process should allow you to do that by specifically grabbing the correct index dynamically.
void Main()
{
var xml = #"
<div class=""downloads"" id=""download_block"">
<h5 style=""text-align:center"">FREE DOWNLOAD LINKS</h5>
<h4>uploadable.ch</h4>
<ul class=""parts"">
<li>
text here
</li>
</ul>
<h4>uploaded.net</h4>
<ul class=""parts"">
<li>
text here
</li>
</ul>
<h4>novafile.com</h4>
<ul class=""parts"">
<li>
text here
</li>
</ul>
</div>";
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xml);
var nav = xmlDocument.CreateNavigator();
var index = nav.Evaluate("count(//h4[text()='uploaded.net']/preceding-sibling::h4)+1").ToString();
var text = xmlDocument.SelectSingleNode("//ul["+index +"]//a/#href").InnerText;
Console.WriteLine(text);
}
Basically, it gets the index of the uploaded.net h4 and then uses that index to select the correct ul tag and get the URL out the of underlying anchor tag.
Sorry for the not so clean and error prone code, but it should get you in the right direction.
Give the snippet you supplied, this will help you get started.
var page = "<div class=\"downloads\" id=\"download_block\"> <h5 style=\"text-align:center\">FREE DOWNLOAD LINKS</h5> <h4>uploadable.ch</h4> <ul class=\"parts\"> <li> text here </li> </ul> <h4>uploaded.net</h4> <ul class=\"parts\"> <li> text here </li> </ul> <h4>novafile.com</h4> <ul class=\"parts\"> <li> text here </li> </ul></div>";
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(page);
var nodes = doc.DocumentNode.Descendants("h4").Where(n => n.InnerText.Contains("uploadable"));
foreach (var node in nodes)
{
var attr = node.NextSibling.NextSibling.Descendants().Where(x=> x.Name == "a").FirstOrDefault().Attributes["href"];
attr.Value.Dump();
}
I created a helper to build a list my problem it's that each time that if expression it's evaluate prints the value with true or false in my html. How can I avoid this behavior???
Here's my helper
#helper elementTipificacion(IEnumerable<prueba.Models.Tipificacion> datos){
foreach (var item in #datos)
{
if (#item.Tipificacion1.Count > 0) <--- this the expression thay print's true or false
{
<li>
#item.Nombre
<ul>
#elementTipificacion(#item.Tipificacion1)
</ul>
</li>
}
else{
<li>
#item.Nombre
</li>
}
#datos.ToList().Remove(#item);
}
}
And this the output
You have too many #. Use # only when you want to output a server side variable to the HTML. When you are inside a server side statement such as foreach, if, ... don't use #:
#helper elementTipificacion(IEnumerable<prueba.Models.Tipificacion> datos)
{
foreach (var item in datos)
{
if (item.Tipificacion1.Count > 0)
{
<li>
<a href="#" id="tipificacion-#item.IdTipificacion">
#item.Nombre
</a>
<ul>
#elementTipificacion(#item.Tipificacion1)
</ul>
</li>
}
else
{
<li>
<a href="#" id="tipificacion-#item.IdTipificacion">
#item.Nombre
</a>
</li>
}
datos.ToList().Remove(item); // <!-- not sure the usefulness of this line
// The .ToList() extension method returns a new list everytime you call it
// and you don't seem to be doing anything with the result of it, you don't even
// assign it to a variable
}
}
I use asp.net and C# 4.
I would like to know if is possible and how to retrieve a "Normal" (not asp.net) element in Code Behind.
For instance: I have a <li></li>, I would like get it from my logic and set is Visible to False.
At the moment I tried to change the MarkUp with:
<li ID="li-item" runat="server">
// Does not work I get Error: ID no identified....
I'm pretty new to Asp.Net, please give me a sample of code. Thanks for your support.
PS: I hope to do not get down votes because it is a too trivial question :)
"li-item" is not a valid identifier. And you need to close the li tag.
Try:
<li ID="li_item" runat="server"></li>
(I have used an underscore instead of -)
Now it should work
If it has an id and runat=server then you should be able to access it as a HtmlGenericControl, http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlgenericcontrol.aspx.
<ul runat="server" id="ULSlider" class="slider_bg_container">
<%-- <li>
<img src="images/Home_Main.jpg" alt="" width="704" height="312" />
</li>
<li>
<img src="images/Slde1.jpg" alt="" width="704" height="312" />
</li>
<li>
<img src="images/Slde2.jpg" alt="" width="704" height="312" />
</li>
<li>
<img src="images/Slde3.jpg" alt="" width="704" height="312" />
</li>
<li>
<img src="images/Slde4.jpg" alt="" width="704" height="312" />
</li>--%>
</ul>
and in code i use this like
protected void LoadData()
{
ImageGalleryBAL objImageBAl = new ImageGalleryBAL();
DataSet ds=objImageBAl.ImageGallery_GetALLImageForSlider();
String s="";
foreach (DataRow dr in ds.Tables[0].Rows)
{
string imgUrl =ConfigurationManager.AppSettings["SiteURL"]+ dr["ImageName"].ToString();
string AltText = dr["AltText"].ToString();
s=s+"<li><img src='"+imgUrl+"' alt='"+AltText+"' width='704' height='312' /> </li>";
}
ULSlider.InnerHtml = s;
}
I have an list that toggles with no problem in FF. I need this working IE for it to be production ready.
It seems (IE) to apply the js to the first #orderItem and the first #familiy only. The rest of the items in the list are ignored.
Any help would be great.
A piece of the HTML (large list):
<div class="classificationContainer">
<ul class="classification" id="orderUL">
<li id="orderItem" class="ordrheading">
<div class="order">
<a href="?nav=search_recherche&lang=en">
<img src="http://dev.ncr.ec.gc.ca/publications/bba-aob/images/node_closedc.gif" alt="By Classification" id="OrdListImage" />
Apodiformes (Swifts and Hummingbirds)
</a>
</div>
<ul class="classification" id="FamilyList">
<li id="familiy">
<div class="family">
<a href="?nav=search_recherche&lang=en">
<img src="http://dev.ncr.ec.gc.ca/publications/bba-aob/images/node_closedc.gif" alt="Family" id="FamListImage" />
Apodidae (Swifts)
</a>
</div>
<ul class="classification" id="SpiecesList">
<li>
<img src="http://dev.ncr.ec.gc.ca/publications/bba-aob/images/node_leafc.gif" alt="Species" />
Chimney Swift (Chaetura pelagica)
</li>
</ul>
</li>
<li id="familiy">
<div class="family">
<a href="?nav=search_recherche&lang=en">
<img src="http://dev.ncr.ec.gc.ca/publications/bba-aob/images/node_closedc.gif" alt="Family" id="FamListImage" />
Trochilidae (Hummingbirds)
</a>
</div>
<ul class="classification" id="SpiecesList">
<li>
<img src="http://dev.ncr.ec.gc.ca/publications/bba-aob/images/node_leafc.gif" alt="Species" />
Ruby throated Hummingbird (Archilochus colubris)
</li>
<li>
<img src="http://dev.ncr.ec.gc.ca/publications/bba-aob/images/node_leafc.gif" alt="Species" />
Rufous Hummingbird (Selasphorus rufus)
</li>
</ul>
</li>
</ul>
</li></ul></div>
I have the following jquery functions:
<script type="text/javascript">
$(document).ready(function () {
// toggle action for the order to familiy
$("li#orderItem").click(function (event) {
// toggle the image
var src = ($("#OrdListImage", this).attr("src") == "/images/node_closedc.gif")
? "/images/node_openc.gif"
: "/images/node_closedc.gif";
$("img#OrdListImage", this).attr("src", src);
//toggle the ul
$('ul#FamilyList', this).toggle($('ul#FamilyList', this).css('display') == 'none');
// stop all link actions
return false;
});
//toggle action from familiy to speices
$("li#familiy").click(function () {
// toggle the image
var src = ($("#FamListImage", this).attr("src") == "/images/node_closedc.gif")
? "/images/node_openc.gif"
: "/images/node_closedc.gif";
$("img#FamListImage", this).attr("src", src);
//toggle the ul
$('ul#SpiecesList', this).toggle($('ul#SpiecesList', this).css('display') == 'none');
// stop all link actions
return false;
});
});
Also check if id's are not repeated (there is only one #orderItem, only one #familiy and etc.). "id" attribute must be unique in html document, "class" can be repeated.
The toggle function provided by jQuery is not guaranteed to work. I lost the reference where I read this (was on jQuery's homepage). I encountered the same problem and (as suggested by jQuery) implemented my own toggle function. I'd suggest trying this, as it's not much work and could provide you a solution.