我怎么能拿很多文本框' MVC中Post的值

时间:2017-11-05 03:13:44

标签: asp.net-mvc post ef-code-first many-to-many

我有一个关于MVC的问题,但首先我很抱歉我的英语:D。 现在我正在尝试为用户创建一个表单,当我想要使用数据库连接到值时,我遇到了一个关键问题。

我的表格是这样的:https://i.hizliresim.com/vJ6r2p.png

模特:

[Table("Testers")]
    public class Testers
    {
        [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int ID { get; set; }

        [StringLength(50),Required]
        public string testerName { get; set; }

        public ICollection<Scores> Scores { get; set; }
    }
    [Table("Technologies")]
        public class Technologies
        {
            [Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
            public int ID { get; set; }
            [StringLength(50)]
            public string technologyName { get; set; }
            [StringLength(50)]
            public string type { get; set; }
        }
    [Table("Scores")]
    public class Scores
    {
        [Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int ID { get; set; }
        [DefaultValue(0)]
        public int score { get; set; }

        public virtual Testers tester { get; set; }
        public virtual Technologies technology { get; set; }
    }

的ViewModels:

  public class TechnologiesView
    {
        public List<Technologies> Technologies { get; set; }
        public Scores Scores { get; set; }
    }

控制器:

public ActionResult Page2()
        {
            TechnologiesView allTechs = new TechnologiesView();
            allTechs.Technologies = db.Technologies.ToList();
            return View(allTechs);
        }

查看:

@model TechnologiesView
@{
    ViewBag.Title = "Page2";
}
<style>
    #lang {
        font-size: 15px;
        color: gray;
    }

    #tech {
        font-size: 13px;
        color: gray;
    }
</style>

<div class="container">
    <div class="row col-xs-12 bilgi" style="color:black">
        @HelperMethods.Title("Kendini Skorla!")
        <br />
        <i>Bilgi Düzeyini 0 ile 5 puan arasında notlar mısın? (0=Hiç 5= İleri Seviye)</i>
    </div>
</div>

<hr />
@using (Html.BeginForm())
{
    <div class="container-fluid" style="padding-left:50px; margin:0px">
        <div class="row" id="lang">

            @foreach (Technologies techs in Model.Technologies)
            {
                if (techs.type == "lang")
                {
                    <div class="col-md-1 col-sm-2 col-xs-6">
                        @(techs.technologyName)
                    </div>
                    <div class="col-md-1 col-sm-2 col-xs-6">
                        (@(Html.TextBoxFor(x => x.Scores.score, new
                     {
                         id = techs.ID,
                         name = "techID",
                         style = "display:inline; width:20px; height:20px; font-size:smaller; padding:0px; text-align:center",
                         @class = "form-control"
                     })))
                    </div>
                }
            }
        </div>
        <hr style="color:black" />
        <div class="row" id="tech">
            @foreach (Technologies techs in Model.Technologies)
            {
                if (techs.type == "tech")
                {
                    <div class="col-md-1 col-sm-2 col-xs-6" id="tech">
                        @(techs.technologyName)
                    </div>
                    <div class="col-md-1 col-sm-2 col-xs-6">
                        @Html.HiddenFor(x=>techs.ID)
                        (@(Html.TextBoxFor(x => x.Scores.score, new
                     {
                         id = techs.ID,
                         name = "techID",
                         style = "display:inline; width:20px; height:20px; font-size:smaller; padding:0px; text-align:center",
                         @class = "form-control"
                     })))
                    </div>
                }
            }
        </div>
        <hr />
        <div class="row col-xs-12" id="lang">
            <span>Kullandığınız IDE’ler (yazınız)</span>
            <br />
            <div style="margin-bottom:10px; text-align:center">
                @HelperMethods.TextArea("Ide", 3)
            </div>
        </div>
        <div style="text-align:right; margin-bottom:10px">
            @HelperMethods.Button("btnPage2")
        </div>
    </div>
}

现在,用户必须为每种技术或语言给自己一个分数,然后我想在用户点击按钮时#34;按照下一页(它的土耳其语)&#34;我将从测试人员中的maxID值中选择最后保存的用户,我必须将技术和测试人员的分数连接起来,但我不知道如何获得文本框&#39;价值和技术的价值是帖子上的这个值:D

1 个答案:

答案 0 :(得分:1)

您生成的表单控件与您的模型完全没有关系(无论如何也是错误的)。使用name方法时,永远不要尝试更改HtmlHelper属性(并且没有理由更改id属性)

接下来,您无法使用foreach循环为集合生成表单控件。您需要for循环或EditorTemplate才能使用索引器生成正确的name属性。有关详细说明,请参阅this answer

然后你不能在循环中使用if块(除非你为集合索引器包含一个隐藏的输入),因为默认情况下DefaultModelBinder所需的集合索引器从零开始并且是连续的。

首先创建视图模型,以表示您希望在视图中显示/编辑的内容。

public class ScoreVM
{
    public int ID { get; set; }
    public string Name { get; set; }
    public int Score { get; set; }
}
public class TechnologiesVM
{
    public List<ScoreVM> Languages { get; set; }
    public List<ScoreVM> Technologies { get; set; }
    public string Notes { get; set; } // for your textarea control
}

请注意,您可能希望为[Range]属性添加验证属性,例如Score属性

在GET方法中,初始化并填充视图模型并将其传递给视图

public ActionResult Page2()
{
    IEnumerable<Technologies> technologies = db.Technologies;
    TechnologiesVM model = new TechnologiesVM
    {
        Languages = technologies.Where(x => x.type == "lang")
            .Select(x => new ScoreVM{ ID = x.ID, Name = x.technologyName }).ToList(),
        Technologies = technologies.Where(x => x.type == "tech")
            .Select(x => new ScoreVM{ ID = x.ID, Name = x.technologyName }).ToList(),
    };
    return View(model);
}

并在视图中

@model TechnologiesVM
....
@using (Html.BeginForm())
{
    ....
    @for (int i = 0; i < Model.Languages.Count; i++)
    {
        @Html.HiddenFor(m => m.Languages[i].ID)
        @Html.HiddenFor(m => m.Languages[i].Name)
        @Html.LabelFor(m => m.Languages[i].Score, Model.Languages[i].Name)
        @Html.TextBoxFor(m => m.Languages[i].Score)
        @Html.ValidationMessageFor(m => m.Languages[i].Score)
    }
    @for (int i = 0; i < Model.Languages.Count; i++)
    {
        .... // repeat above
    }
    @Html.LabelFor(m => m.Notes)
    @Html.TextAreaFor(m => m.Notes)
    @Html.ValidationMessageFor(m => m.Notes)

    <input type="submit" />
}

,POST方法将

public ActionResult Page2(TechnologiesVM model)
{
    if (!ModelState.IsValid)
    {
        return View(model);
    }
    ... // save the data and redirect
}