Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Guys Wish U Happy New Year.
Coming to my question.My database structure for music is shown below

1)Music_Id
2)Song_Name
3)Music_Director

In Create view to edit multiple song properties at a time like

@using (Html.BeginForm("Create", "Home", FormMethod.Post, new { }))
{
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Music</legend>


@foreach (var item in Model)
        {
        
        <div style="float:left;">
         <div class="editor-label">
        @Html.LabelFor(model => item.Music_Id)
      </div>
      <div class="editor-field">
        @Html.EditorFor(model => item.Music_Id)
       @Html.ValidationMessageFor(model => item.Music_Id)
       </div>

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

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


This Is My View.If i pass 3 songs to view this view show 3 song properties in edit mode.Now my question is how to pass these 3 songs properties to controller when i click submit button and insert into database.
Thanks for reading the question.Any suggestions are accepted.
Posted
Updated 1-Jan-14 0:50am
v2
Comments
Bala Selvanayagam 1-Jan-14 7:30am    
Are you saying that you are editing three songs at the same time in the view and click of a button, save those into the database ?

The general practise is that you will list all songs and select the one you want to edit (edit one at a time)..What made you to set the number of songs to be edited at the same time as 3 ? why not 5 or 10 ?
Ajay_Babu 1-Jan-14 23:42pm    
i just give an example of passing of 3 songs.i used foreach loop in view.If i pass model containing 5 songs it will show 5 songs.I need to insert multiple songs into database(not one by one).thanks 4 ur reply.If u know the logic to insert multiple rows.please reply that one.its urgent

 
Share this answer
 
Comments
Ajay_Babu 2-Jan-14 0:10am    
Hip Hip Hurrey.Thanks a lot,I tried a lot.but i didn't get any option.i lost my hair because of this logic.Thank you ,Thank you so much..........
JoCodes 2-Jan-14 1:19am    
Ajay ,You are always welcome :). And thanks for marking/upvoting solution. Happy new year.
Thanks To Jocodes For giving me the way to find the solution

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


In Controller I use

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


NOTE :
Quote:
you can't access IEnumerable by index, use List or Array.
 
Share this answer
 
Comments
Tom Marvolo Riddle 2-Jan-14 0:26am    
Hi Ajay ,if you really wants to give credit to jocodes then formally accept his answer(if it helps)
Ajay_Babu 2-Jan-14 0:37am    
Hi Jas24,Thanks For your Reply,I accepted
Tom Marvolo Riddle 2-Jan-14 1:57am    
Welcome Ajay!
JoCodes 2-Jan-14 1:22am    
Really appreciate your effort to share it here with your code sample so that others with similar issues can be benefited and adding the quote about IEenumerable.:)
Ajay_Babu 2-Jan-14 1:26am    
Thank You Once again

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