Say I have a Required attribute decorating each required property on each MVC model for required field validation. This is pretty standard. Let's say there's some confusion or disagreement among the stakeholders regarding which input fields should be required.
The stakeholders want to have a meeting to review a list of all the input fields that are required for each view. Is there any way to generate a report of this information or would I have to create this report manually by reviewing each model reference
1-by-1 and putting it in a spreadsheet?
So say EmailAddress is marked as Required on the CustomerModel. And say ProductCode is marked as Required on the Product model. How would you auto-generate both of these into a report? You could Find in Files but this would not generate output in a format
suitable for review by a business analyst unless I'm missing something in your suggestion....
I don't think you can generate report but it yeah find in files will help you find all uses a particular type and then you can do all formatting in excel.
If I have a huge application with various model property validation types then manually generating a report from Find in Files would be very time-consuming. So creating a validation report based on this implementation would not be practical....
You can do it programmatically. Please try finding the solution using the code below. Not that this not the complete code for your question, but you may have to refractor the code slightly.
abdu - looks like a creative solution thanks for putting that together. An object list could be created based on the results found and that could be exported to Excel. That's just a snippet though. How to loop through all controllers in the Controllers directory,
then all properties on each controller. Not saying it can't be done but not very easy....
Type[] typelist = GetTypesInNamespace(Assembly.GetExecutingAssembly(), "YourProjectName.Models");
for (int i = 0; i < typelist.Length; i++)
{
var modelName = typelist[i].Name;
}
and here is the code to get all the properties from each model
Employee emp = new Employee();
foreach (var property in emp.GetType().GetProperties())
{
var myProperty = property;
//Do something with the property
}
Try putting these together, and achieve your goal. I got very little time, and could not research much on this, but by utilizing all the code above, you should be ablel to achieve it for sure.
Hope this helps.
Don't forget to "Mark As Answer" if a post helps you.
dotnetterAMG...
Member
236 Points
520 Posts
how to report required fields in mvc?
Jul 15, 2012 06:15 PM|LINK
CPrakash82
All-Star
18720 Points
2899 Posts
Re: how to report required fields in mvc?
Jul 15, 2012 08:08 PM|LINK
Use Visual studio's Find (or Find in files) feature, you can even write regex to find a particuar statement.
Thanks,
dotnetterAMG...
Member
236 Points
520 Posts
Re: how to report required fields in mvc?
Jul 15, 2012 08:17 PM|LINK
CPrakash82
All-Star
18720 Points
2899 Posts
Re: how to report required fields in mvc?
Jul 15, 2012 08:23 PM|LINK
I don't think you can generate report but it yeah find in files will help you find all uses a particular type and then you can do all formatting in excel.
Thanks,
dotnetterAMG...
Member
236 Points
520 Posts
Re: how to report required fields in mvc?
Jul 15, 2012 08:28 PM|LINK
abdu292
Participant
1555 Points
372 Posts
Re: how to report required fields in mvc?
Jul 16, 2012 02:29 AM|LINK
You can do it programmatically. Please try finding the solution using the code below. Not that this not the complete code for your question, but you may have to refractor the code slightly.
bool isRequired = false; if (metadata.ContainerType != null) { isRequired = metadata.ContainerType.GetProperty(metadata.PropertyName) .GetCustomAttributes(typeof(RequiredAttribute), false) .Length == 1; } if(isRequired==true) { //do something }Please let me know if you got the solution. Other wise I will try finding some time to write the code for you.
Hope this helps.
With best regards,
dotnetterAMG...
Member
236 Points
520 Posts
Re: how to report required fields in mvc?
Jul 16, 2012 02:46 AM|LINK
abdu292
Participant
1555 Points
372 Posts
Re: how to report required fields in mvc?
Jul 16, 2012 04:03 AM|LINK
Sorry for the late reply.
Did you mean, loop through each model in the models folder, and all the the properties for each model?
(because, I am unsure, why you need to have the loop through the controllers to know the required attribute).
Best Regards,
With best regards,
dotnetterAMG...
Member
236 Points
520 Posts
Re: how to report required fields in mvc?
Jul 16, 2012 04:11 AM|LINK
abdu292
Participant
1555 Points
372 Posts
Re: how to report required fields in mvc?
Jul 16, 2012 05:05 AM|LINK
Hi, Here is the code
to get all the models, from the Model folder,
using System.Reflection; private Type[] GetTypesInNamespace(Assembly assembly, string nameSpace) { return assembly.GetTypes().Where(t => String.Equals(t.Namespace, nameSpace, StringComparison.Ordinal)).ToArray(); }Usage
Type[] typelist = GetTypesInNamespace(Assembly.GetExecutingAssembly(), "YourProjectName.Models"); for (int i = 0; i < typelist.Length; i++) { var modelName = typelist[i].Name; }Taken from from the link : http://stackoverflow.com/questions/949246/how-to-get-all-classes-within-namespace
and here is the code to get all the properties from each model
Employee emp = new Employee(); foreach (var property in emp.GetType().GetProperties()) { var myProperty = property; //Do something with the property }Try putting these together, and achieve your goal. I got very little time, and could not research much on this, but by utilizing all the code above, you should be ablel to achieve it for sure.
Hope this helps.
With best regards,