Created
July 13, 2018 12:55
-
-
Save ahmeturganci/01459a20f3f29d1a99b876ff35d4cc5d to your computer and use it in GitHub Desktop.
Custom razor component example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Web; | |
| using System.Web.Mvc; | |
| namespace ComponentDemo.Helper | |
| { | |
| public static class HtmlHelperExtensions | |
| { | |
| public static MvcHtmlString AUTextbox(this HtmlHelper helper,string name) | |
| { | |
| return AUTextbox(helper, name, null, null); | |
| } | |
| private static MvcHtmlString AUTextbox(HtmlHelper helper, string name, object ID/*ID*/, object text) | |
| { | |
| return AUTextbox(helper, ("txt" + name + ID.ToString()), text, null); | |
| } | |
| /// <summary> | |
| /// | |
| /// </summary> | |
| /// <param name="helper"></param> | |
| /// <param name="type"></param> | |
| /// <param name="name">ID</param> | |
| /// <param name="text">Value</param> | |
| /// <param name="placeholder">placeholder</param> | |
| /// <param name="htmlAttributes"></param> | |
| /// <returns></returns> | |
| public static MvcHtmlString AUTextbox(this HtmlHelper helper,string type,string name,string text,string placeholder,string styleclass,object htmlAttributes) | |
| { | |
| TagBuilder textbox = new TagBuilder("input"); | |
| textbox.Attributes.Add("type", type); | |
| textbox.Attributes.Add("name", name); | |
| textbox.Attributes.Add("placeholder", placeholder); | |
| textbox.Attributes.Add("id", name); | |
| textbox.Attributes.Add("class", styleclass); | |
| if (!string.IsNullOrEmpty(text)) | |
| { | |
| textbox.Attributes.Add("value", text); | |
| } | |
| textbox.Attributes.Add("onblur", "javascript:if(this.value==''){this.style.backgroundColor = 'yellow';this.focus();};"); | |
| textbox.Attributes.Add("onkeyup", "javascript:if(this.value!=''){this.style.backgroundColor = 'white';}else{this.style.backgroundColor = 'yellow';};"); | |
| textbox.MergeAttributes(new System.Web.Routing.RouteValueDictionary(htmlAttributes)); | |
| return MvcHtmlString.Create(textbox.ToString(TagRenderMode.Normal)); | |
| } | |
| public static MvcHtmlString AURadioButton(this HtmlHelper helper, string name) | |
| { | |
| return AURadioButton(helper, name, true, true, null); | |
| } | |
| public static MvcHtmlString AURadioButton(this HtmlHelper helper, int ID, bool value, bool isChechked) | |
| { | |
| return AURadioButton(helper, ("rd" + ID.ToString()), value, isChechked, null); | |
| } | |
| public static MvcHtmlString AURadioButton(this HtmlHelper helper, string name, bool value, bool isChechked, object htmlAttributes) | |
| { | |
| TagBuilder radioButton = new TagBuilder("input"); | |
| radioButton.Attributes.Add("type", "radio"); | |
| radioButton.Attributes.Add("name", name); | |
| radioButton.Attributes.Add("id", name); | |
| if (isChechked) { radioButton.Attributes.Add("checked", "checked"); } | |
| radioButton.Attributes.Add("value", value.ToString()); | |
| radioButton.MergeAttributes(new System.Web.Routing.RouteValueDictionary(htmlAttributes)); | |
| return MvcHtmlString.Create(radioButton.ToString(TagRenderMode.Normal)); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @using ComponentDemo.Helper; | |
| @model List<ComponentDemo.Models.Activities> | |
| <table style="width:100%"> | |
| <thead> | |
| <th class="hasid"></th> | |
| <th>OKEY</th> | |
| <th>NO</th> | |
| <th>NAME</th> | |
| <th>SUMMARY</th> | |
| <th>LOCATION</th> | |
| <th>TIME</th> | |
| </thead> | |
| @{ var count = 0;} | |
| @foreach (var item in Model) | |
| { | |
| count++; | |
| <tr> | |
| <td class="hasid">@Html.Hidden("hdn" + @count, item.ID, new { @class = "hdnData" })</td> | |
| @*<td>@Html.RadioButton("rd" + item.ID, true, item.IsAllow)</td>*@ | |
| <td>@Html.AURadioButton(item.ID, true, item.IsAllow)</td> | |
| <td>@Html.AURadioButton(item.ID, false, !item.IsAllow)</td> | |
| @*<td>@Html.RadioButton("rd" + item.ID, false, !item.IsAllow)</td>*@ | |
| <td>@item.Name</td> | |
| <td>@item.Summary</td> | |
| <td>@item.Location</td> | |
| <td>@item.Time</td> | |
| </tr> | |
| } | |
| </table><br /> | |
| <form action="/" method="post"> | |
| @Html.Label("", "Name") | |
| @Html.AUTextbox("text", "Name", "", "Activty Name","form-control","") | |
| @Html.Label("", "Summary") | |
| @Html.AUTextbox("text", "Summary", "", "Activty Summary", "form-control", "") | |
| @Html.Label("", "Location") | |
| @Html.AUTextbox("text", "Location", "", "Activty Location", "form-control", "") | |
| @Html.Label("", "Time") | |
| @Html.AUTextbox("numberq", "Time", "", "Activty Time", "form-control", "") | |
| </form> | |
| <input type="button" onclick="save();" value="Save" /> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment