【问题标题】:MVC 4 Passing a list from a view to a controllerMVC 4 将列表从视图传递到控制器
【发布时间】:2015-06-22 21:35:08
【问题描述】:

我是 MVC ASP 的新手,需要您的帮助。 我想将一个列表从视图传递给控制器​​,但每次我提交表单时,控制器中的列表都是空的(null)。

这是我的模型:

namespace ArcheryComp.Models
   {
    [MetadataType(typeof(Participe))]
    public class Participe
    {

        [Required]
        [Display(Name = "Id Archer")]
        public int idArcher { get; set; }
        [Required]
        [Display(Name = "Id Tournoi")]
        public int IdTournament { get; set; }
        [Required]
        [Display(Name = "Division")]
        public int division { get; set; }
        [Required]
        [Display(Name = "Categorie")]
        public string categorie { get; set; }
        [Required]
        [Display(Name = "Score 1")]
        public int ArchScore1 { get; set; }

        [Display(Name = "X")]
        public int ArchScore1_X_6 { get; set; }

        [Display(Name = "10")]
        public int ArchScore1_10_5 { get; set; }
        [Required]
        [Display(Name = "Score 2")]
        public int ArchScore2 { get; set; }

        [Display(Name = "X")]
        public int ArchScore2_X_6 { get; set; }

        [Display(Name = "10")]
        public int ArchScore2_10_5 { get; set; }
        [Required]
        [Display(Name = "Total")]
        public int ArchTotalScore { get; set; }

        [Display(Name = "Total X")]
        public int ArchTotalScore_X_6 { get; set; }

        [Display(Name = "Total 10")]
        public int ArchTotalScore_10_5 { get; set; }

        public List<Participe> liste { get; set; }
    }
   }

这是我的控制器:

namespace ArcheryComp.Controllers
   {
    public class ParticipeController : Controller
    {
        private ArcheryCompEntities db = new ArcheryCompEntities();
       ......
       [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult EncodeResult(IList<Participe> parti)

        {

            foreach (Participe item in parti)
            {


                var data = from part in db.Participe
                           where part.IdArcher == item.IdArcher &&  part.IdTournament == item.IdTournament
                           select part;                    

                item.ArchScore1 = item.ArchScore1;
                item.ArchScore2 = item.ArchScore2;
                item.ArchTotalScore = item.ArchTotalScore;
            }

这是我的观点:

    @model List<ArcheryComp.Participe>

   @{
    ViewBag.Title = "EncodeResult";
   }


    <h2>Encoder Resultats</h2>


    @using (Html.BeginForm("EncodeResult","Participe",FormMethod.Post)) 
   {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(false)
    <table>
        <tr>
            <th>
                @Html.LabelFor(model => Model[0].Archers.Nom)
            </th>
            <th>
                @Html.LabelFor(model => Model[0].Archers.Prenom)
            </th>
            <th>
                @Html.LabelFor(model => Model[0].Divisions.DivDescription)
            </th>
            <th>
                @Html.LabelFor(model => Model[0].Categorie)
            </th>
            <th>
                @Html.LabelFor(model => Model[0].ArchScore1, new {style =  "width: 10px;"})
            </th>
            <th>
                @Html.LabelFor(model => Model[0].ArchScore2, new {style = "width: 10px;"})
            </th>
            <th>
                @Html.LabelFor(model => Model[0].ArchTotalScore, new {style = "width: 10px;"})
            </th>
        </tr>
        @for(int i = 0; i < Model.Count() ; i++)
        {
            <tr>
                <td>                                        
                    @Html.TextBox("archers["+@i+"].IdArcher",  Model[i].Archers.Nom)
                    @Html.ValidationMessageFor(x => x[i].IdArcher)
                </td>
                <td> 
                    @Html.TextBox("archers["+@i+"].Prenom", Model[i].Archers.Prenom)
                </td> 
                 <td>
                     @Html.TextBox("archers["+@i+"].Division", Model[i].Divisions.DivDescription)
                     @Html.ValidationMessageFor(x => x[i].Divisions.Id)

                </td> 
                <td>
                     @Html.TextBox("archers["+@i+"].Categorie", Model[i].Categorie)
                    @Html.ValidationMessageFor(x => x[i].Categorie)
                </td>
                 <td> 
                    @Html.TextBox("suma["+@i+"]", Model[i].ArchScore1,  new{  @onchange = "updatesum()"})
                    @Html.ValidationMessageFor(x => x[i].ArchScore1)
                </td> 
                <td>>
                    @Html.TextBox("sumb["+@i+"]", Model[i].ArchScore2, new { @onchange = "updatesum()" })
                    @Html.ValidationMessageFor(x => x[i].ArchScore2)
                </td>
                <td> 
                    @Html.TextBox("sumt["+@i+"]", Model[i].ArchTotalScore, new { @onchange = "updatesum()" })
                    @Html.ValidationMessageFor(x => x[i].ArchTotalScore)
                </td> 
            </tr>
        }

    </table>
    <p>
        <input type="submit" value="Save" />
    </p>
   }

你能帮忙解决这个问题吗? 非常感谢提前!!!

【问题讨论】:

  • 您的操作方法是否从客户端接收列表?如果可以,请分享发送列表的客户端代码
  • 您无法以简单形式的帖子接收列表 之类的收藏。您必须编写 jquery/javascript 代码,在客户端准备集合对象并将其发送到服务器。下面可以帮到你webcognoscere.com/post/…
  • @Tanzeel,你当然可以发布收藏!

标签: c# asp.net asp.net-mvc asp.net-mvc-4 razor


【解决方案1】:

您使用@Html.TextBox() 为输入指定一个与您的模型属性没有任何关系的名称,这意味着您在提交时无法绑定到集合。

例如,您有一个属性string categorie,这意味着输入的名称需要为name="[0].categorie,但您使用name="archers[0].Categorie“创建输入。始终使用强类型的html帮助器(就像您有用ValidationMessageFor()完成

@Html.TextBoxFor(x => x[i].Categorie)
@Html.ValidationMessageFor(x => x[i].Categorie)

旁注:在您的表格标题中应该是

<td>@Html.DisplayNameFor(m => m.Categorie)</td>

不是@Html.LabelFor()。 &lt;label&gt; 是一个 html 可访问性元素 - 单击它会将焦点设置为关联的控件,在这种情况下,这没有任何意义,因为您有一个包含每个属性的多个控件的表。

【讨论】:

  • 我不认为简单的表单发布/提交可以在服务器端作为列表接收。他需要以参与者列表的形式提交的视图。
  • 当然可以。只要正确命名表单控件,您就可以绑定到任何东西。在这种情况下是List&lt;Participe&gt;,因此控件需要是name="[0].Categorie"、name="[1].Categorie"、name="[2].Categorie" 等(其他属性名称也是如此)
  • 谢谢。明白了。
【解决方案2】:

我知道了,它可以工作了,谢谢 :) 尽管如此,我仍然有一个小问题,我使用以下 (updatesum()) javascript 在字段中动态计算我认为射手得分的总和:

<td> 
                    @Html.TextBox("suma["+@i+"]", Model[i].ArchScore1,  new{ @onchange = "updatesum()"})
                    @Html.ValidationMessageFor(x => x[i].ArchScore1)
                </td> 
                <td>>
                    @Html.TextBox("sumb["+@i+"]", Model[i].ArchScore2, new { @onchange = "updatesum()" })
                    @Html.ValidationMessageFor(x => x[i].ArchScore2)
                </td>
                <td> 
                    @Html.TextBox("sumt["+@i+"]", Model[i].ArchTot`enter code here`alScore, new { @onchange = "updatesum()" })
                    @Html.TextBoxFor(x=>x[i].ArchTotalScore)
                    @Html.ValidationMessageFor(x => x[i].ArchTotalScore)
                </td> 

<script type="text/javascript">
        function updatesum() {
            for (i = 0; i < 15; i++) {
                var sua = "suma_" + i + "_";
                var sub = "sumb_" + i + "_";
                var sut = "sumt_" + i + "_";
                suma = document.getElementById(sua).value;
                sumb = document.getElementById(sub).value;
                sum = (suma - 0) + (sumb - 0);
                document.getElementById(sut).value = sum;
            }

        }

    </script>

你知道将这个javascript函数的结果添加到TextBoxFor中是否可行吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多