What is the exact difference between #Html.Display() and #Html.DisplayText()? Both are giving the same output.
I have a model class Employee which has a property
Name = "my Name".
And I am using Html helper in following way -
#Html.Display("Name")
#Html.DisplayText("Name")
The output for both methods is "my Name"
You can inspect the source code for Display and DisplayText.
#Html.DisplayText(propertyName) will only output the the value of the property (defined by the SimpleDisplayText of ModelMetadata).
#Html.Display(propertyName) provides far more options. If you have provided a DisplayTemplate from the property type, it will use the html in that template by default. You can also specify a specific template name to be used to generate the html. In additional, you can use the additionalViewData parameter of Display() to pass additional information to the template.
In your case, you property is string and you have not defined a DisplayTemplate for string, therefore Display() uses the default (in-built) template which generates the same output as DisplayText()
Related
In MVC5 I have multiple 'shared' display templates for the same data type such as DateTimeused with multiple controllers but I want one of the templates to be the one used unless I specifically state the template name.
Is there a way I can do the following:
Require that a display template only be used if it was specified using the 'UIHint' attribute or template name specified in DisplayFor(x => x.Time, "templatename")
OR
Indicate that a certain display template is used unless the template name is provided.
I am aware of placing the template in the controller's shared display template folder and the search order but I'm referring to multiple templates for the same data type being in the shared display template folder.
If you do not specify a location (using [UIHint] of the 2nd parameter of DisplayFor()),templates are searched in the following order
/Views/YourControllerName/DisplayTemplates
if nothing is found, then
/Views/Shared/DisplayTemplates
and if nothing is found, then the default (in-built) template for the property type is used.
Locate you DateTime.cshtml template in /Views/Shared/DisplayTemplates and it will be used unless you override it using [UIHint] or by specifying the location in #Html.DisplayFor(), or by including a specific template for the controller.
I tried using this as a header for a view but it returns an empty string. In the Razor layout, I have something like:
#model IEnumerable<MVCApp.Models.Model>
<h2>#Html.DisplayNameForModel()</h2>
Do I need to set something on the model definition itself? I tried a data annotation [Display(Name="Model Name")] but it is a build error:
Attribute 'Display' is not valid on this declaration type. It is only valid on 'method, property, indexer, field, param' declarations.
The documentation DisplayNameExtensions.DisplayNameForModel Method is terse. The syntax calls for a parameter, but says:
No overload for method 'DisplayNameForModel' takes 1 arguments
As the Usage section says "When you use instance method syntax to call this method, omit the first parameter"
So, how do I use this method to return something?
I just used the default MVC 5 template project in VS2013 and have the #Html.DisplayNameForModel() working with no issues.
First, you are using the wrong data annotation on your view model. You want to use [DisplayName("My Model Name")] and not [Display()]
[DisplayName("Test View Model")]
public class TestViewModel
{
public string TestProperty { get; set; }
}
Second, the html parameter you are seeing on MSDN is a required parameter for any Html helpers in MVC. You do not have to pass anything for this value, the view engine does this for you. So, in your view, you would use the following to get the Display Name that you set on the model as so.
<h2>#Html.DisplayNameForModel()</h2>
Now, your result should output the display name attribute you set in your html. *Note the Test View Model above the Log In text.
try
#Html.DisplayNameFor(m => m.Name)
I am using Preprocessed Text Templates (.tt) . How can I pass object to another included Preprocessed Text Template? I checked the documentation but didn't find any sample to do that.
Thanks
If you look at the generated code from your preprocessed template, you'll see that it is just a partial class. A great way to pass in data is simply to add another partial of the class, and provide a member and Getter/Setter pair for it. If you then include some further code in that preprocessed template, it will contribute to the same class, so your added member will still be available. If on the other hand, your included code is just class feature clocks (<#+ #>), then you'll likely be defining methods that you can call, in which case you can simply add your data to pass as an extra parameter to those methods.
I'd like to set a DataAnnotation on a view model to a dynamic value that is configurable via the web.config. In the following example I get this error "An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type". Anyone know if this is possible? Thanks
[DataType(DataType.Password)]
[RegularExpression(Properties.Settings.Default.PasswordExpression)]
public string Password { get; set; }
Attribute parameters must be constants, i.e. something whose value can be resolved at compile time. But you could write your own simple Attribute class that took the name of the the item in the appSettings, got the underlying value, and passed that on to the normal RegularExpression processing. Then your attribute would look like this:
[ConfigedRegularExpression("PasswordExpression")]
where PasswordExpression was the name of the app setting containing the actual regular expression string.
and, after writing this and doing a search (I should have done that first), I see someone's worked it out for you here:
How to write custom RegularExpressionValidator which takes the values from the config file?
I have been using mvc2 for a while now, and when i need to set the template i use the DataType Attribute
[DataType("DropDown")]
public int Field { get; set; }
I see others using UiHint to achieve the same results
[UiHint("DropDown")]
public int Field { get; set; }
What is the difference between using these two attributes? Which attribute should I be normally using, or are they for different tasks?
DataType is generally used to make it known that this is a very specific version of a property, such as price.
The most common example of DataType is the [DataType(DataTypes.EmailAddress)] which usually is a string but we're saying that this is a very specific type of string.
They're both helpful and the UIHint overrides the DataType. So if you have a certain DataType but you want to override the editor for that specific property you can use a UIHint.
DataType attribute has two purposes
Provide additional type information for a data field. You do this by applying the DataTypeAttribute attribute to a data field in the data model and by specifying the additional type name from the DataType enumeration. Then the view engine uses the default template for displaying the property, like, a checkbox for a boolean.
If you want to override the default template, and wish to use a custom template, then it can be used to associate a custom field template with that data field. In this case you must provide a partial page[.cshtml, MVC 4] to describe the display.
The purpose of UIHint is exactly same as the second point above.
Where to use what? The answer is: context, ie., what will make more sense, what is closer to the physical problem your code is trying to solve.
What if both are applied to the same property? The answer is: UIHint has precedence, obviously. But why would you apply both?