0

I'm trying to print out all of my model values into my view.

I don't wish to "hard-code" it for each value, but rather have it dynamically grow accordingly to my models structure. This is so that non-developers can test out if everything gets inserted correctly into the database, what values are added etc

Currently I have this code in my controller that prints out all the variables in my model, but I'm not sure how to connect some certain objects values onto it

var result = "";
var myAssembly = typeof(Person).GetTypeInfo().Assembly;
var properties = Converters.GetTypesInNamespace(myAssembly, "Domain.Models")
                           .FirstOrDefault(a => a.Name == "Person")
                           .GetProperties().ToList();

for (var i = 0; i < properties.Count; i++)
{
     result += properties[i].Name + Environment.NewLine;
}

return Ok(result);

And this is the custom method I use

public static Type[] GetTypesInNamespace(Assembly assembly, string nameSpace)
{
    return assembly.GetTypes()
                   .Where(t => String.Equals(t.Namespace, nameSpace, StringComparison.Ordinal))
                   .ToArray();
}

I'd like to get back a view that displays all the values for a Person with a certain id, ie Person/5 returns it's FirstName and LastName, but if I now add a new variable to my model - the view will display that as well

2
  • As stated in the tag model-view-controller don't use that tag for asp.net-mvc. Commented Dec 4, 2018 at 17:47
  • Why not just use a Dictionary<string, string> as property on your ViewModel and loop through it in the view? Commented Dec 4, 2018 at 17:54

1 Answer 1

3

Suppose you have model named Person and has some properties:

public class Person
{
    public string FirstName { get; set; }

    public string LastName { get; set; }

    public int Age { get; set; }
}

Add a controller action to accept person id :

 public IActionResult PestsonDetails(int id)
 {

        //you can search database with person id 
        Person p = new Person();
        p.FirstName = "FirstName";
        p.LastName= "LastName";
        p.Age = 28;

        Dictionary<string, string> personDetails = new Dictionary<string, string>();

        foreach (PropertyInfo prop in p.GetType().GetProperties())
        {

            var propName = prop.Name;
            var propValue = prop.GetValue(p, null);
            personDetails.Add(propName.ToString(),propValue.ToString());

        }
        ViewBag.personDetails = personDetails;
        return View();
  }

Then you could get the property name and value in view like :

@foreach (var personProperty in ViewBag.personDetails)
{
    @personProperty.Key
    @personProperty.Value    
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.