I am looking for a text editor for my asp.net mvc project. I was able to find a lot of text editor out there works pretty well.
What I am looking for is a editor that accept only regualr character like "I am a superman".
I do not want to save "<p><strong>I am a superman</strong></p>" into my SQL table since I have to show it thru textbox(example : <%= Html.TextBox("Remark", Model.empExperience.Remark)%>).
Let me know.
Seeing as you do not wish to allow HTML, your best bet is to simply have a means of stripping HTML from the provided input. There's no need to implement a custom text editor for this sort of thing.
Have a look at: How can I strip HTML tags from a string in ASP.NET?
This is how you do it (to sum up the answers on the link Nathan provided):
private static readonly Regex StripHtml = new Regex("<[^>]*>", RegexOptions.Compiled);
/// <summary>
/// Strips all HTML tags from the specified string.
/// </summary>
/// <param name = "html">The string containing HTML</param>
/// <returns>A string without HTML tags</returns>
public static string StripHtmlTags(string html)
{
if (string.IsNullOrEmpty(html))
return string.Empty;
return StripHtml.Replace(html, string.Empty);
}
Related
guys:
I use swagger to make api document. I use ASP.NET WebAPI2 to develop WebAPI.
And I met three questions:
First: How could I Add Comments for the WebAPI Controller? I try to add Comment on Controller
namespace IMCAPI.Controllers
{
/// <summary>
/// Value API
/// </summary>
[Authorize]
public class ValuesController : ApiController
{
// GET api/values
/// <summary>
/// Get all Values
/// </summary>
/// <returns></returns>
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
, but not work.
Second:How could I search specific WebAPI in Swagger by brower Url? like this Browser Url Search
For example, if I want to search Value API, I input Value in the red section,I want the search result would just show Value API, I don't want other API appears(like Account). Or how to setting SwaggerConfig.cs that can have the search function?
Third:I want to know whether swagger could just read one xml file? I search the Internet, they demo like this
private static string GetXmlCommentsPath()
{
return String.Format("{0}/App_Data/IMCAPI.XML", AppDomain.CurrentDomain.BaseDirectory);
}
If I have multiple XML file, how could I integrate in the swagger?
that it. Hope somebody can help me. Thank you!
Your first question: The comments are appearing, you've put them on the Get method in your code, they're appearing on the Get method in your screenshot. If what you want is a description of the whole service, the Swagger documentation says to use the Description method.
Your second: You can't it shows all the methods on that API.
Third: The Swagger documentation says you call IncludeXmlComments multiple times with the path to each XML.
I am working on an asp.net mvc 5.2.2. web application using Razor view engine. I am trying to modify the default Html.DisplayFor to show substring of 100 character followed by "..." on certain views ? so is this possible to achieved ?
Thanks
Html.DisplayFor is a templated helper. MVC has some default templates built-in for the most common C# types. For a string, it's pretty much just dumps the string value as-is, though.
You can override the built-ins by adding your own display templates to Views\Shared\DisplayTemplates\. The views you add need to be named after one of three things:
A type (either standard C# types or you own classes), e.g. String.cshtml, DateTime.cshtml, Boolean.cshtml, etc.
A member of the DataType enum, e.g. EmailAddress.cshtml, ImageUrl.cshtml, etc. (this requires that the property be decorated with the appropriate DataType attribute)
Virtual anything you want in conjunction with decorating the property with the UIHint attribute. For example [UIHint("Foo")] would use a display template named Foo.cshtml.
In your case, you could use String.cshtml with contents of something like:
#model String
#Model.Substring(0, 100)…
However, I don't think you really want this to apply to every string rendered on your site, everywhere. Using UIHint with a custom display template is probably a better choices here. Create a display template named something like TruncatedString.cshtml and then decorate any properties you want displayed truncated like this with [UIHint("TruncatedString")].
That's only if you insist on using Html.DisplayFor. Really, it would probably be better to add a string extension like:
public static class StringExtensions
{
public static string Truncate(this string s, int length, string suffix = "...", bool html = false)
{
s = s ?? string.Empty;
s = System.Net.WebUtility.HtmlDecode(s);
if (s.Length > length)
{
s = s.Substring(0, length + 1);
s = s.Substring(0, Math.Min(s.Length, s.LastIndexOf(" ") == -1 ? 0 : s.LastIndexOf(" ")));
s = s + suffix;
}
if (html)
{
return System.Net.WebUtility.HtmlEncode(s.Trim());
}
else
{
return s.Trim();
}
}
}
And then, you can just do the following in your view:
#Model.SomeLongString.Truncate(100)
You can achieve that using Editor Templates and UIHint's:
http://www.codeguru.com/csharp/.net/net_asp/mvc/using-display-templates-and-editor-templates-in-asp.net-mvc.htm
http://www.hanselman.com/blog/ASPNETMVCDisplayTemplateAndEditorTemplatesForEntityFrameworkDbGeographySpatialTypes.aspx
Personally my preference would be to create another read-only property in the model and have that return a truncated value. Probably using the truncate extension mentioned here.
you can user the next sentence:
#Html.DisplayFor(Function(modelItem) item.name_field).ToString().Substring(1, n)
#Html.DisplayFor(modelItem => item.CreateDate).ToString().Substring(0,10)
I am creating AJAX enabled reusable controls in MVC razor code. It uses a Controller and 1 or more Razor views to work. What is the best practice for isolating those code files from the rest of my project? Logically, it does not make sense to me to have the Controller and the Views mixed in the with main Controller and View files I use for the rest of the project.
And what if i wanted to reuse the control?
I would probably make an extension method off HtmlHelper, so you can use it by calling:
#Html.MyControl("blah", "blah")
from within your view. This is how my MarkdownHelper works (though it's not actually a control, it just formats some text). This is also how the built-in stuff tends to work (g. Html.TextBox, etc.):
/// <summary>
/// Helper class for transforming Markdown.
/// </summary>
public static partial class MarkdownHelper
{
/// <summary>
/// Transforms a string of Markdown into HTML.
/// </summary>
/// <param name="helper">HtmlHelper - Not used, but required to make this an extension method.</param>
/// <param name="text">The Markdown that should be transformed.</param>
/// <returns>The HTML representation of the supplied Markdown.</returns>
public static IHtmlString Markdown(this HtmlHelper helper, string text)
{
// Transform the supplied text (Markdown) into HTML.
var markdownTransformer = new Markdown();
string html = markdownTransformer.Transform(text);
// Wrap the html in an MvcHtmlString otherwise it'll be HtmlEncoded and displayed to the user as HTML :(
return new MvcHtmlString(html);
}
}
I have some read-only data I want to display in a View, but it needs to be manipulated by jQuery in the browser.
I could use Html.DisplayTextFor but then I can't manipulate it (at least, I don't think I can.) Oh, and I also want to be able to style this.
What's the best way of doing this?
You could just output it in a div or span, maybe write an extension method on HtmlHelper to do this (untested):
public static string Display(this HtmlHelper html, string value, string cssClass)
{
TagBuilder span = new TagBuilder("span");
span.InnerHtml = value;
span.AddCssClass(cssClass);
return span.ToString();
}
I am working on an ASP.NET MVC based CMS that presents a rather extreme case. The system must allow the user to add custom content types based on different fields, and for every field, one can add options and validations. The thing is that everything is stored in a complex DB and extracted at runtime using LINQ.
I am pretty fresh with ASP>NET MVC so the following dilemma came to mind
How should I make the content creation view so that form helpers are not predefined int he view code but are loaded based on the type of the field ? Do I have to create a factory class that checks the value of the type property of the field, and then returns a helper based on that or there's a better way to do it. This one seems pretty rigid to me , because anytime I make a change in the Fieldtypes table, I will have to make sure to create a check for that new type too.
public class CType {
string Name; //e.g Post Article etc
List<ContentData> data ;
...
}
public class ContentData {
string Data; // Basically this is the data stored for each field
FieldInstance fieldInstance;
...
}
public class FieldInstance {
string Title; // e.g Title Body etc.
FieldType Type ; // e.g textbox textarea image checkbox etc
...
}
public class FieldType {
string Type; // e.g textbox textarea image checkbox etc
...
}
I see an HTML Helper in your future. The HTML Helper can work through your Model at runtime and output the appropriate HTML for the View. If you go that route, I suggest you get to know the StringBuilder and TagBuilder classes. They'll simplify things and help make your HTML Helper much more readable.
I did not know about the concept of templated helpers. This is what happens when you're new to something. Pretty much, this is what fixed my problem
http://msdn.microsoft.com/en-us/library/ee308450%28VS.100,printer%29.aspx