पाठ का उद्देश्य । रेजर का उपयोग करके html में डेटा निकालना सीखें। Helpery। PageableData। गतिशील रूप। RedirectToLogin, RedirectToNotFoundPage। त्रुटि पृष्ठ। RssActionResult।
आधार
इसलिए, आइए देखें कि व्यू का हिस्सा कैसे काम करता है।
कंट्रोलर में, सभी एक्शन मेथड्स एक ActionResult टाइप लौटाते हैं। और परिणाम प्रदर्शित करने के लिए, हम उपयोग करते हैं:
return View(modelData);
दृश्य के मुख्य पैरामीटर निम्न हो सकते हैं:
देखने के लिए उपयोग करने का विकल्प निम्नानुसार है:
- फ़ोल्डर में खोजा गया / क्षेत्र / [क्षेत्र] / दृश्य / [नियंत्रक नाम] /
- / क्षेत्र / [क्षेत्र] / दृश्य / साझा / में खोजा गया
- फ़ोल्डर में खोजे गए / दृश्य / [नियंत्रकनाम] /
- फ़ोल्डर में खोजे गए / दृश्य / साझा /
चलो अध्ययन शुरू करते हैं।
उस्तरा
व्यू बनाते समय, दो इंजनों के बीच एक विकल्प होता है: ASPX और रेजर। हम भविष्य में पहले का उपयोग नहीं करेंगे, तो आइए रेजर के बारे में बात करते हैं।

ASPX कोड निष्पादन के लिए
<% %>
%%
<% %>
टैग के साथ बोझिल इंजन था और डेटा आउटपुट के लिए
<%: %>
।
रेजर
@Model.Name
का उपयोग करता है। यानी सब कुछ है कि
@
साथ शुरू होता है या तो कोड निष्पादन या डेटा आउटपुट
@foreach() {…}
, या
@if() { … } else { … }
:
@if (Model.Any()) { <p></p> } @foreach (var role in Model) { <div class="item"> <span class="id"> @role.ID </span> <span class="name"> @role.Name </span> <span class="Code"> @role.Code </span> </div> }
अंदर {} टैग हैं - यह इस तथ्य का एक मार्कर है कि यह एक टेम्पलेट है। टेम्पलेट के अंदर सरल कोड निष्पादन के लिए, हम @ {कोड} संरचना का उपयोग करते हैं, विशेषताओं या पाठ के अंदर डेटा के सही आउटपुट के लिए, निर्माण @ (स्ट्रिंग परिणाम) है:
@{ int i = 0; } @foreach (var role in Model) { <div class="item @(i % 2 == 0 ? "odd" : "")"> <span class="id"> @role.ID </span> <span class="name"> @role.Name </span> <span class="Code"> @role.Code </span> </div> i++; }
गैर-टैग किए गए पाठ को प्रदर्शित करने के लिए, आपको छद्म टैग का उपयोग करने की आवश्यकता है:
@foreach (var role in Model) { @role.Name<text>, </text> }
HTML पाठ को आउटपुट करने के लिए, या तो MvcHtmlString को लौटाया जाना चाहिए, या
Html .Raw (html-string-value) निर्माण का उपयोग करें, अन्यथा पाठ बच गए टैग के साथ प्रदर्शित किया जाएगा।
PageableData
डेटाबेस से तालिका के पृष्ठ आउटपुट पर विचार करें। आइए विश्लेषण करते हैं:
- नियंत्रक को पैरामीटर में उस पृष्ठ का मूल्य प्राप्त करना चाहिए जिसे हम आउटपुट करेंगे
- डिफ़ॉल्ट रूप से, यह पहला पृष्ठ होगा।
- निष्कर्ष में, हमें पता होना चाहिए:
- प्रदर्शित करने के लिए डेटाबेस आइटम की सूची
- पृष्ठों की संख्या
- वर्तमान पृष्ठ
जेनेरिक क्लास बनाएं PageableData (/Models/Info/PageableData.cs):
public class PageableData<T> where T : class { protected static int ItemPerPageDefault = 20; public IEnumerable<T> List { get; set; } public int PageNo { get; set; } public int CountPage { get; set; } public int ItemPerPage { get; set; } public PageableData(IQueryable<T> queryableSet, int page, int itemPerPage = 0) { if (itemPerPage == 0) { itemPerPage = ItemPerPageDefault; } ItemPerPage = itemPerPage; PageNo = page; var count = queryableSet.Count(); CountPage = (int)decimal.Remainder(count, itemPerPage) == 0 ? count / itemPerPage : count / itemPerPage + 1; List = queryableSet.Skip((PageNo - 1) * itemPerPage).Take(itemPerPage); } }
डिफ़ॉल्ट रूप से, पृष्ठ पर प्रदर्शित मानों की संख्या 20 है, लेकिन हम इस पैरामीटर को कंस्ट्रक्टर में बदल सकते हैं। हम IQueryable पास करते हैं और संख्या की गणना करते हैं CountPage। PageNo का उपयोग करके, पृष्ठ का चयन करें:
List = queryableSet.Skip((PageNo - 1) * itemPerPage).Take(itemPerPage);
नियंत्रक में हम उपयोग करते हैं:
public class UserController : DefaultController { public ActionResult Index(int page = 1) { var data = new PageableData<User>(Repository.Users, page, 30); return View(data); } …
दृश्य में हम इस वर्ग का उपयोग करते हैं:
@model LessonProject.Models.Info.PageableData<LessonProject.Model.User> @{ ViewBag.Title = "Users"; Layout = "~/Areas/Default/Views/Shared/_Layout.cshtml"; } <h2>Users</h2> <p> @foreach (var user in Model.List) { <div class="item"> <span class="id"> @user.ID </span> <span class="email"> @user.Email </span> <span class="activateDate"> @user.AddedDate </span> </div> } </p>
चलाएं, जांचें (http: // localhost / उपयोगकर्ता)

जारी रखने के लिए, हम अधिक डेटा उत्पन्न करेंगे (सर्वर एक्सप्लोरर में तालिका में सिर्फ ctrl-c, ctrl-v)

चलिए हेल्पर पेजिनेटर बनाने की ओर बढ़ते हैं, जो हमें इस सूची के माध्यम से स्क्रॉल करने का अवसर देगा।
हेल्पर (पेजरेलपर)
चूंकि हम बूटस्ट्रैप का उपयोग करते हैं, इसलिए हम इसके आधार पर एक पेजिनेटर भी बनाएंगे। कोड में, यह इस तरह दिखता है:
<div class="pagination"> <ul> <li><a href="#">Prev</a></li> <li><a href="#">1</a></li> <li><a href="#">2</a></li> <li><a href="#">3</a></li> <li><a href="#">4</a></li> <li><a href="#">5</a></li> <li><a href="#">Next</a></li> </ul> </div>
हम केवल अंदर में रुचि रखते हैं।
हेल्पर को System.Web.Mvc.HtmlHelper क्लास के लिए एक्सटेंशन के रूप में बनाया गया है। योजना इस प्रकार है:
- आउटपुट Prev (यदि आवश्यक हो तो सक्रिय करें)
- पहले तीन पृष्ठों 1, 2, 3 के लिंक प्रदर्शित करें
- यदि आवश्यक हो तो इलिप्सिस प्रिंट करें
- वर्तमान पृष्ठ का सक्रिय लिंक प्रदर्शित करें
- यदि आवश्यक हो तो इलिप्सिस प्रिंट करें
- अंतिम तीन पृष्ठ प्रदर्शित करें
- अगला प्रिंट करें (यदि आवश्यक हो तो सक्रिय करें)
- उल में सब कुछ संलग्न करें और MvcHtmlString के रूप में प्रिंट करें
कोड इस तरह दिखेगा:
public static MvcHtmlString PageLinks(this HtmlHelper html, int currentPage, int totalPages, Func<int, string> pageUrl) { StringBuilder builder = new StringBuilder();
Namepace LessonProject जोड़ें। दृश्य में घोषणाओं को पूरा करें। ऐसा करने के दो तरीके हैं:
- व्यू में ही
@using LessonProject.Helper;
- Web.config में (अनुशंसित)
<configSections> … <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"> <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" /> <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" /> </sectionGroup> </configSections> + <system.web.webPages.razor> <pages pageBaseType="System.Web.Mvc.WebViewPage"> <namespaces> <add namespace="LessonProject.Helper" /> </namespaces> </pages> </system.web.webPages.razor>
दृश्य में पेजिनेटर जोड़ें:
<div class="pagination"> @Html.PageLinks(Model.PageNo, Model.CountPage, x => Url.Action("Index", new {page = x})) </div>
डिजाइन पर ध्यान दें
x => Url.Action("Index", new {page = x})
यह वह प्रतिनिधि है जो पृष्ठ का लिंक वापस करता है। और Url.Action () - पैरामीटर पेज = x के साथ पृष्ठ / उपयोगकर्ता / सूचकांक के लिए एक लिंक बनाता है।
यहां ऐसा हुआ है (प्रति पृष्ठ आउटपुट की मात्रा घटाकर 5 करने के लिए ताकि अधिक पृष्ठ बन जाएं):

searchengine
डेटा देखने का अगला चरण खोज बनाने के लिए होगा। डेटा क्षेत्रों में से एक में प्रतिस्थापन के संयोग से खोज सरल होगी। इनपुट पैरामीटर सर्चस्ट्रिंग है।
public ActionResult Index(int page = 1, string searchString = null) { if (!string.IsNullOrWhiteSpace(searchString)) {
मान लेने वाला एक वर्ग
SearchEngine
बनाएँ
IQueryable, , (/Global/SearchEngine.cs):
, , [,], {,}, (,):
/// <summary> /// The regex strip html. /// </summary> private static readonly Regex RegexStripHtml = new Regex("<[^>]*>", RegexOptions.Compiled); private static string StripHtml(string html) { return string.IsNullOrWhiteSpace(html) ? string.Empty : RegexStripHtml.Replace(html, string.Empty).Trim(); } private static string CleanContent(string content, bool removeHtml) { if (removeHtml) { content = StripHtml(content); } content = content.Replace("\\", string.Empty). Replace("|", string.Empty). Replace("(", string.Empty). Replace(")", string.Empty). Replace("[", string.Empty). Replace("]", string.Empty). Replace("*", string.Empty). Replace("?", string.Empty). Replace("}", string.Empty). Replace("{", string.Empty). Replace("^", string.Empty). Replace("+", string.Empty); var words = content.Split(new[] { ' ', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); var sb = new StringBuilder(); foreach (var word in words.Select(t => t.ToLowerInvariant().Trim()).Where(word => word.Length > 1)) { sb.AppendFormat("{0} ", word); } return sb.ToString(); }
:
public static IEnumerable<User> Search(string searchString, IQueryable<User> source) { var term = CleanContent(searchString.ToLowerInvariant().Trim(), false); var terms = term.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); var regex = string.Format(CultureInfo.InvariantCulture, "({0})", string.Join("|", terms)); foreach (var entry in source) { var rank = 0; if (!string.IsNullOrWhiteSpace(entry.Email)) { rank += Regex.Matches(entry.Email.ToLowerInvariant(), regex).Count; } if (rank > 0) { yield return entry; } } }
. regex . , Email .
:
, , «cher [2]», , «cher 2». regex = (cher|2). , IQueryable
, IEnumerable - yield return entry
IQueryable, , (/Global/SearchEngine.cs):
, , [,], {,}, (,):
/// <summary> /// The regex strip html. /// </summary> private static readonly Regex RegexStripHtml = new Regex("<[^>]*>", RegexOptions.Compiled); private static string StripHtml(string html) { return string.IsNullOrWhiteSpace(html) ? string.Empty : RegexStripHtml.Replace(html, string.Empty).Trim(); } private static string CleanContent(string content, bool removeHtml) { if (removeHtml) { content = StripHtml(content); } content = content.Replace("\\", string.Empty). Replace("|", string.Empty). Replace("(", string.Empty). Replace(")", string.Empty). Replace("[", string.Empty). Replace("]", string.Empty). Replace("*", string.Empty). Replace("?", string.Empty). Replace("}", string.Empty). Replace("{", string.Empty). Replace("^", string.Empty). Replace("+", string.Empty); var words = content.Split(new[] { ' ', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); var sb = new StringBuilder(); foreach (var word in words.Select(t => t.ToLowerInvariant().Trim()).Where(word => word.Length > 1)) { sb.AppendFormat("{0} ", word); } return sb.ToString(); }
:
public static IEnumerable<User> Search(string searchString, IQueryable<User> source) { var term = CleanContent(searchString.ToLowerInvariant().Trim(), false); var terms = term.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); var regex = string.Format(CultureInfo.InvariantCulture, "({0})", string.Join("|", terms)); foreach (var entry in source) { var rank = 0; if (!string.IsNullOrWhiteSpace(entry.Email)) { rank += Regex.Matches(entry.Email.ToLowerInvariant(), regex).Count; } if (rank > 0) { yield return entry; } } }
. regex . , Email .
:
, , «cher [2]», , «cher 2». regex = (cher|2). , IQueryable
, IEnumerable - yield return entry
IQueryable, , (/Global/SearchEngine.cs):
, , [,], {,}, (,):
/// <summary> /// The regex strip html. /// </summary> private static readonly Regex RegexStripHtml = new Regex("<[^>]*>", RegexOptions.Compiled); private static string StripHtml(string html) { return string.IsNullOrWhiteSpace(html) ? string.Empty : RegexStripHtml.Replace(html, string.Empty).Trim(); } private static string CleanContent(string content, bool removeHtml) { if (removeHtml) { content = StripHtml(content); } content = content.Replace("\\", string.Empty). Replace("|", string.Empty). Replace("(", string.Empty). Replace(")", string.Empty). Replace("[", string.Empty). Replace("]", string.Empty). Replace("*", string.Empty). Replace("?", string.Empty). Replace("}", string.Empty). Replace("{", string.Empty). Replace("^", string.Empty). Replace("+", string.Empty); var words = content.Split(new[] { ' ', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); var sb = new StringBuilder(); foreach (var word in words.Select(t => t.ToLowerInvariant().Trim()).Where(word => word.Length > 1)) { sb.AppendFormat("{0} ", word); } return sb.ToString(); }
:
public static IEnumerable<User> Search(string searchString, IQueryable<User> source) { var term = CleanContent(searchString.ToLowerInvariant().Trim(), false); var terms = term.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); var regex = string.Format(CultureInfo.InvariantCulture, "({0})", string.Join("|", terms)); foreach (var entry in source) { var rank = 0; if (!string.IsNullOrWhiteSpace(entry.Email)) { rank += Regex.Matches(entry.Email.ToLowerInvariant(), regex).Count; } if (rank > 0) { yield return entry; } } }
. regex . , Email .
:
, , «cher [2]», , «cher 2». regex = (cher|2). , IQueryable
, IEnumerable - yield return entry
IQueryable, , (/Global/SearchEngine.cs):
, , [,], {,}, (,):
/// <summary> /// The regex strip html. /// </summary> private static readonly Regex RegexStripHtml = new Regex("<[^>]*>", RegexOptions.Compiled); private static string StripHtml(string html) { return string.IsNullOrWhiteSpace(html) ? string.Empty : RegexStripHtml.Replace(html, string.Empty).Trim(); } private static string CleanContent(string content, bool removeHtml) { if (removeHtml) { content = StripHtml(content); } content = content.Replace("\\", string.Empty). Replace("|", string.Empty). Replace("(", string.Empty). Replace(")", string.Empty). Replace("[", string.Empty). Replace("]", string.Empty). Replace("*", string.Empty). Replace("?", string.Empty). Replace("}", string.Empty). Replace("{", string.Empty). Replace("^", string.Empty). Replace("+", string.Empty); var words = content.Split(new[] { ' ', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); var sb = new StringBuilder(); foreach (var word in words.Select(t => t.ToLowerInvariant().Trim()).Where(word => word.Length > 1)) { sb.AppendFormat("{0} ", word); } return sb.ToString(); }
:
public static IEnumerable<User> Search(string searchString, IQueryable<User> source) { var term = CleanContent(searchString.ToLowerInvariant().Trim(), false); var terms = term.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); var regex = string.Format(CultureInfo.InvariantCulture, "({0})", string.Join("|", terms)); foreach (var entry in source) { var rank = 0; if (!string.IsNullOrWhiteSpace(entry.Email)) { rank += Regex.Matches(entry.Email.ToLowerInvariant(), regex).Count; } if (rank > 0) { yield return entry; } } }
. regex . , Email .
:
, , «cher [2]», , «cher 2». regex = (cher|2). , IQueryable
, IEnumerable - yield return entry
IQueryable, , (/Global/SearchEngine.cs):
, , [,], {,}, (,):
/// <summary> /// The regex strip html. /// </summary> private static readonly Regex RegexStripHtml = new Regex("<[^>]*>", RegexOptions.Compiled); private static string StripHtml(string html) { return string.IsNullOrWhiteSpace(html) ? string.Empty : RegexStripHtml.Replace(html, string.Empty).Trim(); } private static string CleanContent(string content, bool removeHtml) { if (removeHtml) { content = StripHtml(content); } content = content.Replace("\\", string.Empty). Replace("|", string.Empty). Replace("(", string.Empty). Replace(")", string.Empty). Replace("[", string.Empty). Replace("]", string.Empty). Replace("*", string.Empty). Replace("?", string.Empty). Replace("}", string.Empty). Replace("{", string.Empty). Replace("^", string.Empty). Replace("+", string.Empty); var words = content.Split(new[] { ' ', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); var sb = new StringBuilder(); foreach (var word in words.Select(t => t.ToLowerInvariant().Trim()).Where(word => word.Length > 1)) { sb.AppendFormat("{0} ", word); } return sb.ToString(); }
:
public static IEnumerable<User> Search(string searchString, IQueryable<User> source) { var term = CleanContent(searchString.ToLowerInvariant().Trim(), false); var terms = term.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); var regex = string.Format(CultureInfo.InvariantCulture, "({0})", string.Join("|", terms)); foreach (var entry in source) { var rank = 0; if (!string.IsNullOrWhiteSpace(entry.Email)) { rank += Regex.Matches(entry.Email.ToLowerInvariant(), regex).Count; } if (rank > 0) { yield return entry; } } }
. regex . , Email .
:
, , «cher [2]», , «cher 2». regex = (cher|2). , IQueryable
, IEnumerable - yield return entry
IQueryable, , (/Global/SearchEngine.cs):
, , [,], {,}, (,):
/// <summary> /// The regex strip html. /// </summary> private static readonly Regex RegexStripHtml = new Regex("<[^>]*>", RegexOptions.Compiled); private static string StripHtml(string html) { return string.IsNullOrWhiteSpace(html) ? string.Empty : RegexStripHtml.Replace(html, string.Empty).Trim(); } private static string CleanContent(string content, bool removeHtml) { if (removeHtml) { content = StripHtml(content); } content = content.Replace("\\", string.Empty). Replace("|", string.Empty). Replace("(", string.Empty). Replace(")", string.Empty). Replace("[", string.Empty). Replace("]", string.Empty). Replace("*", string.Empty). Replace("?", string.Empty). Replace("}", string.Empty). Replace("{", string.Empty). Replace("^", string.Empty). Replace("+", string.Empty); var words = content.Split(new[] { ' ', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); var sb = new StringBuilder(); foreach (var word in words.Select(t => t.ToLowerInvariant().Trim()).Where(word => word.Length > 1)) { sb.AppendFormat("{0} ", word); } return sb.ToString(); }
:
public static IEnumerable<User> Search(string searchString, IQueryable<User> source) { var term = CleanContent(searchString.ToLowerInvariant().Trim(), false); var terms = term.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); var regex = string.Format(CultureInfo.InvariantCulture, "({0})", string.Join("|", terms)); foreach (var entry in source) { var rank = 0; if (!string.IsNullOrWhiteSpace(entry.Email)) { rank += Regex.Matches(entry.Email.ToLowerInvariant(), regex).Count; } if (rank > 0) { yield return entry; } } }
. regex . , Email .
:
, , «cher [2]», , «cher 2». regex = (cher|2). , IQueryable
, IEnumerable - yield return entry
IQueryable, , (/Global/SearchEngine.cs):
, , [,], {,}, (,):
/// <summary> /// The regex strip html. /// </summary> private static readonly Regex RegexStripHtml = new Regex("<[^>]*>", RegexOptions.Compiled); private static string StripHtml(string html) { return string.IsNullOrWhiteSpace(html) ? string.Empty : RegexStripHtml.Replace(html, string.Empty).Trim(); } private static string CleanContent(string content, bool removeHtml) { if (removeHtml) { content = StripHtml(content); } content = content.Replace("\\", string.Empty). Replace("|", string.Empty). Replace("(", string.Empty). Replace(")", string.Empty). Replace("[", string.Empty). Replace("]", string.Empty). Replace("*", string.Empty). Replace("?", string.Empty). Replace("}", string.Empty). Replace("{", string.Empty). Replace("^", string.Empty). Replace("+", string.Empty); var words = content.Split(new[] { ' ', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); var sb = new StringBuilder(); foreach (var word in words.Select(t => t.ToLowerInvariant().Trim()).Where(word => word.Length > 1)) { sb.AppendFormat("{0} ", word); } return sb.ToString(); }
:
public static IEnumerable<User> Search(string searchString, IQueryable<User> source) { var term = CleanContent(searchString.ToLowerInvariant().Trim(), false); var terms = term.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); var regex = string.Format(CultureInfo.InvariantCulture, "({0})", string.Join("|", terms)); foreach (var entry in source) { var rank = 0; if (!string.IsNullOrWhiteSpace(entry.Email)) { rank += Regex.Matches(entry.Email.ToLowerInvariant(), regex).Count; } if (rank > 0) { yield return entry; } } }
. regex . , Email .
:
, , «cher [2]», , «cher 2». regex = (cher|2). , IQueryable
, IEnumerable - yield return entry