-1

I want make a form using a model that contains a list of another entity, but I don't know how to do it. Note: without Entity Framework.

I have use only a model that contains only its fields, but not another entity.

My code is:

Tarea.cs

public class Tarea
{
    public int ID { get; set; }
    public string NombreTarea { get; set; }
    public int TiempoTarea { get; set; }
}

Employee.cs

public class Employee
{
    public int ID { get; set; }
    public string Name { get; set; }
    public DateTime JoiningDate { get; set; }
    public int Age { get; set; }
    public List<Tarea> Tareas { get; set; }
}

And my view is:

Create.cshtml

@model MVCSimpleApp.Models.Employee
....
@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Employee</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.JoiningDate, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.JoiningDate, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.JoiningDate, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Age, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Age, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Age, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

I don't know how to put the List<Tarea> in the form. Thanks in advance.

Omar Murcia
  • 547
  • 2
  • 11
  • 26
  • Are you wanting to dynamically add `Tarea` records in the view? If so, refer the answers [here](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) and [here](http://stackoverflow.com/questions/40539321/partial-view-passing-a-collection-using-the-html-begincollectionitem-helper/40541892#40541892) for some options –  Jan 07 '18 at 20:55
  • @StephenMuecke yes, I want to add Tarea dynamically. – Omar Murcia Jan 07 '18 at 21:01
  • do you want to show Tareas in drop down or in table or to create form..?? – Jilani pasha Jan 08 '18 at 05:02
  • @Jilanipasha to create dynamically a set of inputs in the same form. – Omar Murcia Jan 08 '18 at 05:03

1 Answers1

1

For Example, if we want to collect two sets of Tareas model,

View Code:

@for (var item = 0; item < 2; item++)
{
  @Html.HiddenFor(model => model.Tareas[item].ID)
  @Html.TextBoxFor(model => model.Tareas[item].NombreTarea, new { @class = "form-control" })
  @Html.TextBoxFor(model => model.Tareas[item].TiempoTarea, new { @class = "form-control" })
}

And in the Post Create Action Method..

[HttpPost]
public ActionResult Create(Employee model)
{
  // here we we get the all values given from the view ...
  string example = model.Name;
  List<Tarea> list = model.Tareas; // and iterate through model.Tareas to get List model values
}
Jilani pasha
  • 389
  • 3
  • 14
  • That is NOT the way to dynamically add new items to a collection (refer the links in the comments to the question for the correct approach) –  Jan 08 '18 at 06:45