MVC 4将列表从视图传递到控制器 [英] MVC 4 Passing a list from a view to a controller

查看:71
本文介绍了MVC 4将列表从视图传递到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new to MVC ASP and would need your assistance. I would like to pass a list from a view to controller but each time I submit my form, the list is empty (null) in the controller.

这是我的模特:

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

您能帮忙解决一下吗? 提前非常感谢!!!

Can you please help sort this out ? Many thanks in advance !!!

推荐答案

使用@Html.TextBox()时,您给输入的名称与模型属性没有任何关系,这意味着您不能绑定到集合提交.

Your use of @Html.TextBox() where you give the input a name that has no relationship whatsoever to your model properties and means that you cannot bind to a collection when you submit.

例如,您有一个属性string categorie,这意味着输入的名称需要为name="[0].categorie,但是您要使用name="archers[0].Categorie创建输入.".请始终使用强类型的html helper(因为用ValidationMessageFor()

For example you have a property string categorie which means that the name of the input would need to be name="[0].categorie, yet you create the input with name="archers[0].Categorie". Always use the strongly typed html helper (as you have done with ValidationMessageFor()

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

侧面说明:在表格标题中应该是

Side note: In your table headers it should be

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

不是@Html.LabelFor(). <label>是html可访问性元素-单击它可以将焦点设置到关联的控件上,在这种情况下,这是没有意义的,因为您有一个表,其中每个表都包含多个控件.

not @Html.LabelFor(). A <label> is a html accessibility element - clicking on it sets focus to the associated control, which in this case makes no sense since you have a table containing multiple controls for each property.

这篇关于MVC 4将列表从视图传递到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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