【发布时间】:2017-11-05 03:13:44
【问题描述】:
我对 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; }
}
视图模型:
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>
}
现在用户必须为每种技术或语言打分在测试人员中,我必须将分数与技术和测试人员联系起来,但我不知道如何获得文本框的值以及帖子上的这个值是哪种技术的值:D
【问题讨论】:
-
你想在这里编辑什么?您的视图根本没有意义,并且不会绑定到与您的模型相关的任何内容。在使用
HtmlHelper方法生成表单控件时,您绝对不会更改name属性。 -
而且您不能使用
foreach循环为集合项生成表单控件(请参阅this answer 以获得解释) -
我想从用户那里为每种技术获取很多价值,我会将它们作为 Scor 保存到数据库中,例如:user1 的 Css 能力是 3 点
-
那么您希望用户为每项技术输入一个分数吗?还有两种不同类型的技术(tech 和 lang)?
-
是的,我正在寻找 10 小时,但我找不到任何我无法匹配值的东西,如果它是 C# 形式,我可以让它变得非常容易,但网络的性质对我来说是不同的:D
标签: asp.net-mvc post ef-code-first many-to-many