How can I find all items beginning with a or â? - asp.net-mvc

I have a list of items that are grouped by their first letter. By clicking a letter the user gets alle entries that begin with that letter.
This does not work for french. If I choose the letter a, items with â are not returned.
What is a good way to return items no matter if they have an accent or not?
<% char alphaStart = Char.Parse("A");
char alphaEnd = Char.Parse("Z"); %>
<% for (char i = alphaStart; i <= alphaEnd; i++) { %>
<% char c = i; %>
<% var abcList = Model.FaqList.Where(x => x.CmsHeader.StartsWith(c.ToString())).ToList(); %>
<% if (abcList.Count > 0 ) { %>
<div class="naviPkt">
<a id="<%= i.ToString().ToUpper() %>" class="naviPktLetter" href="#<%= i.ToString().ToLower() %>"><%= i.ToString().ToUpper() %></a>
</div>
<ul id="menuGroup<%= i.ToString().ToUpper() %>" class="contextMenu" style="display:none;">
<% foreach (var info in abcList) { %>
<li class="<%= info.CmsHeader%>">
<a id="infoId<%= info.CmsInfoId%>" href="#<%= info.CmsInfoId%>" class="abcEntry"><%= info.CmsHeader%></a>
</li>
<% } %>
</ul>
<% } %>
<% } %>

You can easily remove the diacritic signs (accents, umlaut, cedilla and so on) from the string to test only on the base character. This is done by normalizing the string to Unicode form D and removing the character of category "NonSpacingMark". The following extension method strips a string of its diacritic signs :
public static string RemoveDiacritics(this string s)
{
if (s == null)
throw new ArgumentNullException("s");
string formD = s.Normalize(NormalizationForm.FormD);
string noDiacriticsFormD = new string(
formD.Where(c => CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)
.ToArray());
string noDiacritics = noDiacriticsFormD.Normalize(NormalizationForm.FormC);
return noDiacritics;
}
Using this method, your code becomes :
var abcList = Model.FaqList.Where(x => x.CmsHeader.RemoveDiacritics().StartsWith(c.ToString())).ToList();

Try using i.ToString().ToUpper(StringComparison.InvariantCultureIgnoreCase). It uses a culture neutral model for case conversion, which should eliminate accents.

In addition to LBushkin's answer (which I think will work), you can clean up your alphabet generation.
var A = Char.Parse("A");
var alphabet = Enumerable.Range(0, 26).Select(i => A + i);
Then you can use a foreach loop and forget the whole c = i thing. Also you don't need the .ToUpper() calls because you already have capital lettters.

Related

Insert Data Into My DB trouble

This is the View code, this view will show a list of preInscription Demande and two button Valide and Delete the first one it allow Webmaster To Inscription and the seconde one to Refuse Demande.
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<J2V.Models.preinscrit>>" %>
// some Html Code
<% foreach (var item in Model) { %>
<li>
<div class="listinfo">
<h3>
<%: Html.DisplayFor(modelItem => item.Nag) %>
</h3>
<p>
<%: Html.DisplayFor(modelItem => item.Idag) %>
</p>
<span class="price"> <%: Html.DisplayFor(modelItem => item.Adrag) %> <%: Html.DisplayFor(modelItem => item.Vilag) %> <%: Html.DisplayFor(modelItem => item.Gov) %></span> <span class="media">Tel : <%: Html.DisplayFor(modelItem => item.Telag) %> |</span> <%: Html.DisplayFor(modelItem => item.Mailag) %>
</div>
<div class="listingbtns">
<span class="listbuttons"><%: Html.ActionLink("Valide", "Valide", new {mod= item}) %> |</span>
<span class="listbuttons"><%: Html.ActionLink("Supprime", "Delete", new { id=item.Idag }) %></span>
</div>
<div class="clear">
</div>
</li>
<% } %>
This is the "Valide" Action it allow To Validate preinscription and add Data to Agence Table and user table:
[HttpGet]
public ActionResult Valide(Models.preinscrit model )
{
var db = new Models.J2VEntities();
Models.agence ag = new Models.agence();
Models.user user = new Models.user();
ag.Adrag = model.Adrag ;
ag.Gov = model.Gov ;
ag.Idag = model.Idag;
ag.Mailag = model.Mailag;
ag.Nomag = model.Nag;
ag.Vilag = model.Vilag;
user.IsAdmin = false;
user.iduser = model.Idag;
user.password = Models.LogModel.register.CreateRandomPassword();
db.AddTouser(user);
db.AddToagence(ag);
return View("index");
}
When I click on Valide on my view page I get this error :
System.NullReferenceException
at this line ag.Adrag = model.Adrag ;
no need to use form. use actionlink to send item.id.
<span class="listbuttons"><%: Html.ActionLink("Valide", "Valide", new {id= item.id}) %> |</span>
and get id in valide action and get the model in your database by item.id
public ActionResult Valide(int id )
{
var db = new Models.J2VEntities();
Models.agence ag = new Models.agence();
Models.user user = new Models.user();
//select from databese by id and valide
ag.Adrag = model.Adrag ;
ag.Gov = model.Gov ;
ag.Idag = model.Idag;
ag.Mailag = model.Mailag;
ag.Nomag = model.Nag;
ag.Vilag = model.Vilag;
user.IsAdmin = false;
user.iduser = model.Idag;
user.password = Models.LogModel.register.CreateRandomPassword();
db.AddTouser(user);
db.AddToagence(ag);
return View("index");
}
this code is from my project :
//select list by id
public IEnumerable<makale> select_makaleler_by_kategori(int kategori_id)
{
IEnumerable<makale> makale = (from m in entity.makale
where m.kategori_id == kategori_id
orderby m.olusturma_tarihi
select m).ToList<makale>();
return makale;
}
//select one element by id
public makale select_makale(int makale_id)
{
makale makale = (from m in entity.makale
where m.makale_id == makale_id
select m).SingleOrDefault();
return makale;
}
you are using ActionLink for the wrong reason , it isn't used for passing data , you should make a <form> and then submit it using <input type='submit' />
the problem happens because the 3rd parameter in the ActionLink() is the routing values that works with the routing rules , its not used for sending data
from the code sample i think the user didnt enter anything the view just for showing the page
if that is the case then just pass an id to the ActionLink and in the Controller action query again all the information from the database using the id
never depend on passing parameter used for showing purpose from page to page , a hacker can easily break your app
so if you dont want information from the user just pass the id then query again in the other page

Displaying div tags conditionally

I have a view that lists an item, then any reviews it may/may not have. The issue is that there will be an arbitrary (possibly 0) number of reviews. I format each review in its own div element, so I'll have to display 'n' div elements. How can this be done?
Edit, sample element contents:
<div id="promo_item">
Promotion:
<br>
<br>
<table>
#rows for item name, price, list-date
#rows for the values of item name, price, list-date
</table>
#comment goes here
</div>
This is just an example, I probably won't implement it exactly like this, but I do know there has to be a table for the values.
<% #post.reviews.each do |r| %>
<div>stuf here <%= r.user.name #etc.... %></div>
<% end %>
Use javascript to change the properties on the div elements.
You can put this script into your HTML:
How to Show & Hide your Divs
<script language="javascript">
function toggle() {
var el = document.getElementById("toggleText");
var text = document.getElementById("displayText");
if(el.style.display == "block") {
el.style.display = "none";
text.innerHTML = "show";
}
else {
ele.style.display = "block";
text.innerHTML = "hide";
}
}
</script>

Problem: "[action]" does not accept any expressions

I tried to use the following code :
<li>
<% for (int i=0; i<parentList.size(); i++) {
Role p = parentList.get(i);
%>
<li><%=p.getFuncname() %>
<ul>
<% for (int j=0; j<roleList.size(); j++) {
Role c = roleList.get(j);
if (!c.getFuncid().equalsIgnoreCase(c.getParentfunc()) && c.getParentfunc().equalsIgnoreCase(p.getFuncid()))
{
%>
<li><%=c.getFuncname() %>
<%
}
}
%>
</ul>
<% } %>
</li>
but it throw error:
JSPG0227E: Exception caught while translating /menu.jsp:
/menu.jsp(95,17) --> JSPG0124E: According to TLD or attribute directive in tag file, attribute "[action]" does not accept any expressions. Value of expression is: "[%=p.getFunclink() %]".
How can i fix it? Thanks!
The Struts tags cannot accept expressions, thus:
<s:url action="<%=p.getFunclink() %>"/>
is invalid.
First, avoid using scriptlets at all.
Second, look into using the <s:iterator/> tag to iterate over your collections.

MVC - Displaying Content

How to Display the following
public ActionResult Index()
{
IEnumerable<int> items = Enumerable.Range(1000, 5);
ViewData["Collection"] = items;
return View();
}
in "View"
<ul>
<% foreach(int i in (IEnumerable)ViewData["Collection"]){ %>
<li>
<% =i.ToString(); }%>
</li>
</ul>
the foreach throws System.Web.HttpCompileException.
You had the closing brace of the foreach's loop in the wrong place. This is what you need:
<ul>
<% foreach (int i in (IEnumerable)ViewData["Collection"]) { %>
<li>
<%= i.ToString() %>
</li>
<% } %>
</ul>
And you also had some other extra punctuation in there as well (such as an extra semicolon).

In a View, What is the most efficient way to hide HTML related to a null value from the Model?

This is likely a very simple question with a straightforward answer but I'm something of a newbie when it comes to ASP.NET (MVC).
I am returning an address (in pieces) from my model. Some of components are null. Is there a simple or fluent-like way to check for that null value without a lot of extra code to determine whether or not to display the associated surrounding HTML (not just the value)?
Example:
<% foreach (var item in Model)
{ %>
<h3>
<%= Html.ActionLink(item.name, "Details", new { id = item.ID})%></h3>
<div>
<%= Html.Encode(item.address) %><br />
<%= Html.Encode(item.city) %>,
<%= Html.Encode(item.state) %>
<%= Html.Encode(item.zip) %>
</div>
<% } %>
In the above example, if there is a null value for item.address, I want the <br/> tag to be hidden as well so that only the city, state zip string is displayed.
I'm looking for something more elegant than just putting a <% if () { %> conditional out there. Thanks.
You could write an extension method for HtmlHelper that checked to see if it was null or not, and would output nothing if it was, or field + <br /> if it wasn't.
public static string FieldLine(this HtmlHelper helper, object value, bool addBr)
{
if (value == null)
{
return string.Empty;
}
else if (addBr)
{
return helper.Encode(value) + "<br />";
}
else
{
return helper.Encode(value);
}
}
Remember to import the namespace of your extension class into your View aspx. For this example, if my namespace was "MvcApplication1.Extensions", I would use
<%# Import Namespace="MvcApplication1.Extensions" %>
at the top of my View. Then to use it, it would simply be:
<%= Html.FieldLine(item.address, true) %>
<%= Html.FieldLine(item.city, true) %>
etc.
I'm adding another answer based on what womp described above.. I'd make the helper a bit more generic than he did, and still honor the origional Encode as well...
public static string EncodeWithHtml(this HtmlHelper helper, object value, string html)
{
if (value == null)
{
return string.Empty;
}
else
{
return helper.Encode(value) + html;
}
}
This would allow you to do something like:
<%= Html.EncodeWithHtml(item.address, "<br />") %>
or
<%= Html.EncodeWithHtml(item.address, "<img src=\"images\home.gif\"><br />") %>
Assuming item.address is a string...
<%= Html.Encode(item.address) %>
<% if (!string.IsNullOrEmpty(item.address)) { %>
<br />
<% } %>
of course, this is typed out in the little comment box, so be wary of spelling, case, etc, etc.

Resources