DataAnnotations Displayname("Some thing here") and using italics, possible? - asp.net-mvc

As the title suggests i have:
DisplayName("This is the display name")
int Id {get;set;}
And i would like to set a part of this name in italics.
DisplayName("This is the <i>display name</i>")
int Id {get;set;}
But this is getting sanitized and the HTML is not getting used. Is there a way round this?
I can't put the italics on the view itself as i only want a part of the display name italicized.
Cheers,
Kohan

You will have to create your own Custom Template. Then you have the full control over the HTML being rendered.

Related

How to setup a helper for an Html extension

I apologize for the somewhat vagueness of this question in advance. I have setup a number of custom Html helpers to use in partial views...
What i am trying to do is set it up so if want to change how the field is displayed, then I can use an extension.
The following:
#Html.FieldFor("Somefield", "Label name:", #Model.Somevalue)
will render a standard text box on the screen with with the Id of "SomeField" and a Label of "Label Name:" and the third element is what would be shown in the field itself.
Let's say I want to show the Value in #Model.Somevalue on the screen without it being in a text field if the value is not editable at that time...
In that case I would like to be able to have something like this...
#Html.FieldFor("Somefield", "Label name:", #Model.Somevalue).NotEditbable()
and use that in the helper to determine the code that is passed back. Can someone point me to a tutorial or other resource I can use to accomplish this?
below is the Helper in question if that is useful...
public static MvcHtmlString FieldFor(this HtmlHelper helper, string FieldName, string Label, string ValueContents = null)
{
return new MvcHtmlString(String.Format("<label for=\"{0}\">{1}</label><input id=\"{0}\" type=\"text\" Value = \"{2}\">", FieldName, Label, ValueContents));
}
I ended up using this page as a guide to setup a new extension method and then to create the method I switched the MVCHtmlString to a regular string... Made the alterations, I wanted to make and then re-encoded it to a MVCHtmlString...

How to make HTML Tags possible to accept in Textarea in mvc Razor?? Or which other controls should i use to do that?

Hello guys,
I have created one textarea in my application.
And now i want to add HTML tags in my textarea and want to accept it using following tag
#Html.TextBoxFor(model => model.PageTitle, new { style = "Width:500px; maxlength=500;" })
It doesn't allow HTML code but the simple text.
So what should i do for that???
apply the following attribute to the property PageTitle:
[AllowHtml]
public string PageTitle { get; set; }
Two options:
Create a custom EditorTemplate, but that involves also
Use #Html.TextBox which does have the overload to use custom HTML attributes.
This is assuming you meant attributes and not tags as your example code implies.

How to insert a new line in a [DisplayName] annotation

Is there anyway of putting a new line in a [DisplayName()] annotation of an mvc viewmodel? I currently have a property that is:
[DisplayName("Delivery Time (if different from our normal delivery of 10-30am – 12.30pm):")]
public string DeliveryTime { get; set; }
and I want to have a new line after the Delivery Time part so that the bit in brackets is below, is that possible? I've tried \r\n but that didn't work.
Well, the problem you have is that \n is ignored in html. You'd need to use <br /> BUT that does tie your attribute metadata to using html formatting.
I'm looking for a prettier solutions, but a work around is presented in How to add a new line into Display Attribute, Name field
To summarize you put a <br> (or <br/>) in your DisplayName string and use
#Html.Raw(ViewData.ModelMetadata.DisplayName)
to display it.
You can just use style="white-space: pre-line" in your element and \n in DisplayName attribute. At least, it worked for me.

Example of using AdditionalMetadata in MVC 3

In asp.net MVC 3 there is a new attribute that allows us to pass additional Meta Data to our views e.g.
[Required]
[AdditionalMetadata("Tooltip", "The title of the item")]
public string Title { get; set; }
The question is, how do I actually make use of this information in my view? I thought that perhaps it would render out the data as html 5 data attributes but this is not the case.
A simple example would be much appreciated.
According to the documentation:
You can use the
AdditionalMetadataAttribute class to
populate the
ModelMetadata.AdditionalValues
dictionary for a model property.
...
This metadata is made available to any
display or editor template when a
product view model is rendered. It is
up to you as application developer to
interpret the metadata information.
So let's make use of it in the view:
<h2>
#ModelMetadata.FromLambdaExpression(x => x.Title, ViewData).AdditionalValues["Tooltip"]
</h2>
example in a template:
Object optionlabelOverride;
ViewData.ModelMetadata.AdditionalValues.TryGetValue("OptionLabelText", out optionLabelOverride);
the variable you are reading must be an object and then you can cast it. Everything you set in the model is available as ViewData.

setting page html title from within an action

How can I set the Html title from within a controllers action?
I don't believe there is a way to simply set the title directly from the controller with out setting up your views to accept some sort of data associated with the information. Especially since to actually set the title you'll need to output data in between tags.
That said, I'm sure there's something you could do to make this easier on yourself. I'm more or less just thinking aloud here, so I can't guarantee this will work. If I was sure that I would set my title's on every action that I have, then I would keep the title tags in the master page and create a custom attribute so you could do something like this:
[CustomTitleAttribute(Title = "Hello World")]
public ActionResult Index()
{
return View();
}
It would be up to you to implement the attribute and set up how you capture this information in the view and/or master page.
Generally speaking, since you may want the title to change on pages that have dynamic data, the above is probably not something you should do. Instead, just incorporate some way to determine the title you need in a view model. Maybe even a base view model that subsequent view models can inherit from.
public class BaseViewModel
{
public string PageTitle { get; set; }
public string PageDescription { get; set; }
//etc.
}
Then in the views you can do this, or even in your master page I think:
<title><%= Model.PageTitle %></title>
I think from a standpoint of separating concerns(which is kinda the whole point) that would be the best way to go.
It sounds not very wise for controller to control the output.
All a controller do, is to give viewdata to the output view and let the view determine what to do with it.
Please refer to the default ASP.net MVC template for how it is done.

Resources