我是MVC4的新开发人员,我想检查数据库中的数据复选框。 我创建了一个模型服务
[Table("Servicos")]
//[Bind(Exclude = "ServicoId")]
public class Servicos
{
public Servicos()
{
TaxasList = new Collection<Taxas>();
}
//[ScaffoldColumn(false)]
[Key]
public int ServicoId { get; set; }
//[Required(ErrorMessage = "Campo obrigatório")]
[DisplayName("Título")]
//[StringLength(100)]
public string Titulo { get; set; }
//[Required(ErrorMessage = "Campo obrigatório")]
[DisplayName("Tempo previsto (horas)")]
public decimal TempoPrevistoHora { get; set; }
//[Required(ErrorMessage = "Campo obrigatório")]
[DisplayName("Tempo previsto (dias)")]
public int TempoPrevistoDias { get; set; }
//[Required(ErrorMessage = "Campo obrigatório")]
[DisplayName("Custo da hora)")]
public decimal CustoHora { get; set; }
//[Required(ErrorMessage = "Campo obrigatório")]
[DisplayName("Custo do serviço")]
public decimal CustoServico { get; set; }
//[ScaffoldColumn(false)]
public decimal TotalTaxas { get; set; }
public ICollection<Taxas> TaxasList { get; set; }
public override bool Equals(object obj)
{
var servParam = (Servicos)obj;
if (ServicoId == servParam.ServicoId || Titulo == servParam.Titulo)
return true;
return false;
}
和税收模式:
[Table("Taxas")]
//[Bind(Exclude = "TaxasId")]
public class Taxas
{
//[ScaffoldColumn(false)]
[Key]
public int TaxasId { get; set; }
//[Required(ErrorMessage = "Campo obrigatório")]
//[DisplayName("Descrição da taxa")]
//[StringLength(100)]
public string DescricaoTaxa { get; set; }
//[Required(ErrorMessage = "Campo obrigatório")]
//[DisplayName("Porcentagem")]
//[Range(0.01, 100.00, ErrorMessage = "Porcentagem deve estar entre 0.01 e 100.00")]
public decimal Porcentagem { get; set; }
//[ScaffoldColumn(false)]
public bool Ativo { get; set; }
//[ScaffoldColumn(false)]
//[DataType(DataType.DateTime)]
public DateTime? DataCriacao { get; set; }
public ICollection<Servicos> Servicos { get; set; }
public override bool Equals(object obj)
{
var taxParam = (Taxas)obj;
return TaxasId == taxParam.TaxasId || DescricaoTaxa == taxParam.DescricaoTaxa;
}
}
我正在使用Fluent API,当项目生成数据库时,它会创建第三个表TaxasServicos
,因为服务(servico)有很多税(Taxas)..
在服务控制器索引中,我显示服务中的所有税款。
public ActionResult Index()
{
var servicos = db.Servicos.Include(s => s.TaxasList);
return View(servicos);
}
在编辑模式下,我传递所有税(Taxas)并使用View
将其传递给ViewData
。
// GET: /Services/Edit/5
public ActionResult Edit(int id = 0)
{
Servicos servicos = db.Servicos.Find(id);
ViewData["TaxasList"] = db.Servicos.Include(t => t.TaxasList);
if (servicos == null)
{
return HttpNotFound();
}
List<Taxas> taxases = (from t in db.Taxas select t).ToList();
ViewData["lstTaxas"] = taxases;
return View(servicos);
}
//
// POST: /Services/Edit/5
[HttpPost]
public ActionResult Edit(Servicos servicos)
{
if (ModelState.IsValid)
{
db.Entry(servicos).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(servicos);
}
直到这里,一切运作良好,但我无法查看与服务(Servico)有关系的复选框。有人可以帮我解决这个问题吗?
我的Edit.cshtml:
@using appSharpNet.Models
@model appSharpNet.Models.Servicos
@{
ViewBag.Title = "Editar";
}
<h2>Edit</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Serviços</legend>
@Html.HiddenFor(model => model.ServicoId)
<div class="editor-label">
@Html.LabelFor(model => model.Titulo)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Titulo)
@Html.ValidationMessageFor(model => model.Titulo)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.TempoPrevistoHora)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.TempoPrevistoHora)
@Html.ValidationMessageFor(model => model.TempoPrevistoHora)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.TempoPrevistoDias)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.TempoPrevistoDias)
@Html.ValidationMessageFor(model => model.TempoPrevistoDias)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.CustoHora)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.CustoHora)
@Html.ValidationMessageFor(model => model.CustoHora)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.CustoServico)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.CustoServico)
@Html.ValidationMessageFor(model => model.CustoServico)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.TotalTaxas)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.TotalTaxas)
@Html.ValidationMessageFor(model => model.TotalTaxas)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.TaxasList)
</div>
<div class="editor-field">
@{
var taxases = ViewData["lstTaxas"] as List<Taxas>;
if (taxases != null)
{
foreach (var taxase in taxases)
{
@Html.CheckBox("check", false, new { value = taxase.TaxasId });<label>@taxase.DescricaoTaxa</label>
}
}
}
</div>
<p>
<input type="submit" value="Salvar" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
答案 0 :(得分:1)
好的,我找到了解决方案。
有兴趣使用流畅的API学习MVC4,我建议您查看这篇文章The Contoso University by Tom Dykstra。
我需要在项目中进行一些更改以解决我的问题,我在第3部分找到了Contoso大学的解决方案link。