-1

I've got a view from EditApplicationConcessionModel view model

This View Model along with it's other properties has a property ListAnexes

public List<Anexes> ListAnexes { get; set; }

here is theAnexes class

public class Anexes
  {

    [Display(Name = "Number", ResourceType = typeof(Resources.Resources))]
    public string ContractAnexesNumber { get; set; }    

    [Display(Name = "Date", ResourceType = typeof(Resources.Resources))]
    public string ContractAnexDate { get; set; }
  }

SO in my view I iterate through this list of annexes and render a partial view _Anexes which shows the time and the number of the annex

<div id="divAnexes">
        @foreach (var item in Model.ListAnexes)
         {
               l.RenderPartial("_Anexes", item);
         }
 </div>
 <div>
      <button class="btn btn-primary" type="button" id="addAnex">ADD</button>
 </div>

here is my partial view _Anexes

@model MEMineralResources.Common.Entities.Anexes

@using (Html.BeginCollectionItem("ListAnexes"))
{

    <div class="divAnex">
        <div class="form-group">

            @Html.LabelFor(m => m.ContractAnexesNumber, new { @class = "control-label" })
            @Html.TextBoxFor(m => m.ContractAnexesNumber, new { @class = "form-control" })
            @Html.LabelFor(m => m.ContractAnexDate, new { @class = "control-label" })
            @Html.TextBoxFor(m => m.ContractAnexDate, new { @class = "form-control datepicker" })


            <div id="linkDelte"></div>
        </div>
    </div>
}

You see that I have a button addAnnex and when you click it I make ajax request to AddAnex action and just append that partial view _Anexes (which the action returns)to the div with other annexes (so you can fill the data and number of another annex)

 $('#addAnex').on('click', function () {
        $.ajax({
            async: false,
            url: '@Url.Action("AddAnex", "Application")'
        }).success(function (partialView) {
            $('#divAnexes').append(partialView);

        });
    });

Here is the AddAnnex action

public ActionResult AddAnex()
    {
      Anexes anex = new Anexes();
      return PartialView("_Anexes", anex);

    }

The problem is : If the list contains only one Anexes item - then it's number and date are not obligatory. But obviously if you click Add button then you have to fill both annexes' numbers and dates! And I can't think of a custom validation that can suit this case

Tania Marinova
  • 1,788
  • 8
  • 39
  • 67

1 Answers1

0

I think the best choice for you is using remote data annotation and putting some logic on that to check if Anexes no is more than one, apply validation.

Hadee
  • 1,392
  • 1
  • 14
  • 25