2

enter image description here

I've read here about additional information of web API help page. The data annotation actually provides the additional information for documentation. But I want to know that is there anyway to provide additional information without data annotations?

If yes then how?

If not then is there anyway to override the additional information with data annotations for instance the

[Required]

shows Required written in additional information but what if I want to show "This field is required" or something like that?

Thanks

EDIT see in picture I want to update that additional information without data annotation if possible.

Community
  • 1
  • 1
Muhammad Omer
  • 111
  • 2
  • 14
  • I think you are looking for alternate of Data Annotation. Then you should use Fluent API, https://msdn.microsoft.com/en-in/data/jj591617.aspx. – Hiren Visavadiya Mar 07 '16 at 06:44

3 Answers3

3

So the annotation allows you to further specify requirements, i.e if you have the following model:

public class MyModel {

    [Required(ErrorMessage = "You seriously need a name here bro")]
    public string Name{ get; set; }

}

You can then automatically have the validation message shown in your ASP.Net page like so:

@model string
@Html.TextBoxFor(m => m)
@Html.ValidationMessageFor(model => model, "", new { @class = "text-danger"})

So basically, you add a field for the validation message that will be populated by ASP.Net when the Required attribute kicks in.

Daniel
  • 9,491
  • 12
  • 50
  • 66
Pedro G. Dias
  • 3,162
  • 1
  • 18
  • 30
  • alright that provides an answer for overriding the message ... is there anyway for doing so without data annotation? – Muhammad Omer Mar 07 '16 at 07:10
  • Yes, you can use ModelState.AddModelError() in your code and avoid data annotation - don't forget to tag the answer if it solved your problem :) – Pedro G. Dias Mar 07 '16 at 07:12
  • correct me if im mistaken .... im asking about Api help documentation i think this is for simple view and i dont need that – Muhammad Omer Mar 07 '16 at 07:26
  • Tricky - if you want API documentation, why not use the standard constructs for doing that, i.e. by commenting using /// above methods, and then extracting your API documentation as outlined in this answer: http://stackoverflow.com/questions/641364/c-sharp-documentation-generator – Pedro G. Dias Mar 07 '16 at 07:29
  • yes that provides only the description for that property... what i need to do is provide custom additional information for properties. :( or how can i modify that to give additional information?? – Muhammad Omer Mar 07 '16 at 07:39
3

You can edit the Required Attribute in the ModelDescriptionGenerator.cs
Areas>HelpPage>ModelDescriptions>ModelDescriptionGenerator.cs
For example:

    [Required(ErrorMessage ="Must pass")]
    public string Name { get; set; }

I got: Additional information : Must pass

replace:

 { typeof(RequiredAttribute), a => "Required" }

with:

{ typeof(RequiredAttribute), a => {
            RequiredAttribute b =(RequiredAttribute)a;
            return (b.ErrorMessage);
        }

see

Shir
  • 31
  • 1
  • Yes. :( But u can edit the **parameters.cshtml** And edit the message. `foreach (var annotation in parameter.Annotations) {

    @annotation.Documentation

    }`
    – Shir Mar 14 '16 at 07:59
2

If you want to give custom additional information(using data annotation) then @Pedro G. Dias's answer is your solution but if you want to give additional information without using data annotation then I am afraid that it is not possible OR you have to use some alternative procedure to do so as commented by @DynamicVariable on your question.

PS. I've debugged documentation project to check and I found that addition information is actually provided by data annotations.

Malik Rizwan
  • 759
  • 1
  • 10
  • 31