2

I upload a file containing songs.I show the properties of each song.If i upload a file having 3 songs.the view shows like this.

 music id:....                 music id:....                   music id:....
 song Name  :....              song Name  :....                 song Name  :....
 Music director :...           Music director :...             Music director :...

I have a view like this

@model List<MusicBusinessLayer.Music>
 @using (Html.BeginForm("Create", "Home", FormMethod.Post, new { }))
 {
@Html.ValidationSummary(true)

<fieldset>
    <legend>Music</legend>


 @for (int i = 0; i < Model.Count();i++ )
    {

    <div style="float:left;">
     <div class="editor-label">
    @Html.LabelFor(model => Model[i].Music_Id)
  </div>
  <div class="editor-field">
    @Html.EditorFor(model => Model[i].Music_Id)
   @Html.ValidationMessageFor(model => Model[i].Music_Id)
   </div>

    <pre><div class="editor-label">
    @Html.LabelFor(model => Model[i].Song_Name)
      </div>
     <div class="editor-field">
       @Html.EditorFor(model => Model[i].Song_Name)
     @Html.ValidationMessageFor(model => Model[i].Song_Name)
     </div>

        <div class="editor-label">
     @Html.LabelFor(model => Model[i].Music_Director)
     </div>
      <div class="editor-field">
      @Html.EditorFor(model => Model[i].Music_Director)
      @Html.ValidationMessageFor(model => Model[i].Music_Director)
       </div>
  }
    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>

}

I use controller like this

 public ActionResult Create(List<Music> musicfiles)
    {
       //......
    }

How to validate controls which are generated in view.i.e; All fields are given or not?

tereško
  • 58,060
  • 25
  • 98
  • 150
user3145484
  • 41
  • 2
  • 6

2 Answers2

2

Data Annotations cannot operate across a collection. Instead, you need to validate that manually in the action.

check this question it might be useful MVC 3 Unobtrusive validation of a list

Community
  • 1
  • 1
Ahm3d Said
  • 802
  • 9
  • 20
1

You can add DataAnnotations validation attributes to the properties in Music class, as in:

public class Music
{
    [Required]
    public int Music_Id {get; set;}

    [Required]
    public string Song_Name { get; set; }

    public string Music_Director { get; set; }
}

Then given the following view based on the one in the OP (I just removed some unclosed tags):

@model List<MusicBusinessLayer.Music>

@using (Html.BeginForm("Create", "Home", FormMethod.Post, new { }))
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Music</legend>
        @for (int i = 0; i < Model.Count();i++ )
        {
            <div class="editor-label">
                @Html.LabelFor(model => Model[i].Music_Id)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => Model[i].Music_Id)
                @Html.ValidationMessageFor(model => Model[i].Music_Id)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => Model[i].Song_Name)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => Model[i].Song_Name)
                @Html.ValidationMessageFor(model => Model[i].Song_Name)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => Model[i].Music_Director)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => Model[i].Music_Director)
                @Html.ValidationMessageFor(model => Model[i].Music_Director)
           </div>
        }
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

And the following dummy controller methods:

public ActionResult Create()
{
    List<Music> musicFiles = new List<Music>()
    {
        new Music { Music_Id = 123, Song_Name = "foo1" }, 
        new Music { Music_Id = 456, Song_Name = "foo2" }
    };
    return View(musicFiles);
}

[HttpPost]
public ActionResult Create(List<Music> musicFiles)
{
    if (ModelState.IsValid)
        return RedirectToAction("Index");

    return View(musicFiles);
}

You should see ModelState.IsValid as false in the POST Create controller method when Music_Id or Song_Name on any item in the musicFiles list are posted as empty values.

Daniel J.G.
  • 34,266
  • 9
  • 112
  • 112
  • How can custom validation be added to the item? Any links? I am asking how would you bind the error message to the exact list item? Usually how would i get the index of the list item where the error message should be thrown – Vini Nov 13 '15 at 13:25