【发布时间】:2014-12-18 22:27:37
【问题描述】:
在理解如何使用表单创建和编辑字符串集合时遇到了一些困难。我尝试过使用 EditorFor,但似乎没有运气,而是将以下文本放入表单中。我正在尝试编辑集合“关键字”。
System.Collections.Generic.HashSet`1[MVCModuleStarter.Models.Module]System.Collections.Generic.HashSet`1[MVCModuleStarter.Models.Module]
这是我在其中使用 EditorFor 的 Html,其中一个正在工作的 EditorFor 用于字符串以供参考。
<div class="form-group">
@Html.LabelFor(model => model.Category, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Category, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Category, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Keywords, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Keywords, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Keywords, "", new { @class = "text-danger" })
</div>
</div>
这是我控制器中的 Edit 方法;
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "ModuleId,ModuleTitle,ModuleLeader,ModuleDescription,ImageURL,Category,Keywords")] Module module)
{
if (ModelState.IsValid)
{
int moduleId = module.ModuleId;
repository.UpdateModule(module);
repository.Save();
return RedirectToAction("Details", new { Id = moduleId });
}
return View(module);
}
这是模型供参考;
[Required, StringLength(20), Display(Name = "Category")]
public string Category { get; set; }
public virtual ICollection<Keyword> Keywords { get; set; }
关键字模型
public class Keyword
{
[Key, Display(Name = "ID")]
public int KeywordId { get; set; }
[Required, StringLength(100), Display(Name = "Keyword")]
public string KeywordTerm { get; set; }
public virtual ICollection<Module> Modules { get; set; }
}
}
任何帮助都会令人惊叹,对此仍然很陌生!谢谢!
【问题讨论】:
-
您需要为 typeof
Keyword创建一个EditorTemplate或使用for循环来渲染Keyword的每个属性,但如果需要,您需要发布Keyword的模型进一步的帮助 -
@StephenMuecke 刚刚在底部添加了关键字的模型,我想我正确地关注了你,但我们将不胜感激!谢谢!
标签: c# asp.net-mvc asp.net-mvc-3 icollection