For something like code-re-using logic. you can create a action method and its view. Then you can reference or load this view from any of the view and you reach your target.
Once in my project, I created an area named "Common" and then had added an action in the controller and created its related view. I, then loaded this partial view in the entire project. This way, I had seperated my reusable (something like usercontrols in
asp.net) and used them.
That was one way to do it. I am using Razor syntax and I was able to do it another way. Using the helper class, wich I already had created. Taking the bool value was very easy.
public static MvcHtmlString TestValue(this HtmlHelper html, bool? input )
{
//Checks to see if the input has a value
if (input.HasValue)
{
//validates the input against a condition
if (input.Value == true)
{
//meets condition
return MvcHtmlString.Create("Yes");
}
else
{
//does not meet condition
return MvcHtmlString.Create("No");
}
}
//value is returned if there is a null value
return MvcHtmlString.Create("No");
}
Here is the code that is in the cshtml.
@Html.TestValue(item.BoardMember)
It's all about the points... If I helped mark my response as answered.
Marked as answer by rollo1002 on Apr 09, 2012 09:18 AM
In addition to the others, you can declare a helper method either via an extension method (in a class) or in a razor file as a declerative helper. Declerative helpers let's you use razor syntax when creating your Html helper method.
rollo1002
Contributor
2447 Points
452 Posts
Create HTML Helper for repeated code
Apr 07, 2012 07:00 AM|LINK
How would one go about creating a helper class for repeated code. I want to display depending upon the Model items value..
@foreach (var item in Model) { <tr> <td> @if (item.Officer.HasValue) { if (item.Officer.Value == true) { <text>Yes</text> } else { <text>No</text> } } </td>ignatandrei
All-Star
134535 Points
21583 Posts
Moderator
MVP
Re: Create HTML Helper for repeated code
Apr 07, 2012 07:06 AM|LINK
TechFriend
Participant
955 Points
182 Posts
Re: Create HTML Helper for repeated code
Apr 07, 2012 04:24 PM|LINK
For something like code-re-using logic. you can create a action method and its view. Then you can reference or load this view from any of the view and you reach your target.
Once in my project, I created an area named "Common" and then had added an action in the controller and created its related view. I, then loaded this partial view in the entire project. This way, I had seperated my reusable (something like usercontrols in asp.net) and used them.
rollo1002
Contributor
2447 Points
452 Posts
Re: Create HTML Helper for repeated code
Apr 09, 2012 09:18 AM|LINK
That was one way to do it. I am using Razor syntax and I was able to do it another way. Using the helper class, wich I already had created. Taking the bool value was very easy.
public static MvcHtmlString TestValue(this HtmlHelper html, bool? input ) { //Checks to see if the input has a value if (input.HasValue) { //validates the input against a condition if (input.Value == true) { //meets condition return MvcHtmlString.Create("Yes"); } else { //does not meet condition return MvcHtmlString.Create("No"); } } //value is returned if there is a null value return MvcHtmlString.Create("No"); }Here is the code that is in the cshtml.
CodeHobo
All-Star
18647 Points
2647 Posts
Re: Create HTML Helper for repeated code
Apr 09, 2012 03:20 PM|LINK
In addition to the others, you can declare a helper method either via an extension method (in a class) or in a razor file as a declerative helper. Declerative helpers let's you use razor syntax when creating your Html helper method.
For example:
@{ ViewBag.Title = "Home Page"; } @helper TruncateString(string input, int length) { if (input.Length <= length) { @input } else { @input.Substring(0, length)<text>...</text> } }Checkout the following blog post:
http://weblogs.asp.net/jgalloway/archive/2011/03/23/comparing-mvc-3-helpers-using-extension-methods-and-declarative-razor-helper.aspx
Blog | Twitter : @Hattan