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?

3
  • Use [Required] attribute in the model, and in the controller check if the model.isValid() Commented Jan 2, 2014 at 11:44
  • i tried that one.but every time Model.Is Valid() is true for all songs..Even though the fields are not entered Commented Jan 2, 2014 at 11:47
  • Yes you are right. You will have to manually validate each candidate as per @Ahm3d reply. You can also implement a custom validator. Commented Jan 2, 2014 at 12:08

2 Answers 2

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

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

Comments

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.

1 Comment

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

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.