如何将多个项(类对象)从视图传递到控制器 [英] How to pass mutiple items(class objects) from view to controller

查看:62
本文介绍了如何将多个项(类对象)从视图传递到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Guys祝你新年快乐。

来我的问题。我的音乐数据库结构如下所示



1) Music_Id

2)Song_Name

3)Music_Director



在创建视图中一次编辑多首歌曲属性喜欢



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





这是我的视图。如果我通过3首歌曲来观看此视图显示3首歌曲属性在编辑模式。现在我的问题是当我点击提交按钮并插入数据库时​​如何将这3首歌曲属性传递给控制器​​。

感谢阅读问题。接受任何建议。



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.

推荐答案

您对每个项目的索引。



参考提供想法的链接。



http:// stackoverflow.com/questions/12837288/updating-multiple-items-within-same-view [ ^ ]



http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/ [ ^ ]



希望这有帮助
You the index on each item.

Refer the links which gives an idea .

http://stackoverflow.com/questions/12837288/updating-multiple-items-within-same-view[^]

http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/[^]

Hope this helps


感谢Jocodes给我找到解决方案的方法< br $>


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 :

引用:

你不能通过索引访问IEnumerable,使用List或Array。

you can't access IEnumerable by index, use List or Array.


这篇关于如何将多个项(类对象)从视图传递到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆