ASP.NET MVC #8
نویسنده: وحید نصیری
تاریخ: ۱۳۹۱/۰۱/۱۳ ۲۲:۱۰:۰۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
using System;
using System.Web.Mvc;
namespace MvcApplication4.Helpers
{
public static class LabelExtensions
{
public static string MyLabel(this HtmlHelper helper, string target, string text)
{
return string.Format("<label for='{0}'>{1}</label>", target, text);
}
}
}
@using MvcApplication4.Helpers
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@Html.MyLabel("firstName", "First Name:")
Index
<label for='firstName'>First Name:</label>
using System.Web.Mvc;
namespace MvcApplication4.Helpers
{
public static class LabelExtensions
{
public static MvcHtmlString MyLabel(this HtmlHelper helper, string target, string text)
{
return MvcHtmlString.Create(string.Format("<label for='{0}'>{1}</label>", target, text));
}
}
}
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="MvcApplication4.Helpers"/>
</namespaces>
using System.Web.Mvc;
namespace MvcApplication4.Helpers
{
public static class LabelExtensions
{
public static MvcHtmlString MyNewLabel(this HtmlHelper helper, string target, string text)
{
var labelTag = new TagBuilder("label");
labelTag.MergeAttribute("for", target);
labelTag.InnerHtml = text;
return MvcHtmlString.Create(labelTag.ToString());
}
}
}
@using MvcApplication4.Models
@helper GetProductsList(List<Product> products)
{
<ul>
@foreach (var item in products)
{
<li>@item.Name ($@item.Price)</li>
}
</ul>
}
@model List<MvcApplication4.Models.Product>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@ProductsList.GetProductsList(@Model)
مگه این به معنی ایجاد یک رشتهی encoded نیست؟
علامت "-" قبل از encoded به چه معنی است؟
<%: %>
public IHtmlString Raw(string value)
{
return new HtmlString(value);
}
public IHtmlString Raw(object value)
{
return new HtmlString(value == null ? null : value.ToString());
}
namespace System.Web.Mvc
{
public sealed class MvcHtmlString : HtmlString
{
[SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification = "MvcHtmlString is immutable")]
public static readonly MvcHtmlString Empty = Create(String.Empty);
private readonly string _value;
public MvcHtmlString(string value)
: base(value ?? String.Empty)
{
_value = value ?? String.Empty;
}
public static MvcHtmlString Create(string value)
{
return new MvcHtmlString(value);
}
public static bool IsNullOrEmpty(MvcHtmlString value)
{
return (value == null || value._value.Length == 0);
}
}
}
@model List<MvcApplication4.Models.Product>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@ProductsList.GetProductsList(List<MvcApplication4.Models.Product>)
یا
@ProductsList.GetProductsList(MvcApplication4.Models.Products)
ProductsList.GetProductsList(List<MvcApplication4.Models.Product>)
public static void GetProductsList(List<Product> list)
{
// ...
}
ClassName.GetProductsList(...instance...)
public ActionResult Index()
{
var products = new Products();
return View(products);
} return view(products)
var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
var url = urlHelper.Action("Home", "Index"); UrlHelper.GenerateUrl(null, actionName, controllerName,
null, null, null, routeValues, htmlHelper.RouteCollection,
htmlHelper.ViewContext.RequestContext, true); @using WebApplication1.Models
@helper GetProductsList(List<Product> products)
{
<ul>
@foreach (var item in products)
{
<li>
@System.Web.Mvc.Html.LinkExtensions.ActionLink(
((System.Web.Mvc.WebViewPage)CurrentPage).Html,
item.Name,
"ActionName",
"ControllerName",
new { id = item.ProductNumber },
null)
</li>
}
</ul>
} Html.ActionLink("foo") ActionLink(Html, "foo")
@Html.TextBoxFor(m => m.LastName, new { @class = "class-name", placeholder = "last name" })
دلیل Null بودن رو متوجه نمیشم؟
@HTML5Controls.Controls.Lists.DrawList(...) //==== @HTML.DrawList(...)
public static MvcHtmlString MyImage2(this HtmlHelper helper, string src, string alt, int width, int height)
{
var img=new TagBuilder("img");
img.MergeAttribute("src", VirtualPathUtility.ToAbsolute(src));
img.MergeAttribute("alt", alt);
img.MergeAttribute("width", width.ToString());
img.MergeAttribute("height", height.ToString());
return MvcHtmlString.Create(img.ToString());
} var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
var url = urlHelper.Content("~/stuff/");