modelmetadata

MVC Html Helper: modelmetadata from string expression

╄→尐↘猪︶ㄣ 提交于 2020-02-04 02:49:08
问题 I am trying to build an html helper that would have access to modelmetadata. I need both versions of helper to work: from string expression and from lambda expression: Example: public static MvcHtmlString MyLabel(this HtmlHelper html, string htmlFieldName) { return LabelHelper(html, htmlFieldName); } public static MvcHtmlString MyLabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression) { return LabelHelper(html, ExpressionHelper.GetExpressionText

Is it possible to override the required attribute on a property in a model?

久未见 提交于 2019-12-18 03:54:33
问题 I'm curious to find out if it is possible to override the [Required] attribute that has been set on a model. I'm sure there most be a simple solution to this problem, any takers? 回答1: Depends on what precisely you are doing. If you are working with a subclass, using the model with the Required attribute as the base, you can do this: Redefine the property with the new keyword, rather than override it. public class BaseModel { [Required] public string RequiredProperty { get; set; } } public

Is it possible to override the required attribute on a property in a model?

二次信任 提交于 2019-12-18 03:54:09
问题 I'm curious to find out if it is possible to override the [Required] attribute that has been set on a model. I'm sure there most be a simple solution to this problem, any takers? 回答1: Depends on what precisely you are doing. If you are working with a subclass, using the model with the Required attribute as the base, you can do this: Redefine the property with the new keyword, rather than override it. public class BaseModel { [Required] public string RequiredProperty { get; set; } } public

Add new Property to MVC ModelMetadata

时光毁灭记忆、已成空白 提交于 2019-12-12 11:43:20
问题 I'm seeing alternative approaches to this such as AdditionalValues, but I'm wondering if it's possible to end up in a scenario where you can add a new property to the ModelMetadata object available in template views. For example you could have: @ViewData.ModelMetadata.MvvmBound I want to use this in Editor and Display templates to add MVVM attributes to the HTML elements being rendered. I'm completely lost, but here are my efforts so far: public class MyModelMetaDataProvider :

ASP.NET MVC Custom ModelMetadataProvider and ModelValidatorProvider

隐身守侯 提交于 2019-12-12 08:16:15
问题 In my ASP.NET MVC application, I have metadata based model defined in database. I have a custom object defining the metadata of the data and uses dataset for DTO. To display this, I am planning to write a custom ModelMetadataProvider and ModelValidatorProvider. Is this the right approach? Any pointers on custom ModelMetadataProvider and ModelValidatorProvider? 回答1: The last day or so I was trying to figure out the same. It seems that writing a custom ModelMetadataProvider and

Limit JavaScript and CSS files on ASP.NET MVC 2 Master Page based on Model and View content

佐手、 提交于 2019-12-11 14:08:24
问题 I want to include certain .js and .css files only on pages that need them . For example, my EditorTemplate DateTime.ascx needs files anytimec.js and anytimec.css . That template is applied whenever I use either the EditorFor or EditorForModel helper methods in a view for a model with a DateTime type value. My technique: I've put this condition into the <head> section of my master page. It checks for a DateTime type property in the ModelMetadata. <% if (this.ViewData.ModelMetadata.Properties

How can I extend CachedDataAnnotationsModelMetadataProvider?

让人想犯罪 __ 提交于 2019-12-10 09:36:37
问题 We are looking to use CachedDataAnnotationsModelMetadataProvider as it improves performance and we use a lot of Meta Data in our MVC4 application. We are currently creating a custom ModelMetadataProvider inheriting from DataAnnotationsModelMetadataProvider and overriding CreateMetadata attribute to do some automatic display name creation e.g. remove Id from names etc. However we also want to cache it so we wanted to base our custom ModelMetadataProvider on

How to Transfer DataAnnotation metadata to ViewModel with AutoMapper with Betty's Approach

梦想与她 提交于 2019-12-09 18:31:44
问题 I need clarification on how to implement Betty's code solution to transferring data annotation metadata to ViewModels with AutoMapper (see here). Or if you have a better way, please share that. Maybe the implementation of Betty's answer is obvious to someone who knows AutoMapper well, but I'm new to it. Here is a simple example, what do I add to this code to make Betty's solution work: // Data model Entity public class User1 { [Required] public int Id { get; set; } [Required] [StringLength(60

How to add MetaData to a dynamically build MVC3 ViewModel?

南楼画角 提交于 2019-12-08 05:12:54
问题 One of the key features of a project I'm working on is the ability for the user to configure Forms (as in "Forms" to fill-up) based on a pool of pre-existing field types (well known types, for instance "user name", "date of birth" etc. but also "generic types" like "string", "DateTime" etc.). We used to have a static ViewModel that worked fine for the "well known" types and looked like this: public class UserInputModel { [StringLength(200)] public string Name { get; set; } [Required

ASP.NET MVC3 WebGrid Helper and Model Metadata

房东的猫 提交于 2019-12-07 08:42:54
问题 I'm trying to use the WebGrid html helper in ASP.NET MVC 3 to autogenerate the columns according to the information found in the ModelMetadata. For example the code in a view that accepts a list of objects would be: var grid = new WebGrid(Model); @grid.GetHtml(columns: ViewData.ModelMetadata.Properties.Single.Properties .Select(p => grid.Column( columnName: p.PropertyName, header: p.ShortDisplayName ))); This actually works like a charm (I was surprised it was that easy actually). What