Click here to Skip to main content
15,917,862 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My View looks like

@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>
            
}


And My Music.Class is show below


C#
public class Music
    {
        [Required]
        public int Music_Id{ get; set; }
        [Required]
        public string  Song_Name { get; set; }
        [Required]
        public string Music_Director { get; set; }

}



How to validate this list of music files in controller.Thanks in advance for reading the question
Posted
Updated 2-Jan-14 2:07am
v2

Does checking for the following provide enough information in your controller?

C#
if (ModelState.IsValid)
{
    // Continue process
}
else
{
    // Handle errors
}
 
Share this answer
 
Comments
Ajay_Babu 2-Jan-14 7:37am    
i checked but every time Modelstate.Isvalid is true
[no name] 2-Jan-14 8:02am    
Would you be able to post your MusicBusinessLayer.Music class code?
Ajay_Babu 2-Jan-14 8:08am    
i post my class.see once
[no name] 2-Jan-14 8:26am    
If I copy your HTML and add the following code to my controller, ModelState.IsValid shows the correct value (I blanked out one of the fields then submitted the form):

public ActionResult Create()
{
var model = new List<Music>();

model.Add(new Music { Music_Id = 1, Music_Director = "ABC", Song_Name = "DEF" });
model.Add(new Music { Music_Id = 2, Music_Director = "XYZ", Song_Name = "GHI" });

return View(model);
}

[HttpPost]
public ActionResult Create(List<music> model)
{
if (ModelState.IsValid)
{

}

return View(model);
}

Is this different from what you are doing? To look further into it, I think we'd need to see the controller code as well.
Ajay_Babu 3-Jan-14 1:33am    
Hai brent jenkins.Thanks for showing your interest in my question.I never get this type of person who working long time for my question.Lot of thanks to that one.I searched in web from yesterday onwards, i never get a solution till now.so i left it now,I search for an alternative to this one.Because of my project manager makes me to do fast.So Thanks a lot.If you want to give a solution to this one.i feel happy if you give me a solution.Here is my controller code.

public ActionResult Create(List <music> musicfiles)
{
if(Modelstate.IsValid)
{
//insert into database
}
}

Everytime it valid eventhough the fields are null.
Following up on the previous posts, it might be worth thinking about splitting the view and create actions up.

Looking at your original post, it's difficult to see a scenario where you might want to add many items at once (how would the application know how many items you wanted to add?).

So starting with your model class..
C#
namespace Sample.Models
{
    using System.ComponentModel.DataAnnotations;

    /// <summary>
    /// The Music class.
    /// </summary>
    public class Music
    {
        [Required]
        public int Music_Id { get; set; }

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

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

..add in two views; Index (to display the current list of music files stored in your data source):
HTML
@model List<sample.models.music>

<h1>Music</h1>

<table>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @item.Music_Id
            </td>
            <td>
                @item.Song_Name
            </td>
            <td>
                @item.Music_Director
            </td>
        </tr>
    }
</table>

@Html.ActionLink("Add", "Create")

..and Create (to allow a new music file to be defined):
HTML
@model Sample.Models.Music

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

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

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

Then finally update your controller to handle the appropriate actions:
C#
namespace Sample.Controllers
{
    using System.Collections.Generic;
    using System.Web.Mvc;
    using Sample.Models;

    /// <summary>
    /// The HomeController class.
    /// </summary>
    public class HomeController : Controller
    {
        /// <summary>
        /// Performs the Index action.
        /// </summary>
        /// <returns>The Index view with data.</returns>
        public ActionResult Index()
        {
            return View(GetData());
        }

        /// <summary>
        /// Performs the Create (GET) action.
        /// </summary>
        /// <returns>The Create view with initial data.</returns>
        [HttpGet]
        public ActionResult Create()
        {
            return View(new Music());
        }

        /// <summary>
        /// Performs the Create (POST) action.
        /// </summary>
        /// <returns>The Create view with validation information, or a redirect back to the Index view.</returns>
        [HttpPost]
        public ActionResult Create(Music model)
        {
            if (ModelState.IsValid)
            {
                AddItem(model);
                return RedirectToAction("Index");
            }

            return View(model);
        }

        /// <summary>
        /// Gets the simulated data source items.
        /// </summary>
        /// <returns>The simulated data source items.</returns>
        private List<music> GetData()
        {
            if (Session["data"] == null)
            {
                Session["data"] = new List<music>();
            }

            return (List<music>)Session["data"];
        }

        /// <summary>
        /// Adds a new item to the simulated data source.
        /// </summary>
        /// <param name="item">The item to add.</param>
        private void AddItem(Music item)
        {
            var data = GetData();
            data.Add(item);
            Session["data"] = data;
        }
    }
}

Note that in the example I've given, I'm just creating a simulated data source as a Session variable.

If you really wanted to add multiple items at once, you could modify the Create action to just add a blank item to your data source and change add a POST Index action to perform an Update on the data instead. Let me know if that's what you're looking for and I'll post that solution here.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900