How to use Html.FieldIdFor in Asp.Net Mvc.(@Html.FieldIdFor(r=r.Id)).
1> Open a visual studio add new Project.
2>Create a class file name it HtmlExtension.cs and we write a logic for Html helper Extension.
3> File will create in Solution Explore of the project.
4> Now we will write a logic for HtmlExtension to get Id.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
namespace ItsMe
{
public static class HtmlExtension
{
public static string FieldIdFor<T, TResult>(this HtmlHelper<T> html, Expression<Func<T, TResult>> expression)
{
var id = html.ViewData.TemplateInfo.GetFullHtmlFieldId(ExpressionHelper.GetExpressionText(expression));
return id.Replace('[', '_').Replace(']', '_');
}
public static string FieldNameFor<T, TResult>(this HtmlHelper<T> html, Expression<Func<T, TResult>> expression)
{
return html.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression));
}
}
}
5> Name space required for Html helper
using System.Linq.Expressions
using System.Web.Mvc
6> Save it
7> Create a View to Test the Html helper Attribute is visible or not
8> Create Controller name sampleController , Index Action will generate
In Index action --> right click of the mouse -> Add view option will visible,
View name:- Index -> select templete as a create -> Index page will autogenerate
Html Tags.
9> Now write @Html.FieldIdFor -> AI(Artificial Intelligence) will prepare to fire the
query. Look at an Image.
1> Open a visual studio add new Project.
2>Create a class file name it HtmlExtension.cs and we write a logic for Html helper Extension.
3> File will create in Solution Explore of the project.
4> Now we will write a logic for HtmlExtension to get Id.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
namespace ItsMe
{
public static class HtmlExtension
{
public static string FieldIdFor<T, TResult>(this HtmlHelper<T> html, Expression<Func<T, TResult>> expression)
{
var id = html.ViewData.TemplateInfo.GetFullHtmlFieldId(ExpressionHelper.GetExpressionText(expression));
return id.Replace('[', '_').Replace(']', '_');
}
public static string FieldNameFor<T, TResult>(this HtmlHelper<T> html, Expression<Func<T, TResult>> expression)
{
return html.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression));
}
}
}
5> Name space required for Html helper
using System.Linq.Expressions
using System.Web.Mvc
6> Save it
7> Create a View to Test the Html helper Attribute is visible or not
8> Create Controller name sampleController , Index Action will generate
In Index action --> right click of the mouse -> Add view option will visible,
View name:- Index -> select templete as a create -> Index page will autogenerate
Html Tags.
9> Now write @Html.FieldIdFor -> AI(Artificial Intelligence) will prepare to fire the
query. Look at an Image.