0

How to convert model class into dynamic controls in the view without a loop? I want to create if checkbox than show checkbox control if dropdown than show dropdown if text than create textbox.

 public class DataSources
    {

        public int Id { get; set; }
        [DisplayName("text")]
        public string Name { get; set; }
        [DisplayName("text")]
        public string Phone { get; set; }
        [DisplayName("checkbox")]
        public bool Active { get; set; }

        [DisplayName("dropdown")]
        //[Column(TypeName = "dropdown")]
        public List<DataSources> MyList { get; set; }

    }
3
  • Please clarify what you mean in the second sentence. Do you mean if a checkbox is selected, then an additional checkbox and dropdown menu will appear? Commented Apr 2, 2019 at 10:43
  • [DisplayName("checkbox")] i write that for understanding, that this model class generate textboxes,checkbox if datatype is bool and dropdown if datatype is list Commented Apr 2, 2019 at 10:47
  • Provide more details. The question is not clear Commented Apr 2, 2019 at 10:48

3 Answers 3

1

You can use the Editorfor HTML helper which binds the control based on the model type. For an example, if the type is a string then it will take textbox and if it is a list, it will bind the dropdown:

@Html.EditorFor(model => model.Propertyname)

You can find the link here for details explanation - https://www.steelcm.com/how-to-use-the-html-editor-for-method/

Sign up to request clarification or add additional context in comments.

3 Comments

You can add a class like : @Html.EditorFor(x=> x.MyProperty, new { htmlAttributes = new { @class = "MyCssClass" } }). I do not understand when you say complete class.
i say about that model class, without add property name. only one line use for create controls without loop
Class has only attributes which are used to set display names, validations. I don't remember any property which uses for generating the controls dynamically.
1

i understand that what do you want, you want to show controls only use of one line than you can use this

@model MVCApp.Models.DataSource
@Html.EditorForModel()

by use of this its automatically create dynamic controls.

1 Comment

thanks man i want to use something like this, i use this its working.
0

The class does not support any such property to manage these things dynamically. One way around to implement this is as follows :

My ActionResult Method be like(Consider this as sample data) :

DataSource model = new DataSource();
model.MyList = new List<DataSource>();
var data = new DataSource()
{
    Id = 1,
    Name = "test"
 };
 model.MyList.Add(data);
 return View(model);

This will return data to the View. And your View should be like as below :

@model MVCApp.Models.DataSource

@if (Html.DisplayNameFor(x => x.Name).ToString() == "text")
{
    @Html.TextBoxFor(model => model.Name)
}
@if (Html.DisplayNameFor(x => x.Phone).ToString() == "text")
{
    @Html.TextBoxFor(model => model.Phone)
}

@if (Html.DisplayNameFor(x => x.Active).ToString() == "checkbox")
{
    @Html.CheckBoxFor(model => model.Active)
}
@if (Html.DisplayNameFor(x => x.MyList).ToString() == "dropdown")
{
    @Html.DropDownListFor(m => m.Id,
                new SelectList(Model.MyList.Select(x=>x.Name)),
                "-- Select --")
}

This worked for me. You need to create your own HTML helper to achieve this more effectively.

Let me know if this helps!

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.