简体   繁体   中英

How to show the model's attribute dynamically in mvc?

Now, I can show and update a model if I know its name and value. For example, here is my Student model:

public class Student
{
    public string Name { get; set; }
    public bool Sex { get; set; }
    public bool Address { get; set; }
}

And, here is what I have in my View:

@Html.TextBoxFor(model => model.Name)
@Html.TextBoxFor(model => model.Sex)
@Html.TextBoxFor(model => model.Address)

I want to show and update a model, but I do not know how many attributes it has and what their names and values are. For example, if I return a Fruit model to the view, I will need to show and update its attributes like Price or Weight. If I return a Student model, I'll need to show and update attributes like Name, Sex, and Address. I have more than ten models in my project. My boss says that I can use key and value like Dictionary<string,string> this way, and iterate through the model attributes, but I do not know how to do it.

Here's a simple example that shows how to do this using a dynamic model.

Firstly, I setup a couple of classes to represent the different examples of models you provided above:

public class Student
{
    public string Name { get; set; }
    public string Sex { get; set; }
    public string Address { get; set; }
}

public class Fruit
{
    public decimal Price { get; set; }
    public decimal Weight { get; set; }
}

Next, I created a DisplayTemplate for each type, like so:

@model Fruit

<p>Fruit template</p>

@Html.DisplayFor(m => m.Price)
@Html.DisplayFor(m => m.Weight)

@model Student

<p>Student template</p>

@Html.DisplayFor(m => m.Name)
@Html.DisplayFor(m => m.Sex)
@Html.DisplayFor(m => m.Address)

Now for the fun part. I created a view model to hold the dynamic model whilst also supplying a field to get the underlying type of the model:

public class ViewModel
{
    public dynamic Model { get; set; }
    public Type ModelType { get; set; }
}

That allows us to do 2 things:

  1. To assign an arbitrary type to Model .
  2. Use ModelType as a way to control which DisplayTemplate should be invoked for the model.

Therefore, your view would look something like the following:

@model ViewModel

@Html.DisplayFor(m => m.Model, Model.ModelType.ToString())

As you can see, this overload of Html.DisplayFor allows us to specify the template name, which is what the second parameter represents.

Finally, I created a quick action method to test this.

Firstly, for the Student type:

public ActionResult Index()
{
    var model = new ViewModel();
    model.ModelType = typeof(Student);
    model.Model = new Student { Name = "John", Sex = "Male", Address = "asdf" };

    return View(model);
}

Secondly, for the Fruit type:

public ActionResult Index()
{
    var model = new ViewModel();
    model.ModelType = typeof(Fruit);
    model.Model = new Fruit { Price = 5, Weight = 10 };

    return View(model);
}

Both gave the desired output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM