2017-07-08 67 views
2

找到了解决办法,而是一个非常难看的一个,看到在底部,有人可以改善这个解决方案在MVC,而“创造”到模型?动态添加ICollection的项目

所以我有一个假人模型像这样的

public class TestModel 
{ 
    public int TestModelID { get; set; } 
    public string Name { get; set; } 
} 

而另一个像这样的

public class Collector 
{ 
    public int CollectorID { get; set; } 
    public string CollectorString { get; set; } 
    public ICollection<TestModel> MyList { get; set; } 
} 

我想(在简单的CRUD风格)创建一个新的对象收藏家和填充(后来与动态添加新的领域,现在只有一个)ICollection中。 这是我的看法

@model TestApplication.Models.Collector 

@{ 
    ViewBag.Title = "Create"; 
} 

<h2>Create</h2> 


@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken() 

    <div class="form-horizontal"> 
     <h4>Collector</h4> 
     <hr /> 
     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
     <div class="form-group"> 
      @Html.LabelFor(model => model.CollectorString, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.CollectorString, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.CollectorString, "", new { @class = "text-danger" }) 
      </div> 
     </div> 


     <div class="form-group"> 
      @Html.LabelFor(model => Model.MyList.ToList()[0].Name) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.MyList.ToList()[0].Name, new { htmlAttributes = new { @class = "form-control" } }) 


      </div> 
     </div> 
     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" value="Create" class="btn btn-default" /> 
      </div> 
     </div> 
    </div> 
} 

<div> 
    @Html.ActionLink("Back to List", "Index") 
</div> 

@section Scripts { 
    @Scripts.Render("~/bundles/jqueryval") 
} 

而且控制器

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Create(Collector collector) 
{ 
    if (ModelState.IsValid) 
    { 
     db.Collectors.Add(collector); 
     db.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 

    return View(collector); 
} 

这是生成的HTML代码(只有我希望有关部分)

<div class="form-group"> 
    <label for="">Name</label> 
    <div class="col-md-10"> 
     <input class="form-control text-box single-line" name="[0].Name" type="text" value="" /> 
    </div> 
</div> 

然而,MyList控制器时创建总是空的,为什么? (是的,我知道Haackeds博客文章,仍然无法弄清楚,为什么它不工作)

所以问题是,这条线

@Html.EditorFor(model => model.MyList.ToList()[0].Name, new { htmlAttributes = new { @class = "form-control" } }) 

虽然很推荐MVC使用,这产生这里

<input class="form-control text-box single-line" name="[0].Name" type="text" value="" /> 

这显然不工作。我如何获得剃刀改变[0].NameMyList[0].Name

**更新:** 所以我找到了解决办法,如果硬编码这个位置

<input class="form-control text-box single-line" name="MyList[0].Name" type="text" value="" /> 

控制器理解它,我不明白null。如何用剃刀去解决呢?

+0

你'Collector'应类包含一个构造函数和初始化'MyList'成为'MYLIST =新名单();' – Monah

+0

@RobertStettler是否考虑将ICollection更改为IList,以便您可以使用索引器。 ICollection在IList中没有索引器属性。我提供了一个解释的答案。 – Nkosi

回答

1

ICollection<T>没有一个索引。IList<T>确实

public class Collector { 
    public Collector() { 
     MyList = new List<TestModel>(); 
    } 
    public int CollectorID { get; set; } 
    public string CollectorString { get; set; } 
    public IList<TestModel> MyList { get; set; } 
} 

这将允许

@Html.EditorFor(model => model.MyList[0].Name, new { htmlAttributes = new { @class = "form-control" } }) 

在视图中以产生期望的标记

<input class="form-control text-box single-line" name="MyList[0].Name" type="text" value="" /> 

其也可以在一个循环中用于多个项

<div class="form-group"> 
@for(int i = 0; i < Model.MyList.Count; i++) { 
    @Html.LabelFor(model => Model.MyList[i].Name, htmlAttributes: new { @class = "control-label col-md-2" }) 
    <div class="col-md-10"> 
     @Html.EditorFor(model => model.MyList.ToList()[i].Name, new { htmlAttributes = new { @class = "form-control" } }) 
    </div> 
} 
</div> 

当从控制器初始调用中返回模型时,只要确保对mdoel的收集索引的调用具有一个项目即可。

[HttpGet] 
public ActionResult Create() { 
    var model = new Collector(); 
    model.MyList.Add(new TestModel()); 
    return View(model); 
} 
+0

完美,谢谢,完美的作品 – rst

0

试着改变你的收集类,包括初始化。

public class Collector 
    { 
    public Collector() 
    { 
     set MyList = new Collection<TestModel>(); 
    } 
    public int CollectorID { get; set; } 
    public string CollectorString { get; set; } 
    public ICollection<TestModel> MyList { get; set; } 
    } 
+0

这段代码无法建立,你的意思是删除'set'?我得到错误*类型或名称空间集无法找到*。即使它没有工作 – rst

0

您可以通过使用此语法剃刀做

<div class="form-group"> 
    @for(int i = 0; i < Model.MyList.Count; i++) { 
     @Html.LabelFor(model => Model.MyList[i].Name, htmlAttributes: new {  @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
     @Html.EditorFor(model => model.MyList[i].Name, new { htmlAttributes = new { @class = "form-control" } }) 
     </div> 
    } 
</div>