Building HTML Twitter Bootstrap 3 in code - c#

.NET 4.51 Webforms
I an writing some user controls and need to generate some HTML for Twitter Bootstrap 3. I was wondering if anyone had come across a class that would assist with this before I roll my own?
There seems to be an abundance of HTML helpers for MVC https://www.twitterbootstrapmvc.com/ but nothing where I can generate HTML from code behind easily. Yes I can use something like HTML helpers in Webform? or http://msdn.microsoft.com/en-us/library/system.web.mvc.htmlhelper_methods(v=vs.118).aspx or http://msdn.microsoft.com/en-us/library/system.web.mvc.tagbuilder(v=vs.111).aspx but if there is something out there already then I would rather use that than rolling my own.
Has anyone come across anything like this?

If your're talking about textboxes, textareas and so on, the best way to do it is by making a function that adds your custom html attributes to that element.
After that you should do method which returns MvcHtmlString
public static MvcHtmlString CustomTextBox(this HtmlHelper helper.....)
{
var attributes= customFunctionsToSetAttributes // Dictionary<string,object>
helper.TextBox(name, value, format, attributes); // mvc original .TextBox helper
}

Related

Get content block without HTML

Is there a built-in Sitefinity method for displaying blog post content without the HTML? NOT the standard standalone C# methodology, but a built-in method in Sitefinity so I can avoid duplicating functionality.
A built in method in Sitefinity, not a generic C# solution.
I'd like to show a summary from my content rather than the separate summary field. I'm in my custom blog list .cshtml file, doing this to retrieve content:
Html.Raw(item.Fields.Content).ToString()
Which gives me the content, but I need to display with without the HTML tags a user may have added.
If you refer using Telerik.Sitefinity.Utilities you will get an extension method StripHtmlTags for the string type.
This is a public extension method and you should not have problems using it.
I see there are a few classes in Sitefinity that do HTML stripping:
public static HtmlStripper in Telerik.Sitefinity.Modules.Newsletters.Composition namespace.
and
internal static class HtmlStripper in Telerik.Sitefinity.Utilities.HtmlParsing namespace, which seems to be the one you need, because it has a StripHtml method, but you cannot use it as it is internal.
I agree with #Zohar that in this particular case you should just use a standard C# approach - you cannot go wrong with it.

How to create custom validation helper that change the corresponsing field value in MVC 4?

I'm starting to write my own field validation helper, no matter the reason.
I want to control the value in the textbox being validated and not only render a validation message.
Is there a way to access the html control being validated from my custom extension code?
Here is a very basic beginning:
(Please don't judge the code, i simplified it for the sake of the question)
public static MvcHtmlString CustomValidatioMessageFor<TModel,
TProperty>(this HtmlHelper obj, Expression<Func<TModel,TProperty>>
expression){
string html = (string)obj.ValidationMessageFor(expression);
html = "<div>" + /* Here goes the error message blabla... */ + "</div>";
return new MvcHtmlString(html);
}
I want to control the value in the textbox being validated
Validation helpers are designed to validate the data once it is input but you need to constraint the data while it is being input. This means that you cannot achieve it using validation helpers; you need a more eager approach based on hooking into keyboard/mouse events. Also, it will be completely javascript based as on server side you don't have text boxes; you only have posted models.
The ASP.net MVC framework uses jquery-validation to perform validation client side prior to errors being passed back to the server. If a Validation helper's code has been hit, the data has already been submitted off the client, and the only thing the server can do is accept or deny the processing of the data.
You can write your own handlers for fields and define different rules, see the source for jquery validation demo for multiple examples of different ways to take action on your form fields.

ModelState.AddModelError on form level with ASP.NET MVC 2 RC

In the previous versions on ASP.NET MVC, you could use ModelState.AddModelError("**_FORM**", "error") to make an error on the form-level instead of a property-level. This seems not to work anymore with the new RC of ASP.NET MVC 2.
Is there another way how to do it? I use <%= Html.ValidationSummary() %> at my View-file...
I personally use AddModelError("" - I don't know where does _FORM come from? - and ValidationSummary() is no black magic - I use my own helper instead that, for example, includes specific (user-specific) exceptions messages into output. The helper is very easy to write - a simple foreach loop over ModelState - so you may consider writing your own one. There you're free to show form-level, property-level, or whatever you like.
the error are inside modelstate and is send to the helper class of validationsummary by following argument:
this.ViewData.ModelState.Keys
this.ViewData.ModelState.Values

ASP.NET MVC: Get lowercase links (instead of Camel Case)

All my dynamically generated action links etc. are creating links like /Account/Setup. It looks strange.
I'd like all my links to be lowercase (meaning /account/setup). Any way to do this?
There is simpler solution in .NET Framework 4.5, a new property RouteCollection.LowercaseUrls, here's an example
Take a look at http://goneale.com/2008/12/19/lowercase-route-urls-in-aspnet-mvc/. You can find more information in another stackoverflow at How can I have lowercase routes in ASP.NET MVC?.
The other posts thus far have not tackled the scenario where you navigate to the root of your web directory. If you have a mapping that directs to the HomeController Index action, you would like the following URL to appear:
mysite/home/ or even mysite/home/index
No amount of Html helper function usage will change the fact that, by default, the following will be shown in the browser location bar:
mysite/Home or mysite/Home/Index
Write an extension method for Html:
public static class MyHtmlExtensions
{
public static string LowerActionLink(this HtmlHelper htmlHelper,someargs)
{
return String.ToLowerInvariant(htmlHelper.ActionLink(someArgs));
}
}
Then use Html.LowerActionLink instead of Html.ActionLink

How do I move boolean logic out of my View?

In my ASP.NET MVC View I pick a sprite based on a boolean value set in the model like this:
<div class="sprite-icon_dog<% =(Model.HasNewDog ? "_new" : "") %>"></div>
This is ugly and I don't like it.
My objective is to use the sprite-icon_dog_new if Model.HasNewDog is true and use sprite-icon_dog if Model.HasNewDog is false.
What is a more elegant and more readable way to do this?
I think a HTML Helper would be the way to go?
public static string DogDiv(this HTMLHelper html, bool HasDog)
{
return "...."
}
In your view:
<%=Html.DogDiv(Model.HasDog) %>
Hope that helps,
Dan
It's ugly, but it may be the best option. If you move the logic to an html helper you are now blurring the lines between what is display and what is business logic. If you want to change your markup or css, you will be limited to what you can do unless you change code in the Html helper class.
In my opinion, part of that uglyness is getting used to the MVC model. One of the pillars of MVC is to provide a strict seperation between the display, business logic and data model, but if you start adding display logic in a helper it starts to negate what MVC is intended to do.

Categories