【问题标题】:Creating and editing a collection of strings using MVC3使用 MVC3 创建和编辑字符串集合
【发布时间】: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


【解决方案1】:

你需要为Keyword创建一个EditorTemplate,例如

/Views/Shared/EditorTemplates/Keyword.cshtml 中(根据需要添加 div、类名等)

@model Keyword
@Html.HiddenFor(m => m.KeywordId)
@Html.LabelFor(m => m.KeywordTerm)
@Html.TextBoxFor(m => m.KeywordTerm)
@Html.ValidationMessageFor(m => m.KeywordTerm)

然后在主视图中

Html.EditorFor(m=> m.Keywords)

注意我省略了集合属性Modules,但如果您还想编辑它们,请为Modules添加另一个EditorTemplate

您也可以在主视图中使用for 循环。这意味着集合需要是IList&lt;T&gt;

for(int i = 0; i < Model.Keywords.Count, i++)
{
  @Html.HiddenFor(m => m.Keywords[i].KeywordId)
  // other properties of Keyword
  for (int j = 0; j < Model.Keywords[i].Modules.Count; j++)
  {
    @Html.TextBoxFor(m => m.Keywords[i].Modules[j].SomeProperty)
    // other properties of Module
  }
}

【讨论】:

  • ICollection&lt;T&gt; 没有索引器,所以不确定m.Keywords[i] 应该如何工作(期望这是IList&lt;T&gt;?)
  • @BradChristie,是的,如果使用 for 循环选项,则需要为 IList&lt;T&gt;
猜你喜欢
  • 2012-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多