c# - MVC 4 将列表从 View 传递到 Controller

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

我是 MVC ASP 新手,需要您的帮助。 我想将列表从 View 传递到 Controller ,但每次提交表单时, Controller 中的列表都是空的 (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; }
    }
   }

这是我的 Controller :

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

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

最佳答案

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

例如,您有一个属性 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() 。一个<label>是一个 html 可访问性元素 - 单击它会将焦点设置到关联的控件,在这种情况下,这没有任何意义,因为您有一个包含每个属性的多个控件的表。

关于c# - MVC 4 将列表从 View 传递到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30990228/

相关文章:

c# - 用户登录后更改 HttpContext.Current.User.Identity.Name

c# - 如何停止将重复的用户名添加到数据库表中?

c# - 使用 javascript 确认框

asp.net-mvc - 如何在 ASP.NET Controller 中获取 'MvcApplication' 实例?

c# - 在 C# 中解决矩阵问题的库

c# - 在Redis中存储带有DateTime键的对象

c# - 如何将日期时间格式化为 "yyyy-MM-dd"

javascript - RadAjaxNamespace 未定义

asp.net - Session 在 MVC 中实际上是如何工作的?

asp.net-mvc - 如何找出我的 'On-Premises Authority' 网址?