متدهای کمکی مفید در پروژه های asp.net mvc
نویسنده: امیرحسین جلوداری
تاریخ: ۱۳۹۱/۰۴/۰۹ ۱۶:۱۶
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
public static class Helpers
{
//در اینجا متدها ی کمکی قرار میگیرند
}
public static MvcHtmlString FarsiDate(this HtmlHelper html, DateTime dateTime)
{
var tag = new TagBuilder("span");
tag.MergeAttribute("dir", "ltr");
tag.AddCssClass("farsi-date");
tag.SetInnerText(Calendar.ConvertToPersian(dateTime).ToString("W"));
return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));
}
@Html.FarsiDate(news.DateTimeCreated)
public static MvcHtmlString FarsiTime(this HtmlHelper html, DateTime dateTime)
{
var tag = new TagBuilder("span");
tag.MergeAttribute("dir", "ltr");
tag.AddCssClass("farsi-time");
tag.SetInnerText(Calendar.ConvertToPersian(dateTime).ToString("R"));
return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));
}
@Html.FarsiTime(news.DateTimeCreated)
public static MvcHtmlString FarsiDateAndTime(this HtmlHelper html, DateTime dateTime)
{
return MvcHtmlString.Create(FarsiTime(html, dateTime).ToHtmlString() + " , " + FarsiDate(html, dateTime).ToHtmlString());
}
@Html.FarsiDateAndTime(news.DateTimeCreated)
public static MvcHtmlString FarsiRemaining(this HtmlHelper html, DateTime dateTime)
{
var tag = new TagBuilder("span");
tag.MergeAttribute("dir", "rtl");
tag.AddCssClass("farsi-remaining");
tag.SetInnerText(Calendar.ConvertToPersian(dateTime).ToRelativeDateString("TY"));
return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));
}
@Html.FarsiRemaining(news.DateTimeCreated)
public static string GetSummary(this HtmlHelper html, string text, int max)
{
string summaryHtml = string.Empty;
// load our html document
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(text);
int wordCount = 0;
foreach (var element in htmlDoc.DocumentNode.ChildNodes)
{
// inner text will strip out all html, and give us plain text
string elementText = element.InnerText;
// we split by space to get all the words in this element
string[] elementWords = elementText.Split(new char[] { ' ' });
// and if we haven't used too many words ...
if (wordCount <= max)
{
// add the *outer* HTML (which will have proper
// html formatting for this fragment) to the summary
summaryHtml += element.OuterHtml;
wordCount += elementWords.Count() + 1;
}
else
{
break;
}
}
return summaryHtml;
}
@Html.Raw(Html.GetSummary(news.Content, 60))
public static List<string> GetListOfErrors(this ModelStateDictionary modelState)
{
var list = modelState.ToList();
var listErrors = new List<string>();
foreach (var keyValuePair in list)
{
listErrors.AddRange(keyValuePair.Value.Errors.Select(error => error.ErrorMessage));
}
return listErrors;
}
var listErrors = ModelState.GetListOfErrors();
public static string RemoveAllHtmlTags(string text)
{
return string.IsNullOrEmpty(text) ?
string.Empty :
Regex.Replace(text, @"<(.|\n)*?>", string.Empty);
}
public static string RemoveAllHtmlTags(this string text)
{
var withoutHtml = String.IsNullOrEmpty(text) ?
String.Empty :
Regex.Replace(text, @"<(.|\n)*?>", String.Empty);
withoutHtml = withoutHtml.Replace(" ", " ");
withoutHtml = withoutHtml.Replace("‌", " ");
withoutHtml = withoutHtml.Replace(""", " ");
withoutHtml = withoutHtml.Replace("amp;", "");
withoutHtml = withoutHtml.Replace("«", "«");
withoutHtml = withoutHtml.Replace("»", "»");
return withoutHtml;
} public static string TruncateAtWord(this string input, int length)
{
if (input == null || input.Length < length)
return input;
int iNextSpace = input.LastIndexOf(" ", length);
return string.Format("{0}...", input.Substring(0, (iNextSpace > 0) ? iNextSpace : length).Trim());
} public static class StringHelpers
{
public static string ToSeoUrl(this string url)
{
// make the url lowercase
string encodedUrl = (url ?? "").ToLower();
// replace & with and
encodedUrl = Regex.Replace(encodedUrl, @"\&+", "and");
// remove characters
encodedUrl = encodedUrl.Replace("'", "");
// remove invalid characters
encodedUrl = Regex.Replace(encodedUrl, @"[^a-z0-9-\u0600-\u06FF]", "-");
// remove duplicates
encodedUrl = Regex.Replace(encodedUrl, @"-+", "-");
// trim leading & trailing characters
encodedUrl = encodedUrl.Trim('-');
return encodedUrl;
}
}
var radio = doc.DocumentNode.SelectNodes("//input[@class='radio']");
string isChecked = element.GetAttributeValue("checked", "unchecked");
public static class HtmlHelperExtensions
{
private const string Nbsp = " ";
private const string SelectedAttribute = " selected='selected'";
public static MvcHtmlString NbspIfEmpty(this HtmlHelper helper, string value)
{
return new MvcHtmlString(string.IsNullOrEmpty(value) ? Nbsp : value);
}
public static MvcHtmlString SelectedIfMatch(this HtmlHelper helper, object expected, object actual)
{
return new MvcHtmlString(Equals(expected, actual) ? SelectedAttribute : string.Empty);
}
}
<select>
@foreach (var item in ViewBag.Items)
{
<option@Html.SelectedIfMatch((string)ViewBag.SelectedItem,
(string)item.ItemName)>@item.ItemName</option>
}
</select>
var data = context.Tabel1.Select(
p =>
new
{
p.Date1,
p.Comment,
p.Cost,
p.UserId,
}).FirstOrDefault(p.UserId == UserId);
var data = context.Tabel1.Select(
p =>
new
{
Date1= Persia.Calendar.ConvertToPersian(p.Date1),
p.Comment,
p.Cost,
p.UserId,
}).FirstOrDefault(p.UserId == UserId);
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
db.table.Where(...)
.Select(p =>
new
{
p.Date1,
p.Comment,
p.Cost,
p.UserId,
})
.AsEnumerable() // مهم
.Select(...در اینجا مجاز هستید از هر نوع تابعی استفاده کنید..)
.ToList();