Add Validation to Partial View Loaded using TagHelper
CustomTagHelper:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace KeyHost.CustomTagHelpers
{
using System.Text.Encodings.Web;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
public class TabPageTagHelper: TagHelper
{
private readonly HtmlEncoder _htmlEncoder;
public TabPageTagHelper(HtmlEncoder htmlEncoder)
{
_htmlEncoder = htmlEncoder;
}
[HtmlAttributeName("controller")]
public string Controller { get; set; } = "";
[HtmlAttributeName("hrefs")]
public string Hrefs { get; set; } = "";
[HtmlAttributeName("tab-names")]
public string TabNames { get; set; } ="";
[HtmlAttributeName("default-partial-view")]
public string DefaultPartialView { get; set; } = "";
public override void Process(TagHelperContext context, TagHelperOutput output)
{
var sb = new StringBuilder();
sb.Append("<ul id=\"tabstrip\" class=\"nav nav-tabs nav-justified\" role=\"tablist\">");
int tabCounter = 0;
List<string> hrefsList = Hrefs.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
List<string> tabNamesList = TabNames.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
foreach (string href in hrefsList)
{
if (tabCounter == 0)
{
sb.Append("<li class=\"active\">");
}
else
{
sb.Append("<li>");
}
sb.Append($"<a href=\"#{href}\" role=\"tab\" data-toggle=\"tab\">{tabNamesList[tabCounter]}</a>");
sb.Append("</li>");
tabCounter++;
}
sb.Append("</ul>");
tabCounter = 0;
sb.Append("<div class=\"tab-content\">");
foreach (string href in hrefsList)
{
if (tabCounter == 0)
{
sb.Append($"<div class=\"tab-pane fade in active\" id=\"{href}\">");
sb.Append($"<partial name=\"{DefaultPartialView}\" />");
}
else
{
sb.Append($"<div class=\"tab-pane fade\" id=\"{href}\">");
}
sb.Append("</div>");
tabCounter++;
}
output.Content.SetHtmlContent(sb.ToString());
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace KeyHost.CustomTagHelpers
{
using System.Text.Encodings.Web;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
public class TabPageTagHelper: TagHelper
{
private readonly HtmlEncoder _htmlEncoder;
public TabPageTagHelper(HtmlEncoder htmlEncoder)
{
_htmlEncoder = htmlEncoder;
}
[HtmlAttributeName("controller")]
public string Controller { get; set; } = "";
[HtmlAttributeName("hrefs")]
public string Hrefs { get; set; } = "";
[HtmlAttributeName("tab-names")]
public string TabNames { get; set; } ="";
[HtmlAttributeName("default-partial-view")]
public string DefaultPartialView { get; set; } = "";
public override void Process(TagHelperContext context, TagHelperOutput output)
{
var sb = new StringBuilder();
sb.Append("<ul id=\"tabstrip\" class=\"nav nav-tabs nav-justified\" role=\"tablist\">");
int tabCounter = 0;
List<string> hrefsList = Hrefs.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
List<string> tabNamesList = TabNames.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
foreach (string href in hrefsList)
{
if (tabCounter == 0)
{
sb.Append("<li class=\"active\">");
}
else
{
sb.Append("<li>");
}
sb.Append($"<a href=\"#{href}\" role=\"tab\" data-toggle=\"tab\">{tabNamesList[tabCounter]}</a>");
sb.Append("</li>");
tabCounter++;
}
sb.Append("</ul>");
tabCounter = 0;
sb.Append("<div class=\"tab-content\">");
foreach (string href in hrefsList)
{
if (tabCounter == 0)
{
sb.Append($"<div class=\"tab-pane fade in active\" id=\"{href}\">");
sb.Append($"<partial name=\"{DefaultPartialView}\" />");
}
else
{
sb.Append($"<div class=\"tab-pane fade\" id=\"{href}\">");
}
sb.Append("</div>");
tabCounter++;
}
output.Content.SetHtmlContent(sb.ToString());
}
}
}
View:
@{
ViewData["Title"] = "Environments";
}
<h1>@ViewData["Title"]</h1>
<tab-page hrefs="AddEnvironment,ListEnvironments" tab-names="Add,Search" default-partial-view="_AddEnvironment"></tab-page>
<script>
function LoadTab(tabID) {
$(".tab-pane").each(function () {
$(this).empty();
});
$.ajax({
url: "/@ViewContext.RouteData.Values["controller"]/" + tabID,
cache: false,
type: "get",
dataType: "html",
success: function (result) {
$("#" + tabID).html(result);
if (tabID == "AddEnvironment") {
var currForm = $('#addEnvironmentForm');
currForm.removeData("validator");
currForm.removeData("unobtrusiveValidation");
$.validator.unobtrusive.parse(currForm);
currForm.validate();
}
}
})
}
$('#tabstrip a').click(function (e) {
e.preventDefault()
LoadTab($(this).attr("href").substr(1));
$(this).tab('show')
});
$(document).ready(function () {
LoadTab("AddEnvironment");
$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
if ($(e.target)[0].hash.substr(1) == "ListEnvironments") {
$("#environments").DataTable(
{
visible: true,
api: true,
"processing": true, // for show progress bar
"serverSide": true, // for process server side
"filter": true, // this is for disable filter (search box)
"orderMulti": false, // for disable multiple column at once
"ajax": {
"url": "/Settings/LoadEnvironments",
"type": "POST",
"datatype": "json"
},
"columnDefs":
[{
"targets": [0],
"visible": false,
"searchable": false
}],
"columns": [
{ "data": "environmentId", "name": "EnvironmentId", "autoWidth": true },
{ "data": "name", "name": "Name", "autoWidth": true }
]
}).columns.adjust();
}
});
});
</script>
Comments
Post a Comment