public static MvcHtmlString EmployeeFor <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression, string searchUrl, IDictionary <string, object> htmlAttributes) { string buildstamp = Inl.MvcHelper.BsHelper.GetBuildStamp().ToString(); // Register the dependency BsHelper.AddDependency(Dependency.TypeAhead); BsHelper.AddCssDependency($" <link href='~/EmployeeHelper/Css/Employee.helper.css?s={buildstamp}' rel='stylesheet' />"); BsHelper.AddJsDependency($" <script src='~/EmployeeHelper/Script/employee.helper.js?s={buildstamp}'></script>"); // make sure that htmlAttributes is not null, or we won't be able to add it if (htmlAttributes == null) { htmlAttributes = new Dictionary <string, object>(); } // get the field name for use with bootstrap ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData); string htmlFieldName = ExpressionHelper.GetExpressionText(expression); string labelText = metadata.DisplayName; if (labelText == null) { labelText = (metadata.PropertyName ?? htmlFieldName.Split('.').Last()).SplitCamelCase(); } // add a placeholder if it isn't already there if (!String.IsNullOrEmpty(labelText)) { if (!htmlAttributes.ContainsKey("placeholder")) { htmlAttributes["placeholder"] = labelText; } } // add form-control if (htmlAttributes.ContainsKey("class")) { htmlAttributes["class"] += " form-control"; } else { htmlAttributes["class"] = "form-control"; } string htmlId = GenerateIdFromName(htmlFieldName); string searchBox = htmlHelper.TextBoxFor(expression, htmlAttributes).ToString(); searchBox = searchBox.Replace(htmlFieldName, htmlFieldName + "_search"); searchBox = searchBox.Replace($"placeholder=\"{htmlFieldName}_search\"", $"placeholder=\"{htmlFieldName}\""); if (htmlFieldName != htmlId) { searchBox = searchBox.Replace(htmlId, htmlId + "_search"); } string hiddenField = htmlHelper.HiddenFor(expression).ToString(); string employees = "<span class=\"employee-badge\">Employee</span>"; string extras = $@"<script>$(function() {{ employeeInit('{searchUrl}', '{htmlId}'); }})</script>"; return(new MvcHtmlString("<div class=\"employee-wrap\">" + searchBox + hiddenField + employees + extras + "</div>")); }
public static MvcHtmlString EmployeesFor <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression, string searchUrl, string getUrl, IDictionary <string, object> htmlAttributes) { string buildstamp = Inl.MvcHelper.BsHelper.GetBuildStamp().ToString(); // Register the dependency BsHelper.AddDependency(Dependency.TypeAhead); BsHelper.AddCssDependency($" <link href='~/EmployeeHelper/Css/Employees.helper.css?s={buildstamp}' rel='stylesheet' />"); BsHelper.AddJsDependency($" <script src='~/EmployeeHelper/Script/employees.helper.js?s={buildstamp}'></script>"); // make sure that htmlAttributes is not null, or we won't be able to add it if (htmlAttributes == null) { htmlAttributes = new Dictionary <string, object>(); } // get the field name for use with bootstrap ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData); string htmlFieldName = ExpressionHelper.GetExpressionText(expression); string labelText = metadata.DisplayName; if (labelText == null) { labelText = (metadata.PropertyName ?? htmlFieldName.Split('.').Last()).SplitCamelCase(); } // add a placeholder if it isn't already there if (!String.IsNullOrEmpty(labelText)) { if (!htmlAttributes.ContainsKey("placeholder")) { htmlAttributes["placeholder"] = "Search Employees to Add..."; } } // add form-control if (htmlAttributes.ContainsKey("class")) { htmlAttributes["class"] += " form-control"; } else { htmlAttributes["class"] = "form-control"; } // DS: Since we always need a List<string> to check contains, we need to convert whatever we have List <string> values = new List <string>(); if (metadata.Model == null) { // do nothing } else if (metadata.ModelType == typeof(IEnumerable <SelectListItem>)) { values = ((IEnumerable <SelectListItem>)metadata.Model).Select(x => x.Value).ToList(); } else if (typeof(IEnumerable).IsAssignableFrom(metadata.ModelType)) { IEnumerable enumerable = (IEnumerable)metadata.Model; foreach (object item in enumerable) { values.Add(item.ToString()); } } string searchBox = htmlHelper.TextBox(htmlFieldName + "_search", string.Join(",", values), htmlAttributes).ToString(); string employeeBadge = "<span class=\"employee-badge\">Employees</span>"; string extras = $@"<script>$(function() {{ employeesInit('{searchUrl}', '{getUrl}', '{GenerateIdFromName(htmlFieldName)}', '{htmlFieldName}'); }})</script>"; return(new MvcHtmlString("<div class='employee-wrap'>" + searchBox + employeeBadge + "</div>" + extras)); }
public static MvcHtmlString ValidationErrorFor <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression, string error) { return(htmlHelper.HasError(ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData), ExpressionHelper.GetExpressionText(expression)) ? new MvcHtmlString(error) : null); }
public static MvcHtmlString UploaderFor <TModel, TValue>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TValue> > expression, IDictionary <string, object> attributes = null) { var data = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData); string propertyName = data.PropertyName; var Url = new UrlHelper(htmlHelper.ViewContext.RequestContext); string controller = htmlHelper.ViewContext.RouteData.Values["controller"].ToString().ToLower(); #region Default Attributes string mainFilesPath = ConfigurationManager.AppSettings["MainFilesPath"].ToString(); string uploaderDefaultText = ConfigurationManager.AppSettings["UploaderDefaultText"].ToString(); string uploaderDefaultNote = ConfigurationManager.AppSettings["UploaderDefaultNote"].ToString(); string uploadTemplateId = ConfigurationManager.AppSettings["UploadTemplateId"].ToString(); string downloadTemplateId = ConfigurationManager.AppSettings["DownloadTemplateId"].ToString(); string directory = (ConfigurationManager.AppSettings[controller + "Directory"] ?? controller).ToString(); string allowedExtensions = (ConfigurationManager.AppSettings[controller + "AllowedExtensions"] ?? ConfigurationManager.AppSettings["AllowedExtensions"]).ToString(); int maxFileSize = Convert.ToInt32(ConfigurationManager.AppSettings[controller + "MaxFileSize"] ?? ConfigurationManager.AppSettings["MaxFileSize"]); string maxNumberOfFiles = (ConfigurationManager.AppSettings[controller + "MaxNumberOfFiles"] ?? ConfigurationManager.AppSettings["MaxNumberOfFiles"]).ToString(); #endregion #region Attributes if (attributes != null && attributes.Any()) { foreach (var item in attributes) { if (item.Key.ToLower() == "uploadtemplateid") { uploadTemplateId = item.Value.ToString(); } if (item.Key.ToLower() == "downloadtemplateid") { downloadTemplateId = item.Value.ToString(); } if (item.Key.ToLower() == "uploaderdefaultnote") { uploaderDefaultText = item.Value.ToString(); } if (item.Key.ToLower() == "uploaderdefaulttext") { uploaderDefaultNote = item.Value.ToString(); } if (item.Key.ToLower() == "mainfilespath") { mainFilesPath = item.Value.ToString(); } if (item.Key.ToLower() == "directory") { directory = item.Value.ToString(); } if (item.Key.ToLower() == "allowedextensions") { allowedExtensions = item.Value.ToString(); } if (item.Key.ToLower() == "maxfilesize") { maxFileSize = Convert.ToInt32(item.Value.ToString()); } if (item.Key.ToLower() == "maxnumberoffiles") { maxNumberOfFiles = item.Value.ToString(); } } } #endregion bool isInEditMode = false; var existingFiles = "var files = ["; if (data.Model != null) { isInEditMode = true; var arrayOfFiles = (data.Model + "").Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries); foreach (var item in arrayOfFiles) { var fullrelativePath = Url.Content(mainFilesPath + directory + "/" + item); if (File.Exists(HttpContext.Current.Server.MapPath(fullrelativePath))) { var fileVar = new FileInfo(HttpContext.Current.Server.MapPath(fullrelativePath)); existingFiles += (existingFiles == "var files = [" ? "" : ",") + @"{ 'name': '" + item + @"', 'prettyName': '" + item.Split('~')[1] + @"', 'size': " + fileVar.Length + @", 'type': '" + fileVar.GetType() + @"', 'url': '" + fullrelativePath + @"', 'deleteUrl': '/UploadHandler/Delete?id=" + HttpUtility.UrlEncode(fullrelativePath) + @"', 'thumbnailUrl': '" + fullrelativePath + @"?width=48&height=48&mode=max', 'deleteType': 'DELETE', 'inputName': '" + propertyName + @"' }"; } ; } existingFiles += "];"; } MvcHtmlString result = new MvcHtmlString("<div id='fileupload_" + propertyName + @"' class='row fileupload-buttonbar'> <div class='col-lg-9'> <span class='btn btn-success fileinput-button green' style='width: 300px;'> <i class='glyphicon glyphicon-plus'></i> <span>" + uploaderDefaultText + @"</span> <input type='file' multiple/> <input type='hidden' id='" + propertyName + "' name='" + propertyName + "' value='" + data.Model + @"'/> </span>" + uploaderDefaultNote + @" </div> <table role='presentation' class='table table-striped'><tbody class='files'></tbody></table> </div> <script> $(function(){ 'use strict'; $('#fileupload_" + propertyName + @"').fileupload({ uploadTemplateId: '" + uploadTemplateId + @"', downloadTemplateId: '" + downloadTemplateId + @"', autoUpload: true, maxNumberOfFiles: " + maxNumberOfFiles + @", maxFileSize: " + maxFileSize + @", acceptFileTypes: /(\.|\/)(" + allowedExtensions + @")$/i, url: '" + Url.Content("~/UploadHandler/UploadFiles?inputname=" + propertyName + "&directory=" + mainFilesPath + directory) + @"' }); " + (isInEditMode ? existingFiles + "$('#fileupload_" + propertyName + @"').fileupload('option', 'done').call($('#fileupload_" + propertyName + @"'), $.Event('done'), {result: {files: files},editMode:true});" : "") + @"}); </script> "); return(result); }
public static MvcHtmlString LabelForRequired <TModel, TValue>(this HtmlHelper <TModel> html, System.Linq.Expressions.Expression <Func <TModel, TValue> > expression, string labelText = "") { return(LabelHelper(html, ModelMetadata.FromLambdaExpression(expression, html.ViewData), ExpressionHelper.GetExpressionText(expression), labelText)); }