2017-08-12 22 views
-1

我想添加一个多行输入或脚本的帮助添加几个输入提交他们作为列表<对象>控制器。如果我使用TextBoxFor我得到计数​​= 0在此列表的控制器和null如果我用户EditorFor。 据我了解,现在我只是形成一个对象,而不是一个对象列表,这就是为什么它为空或计数= 0 ...但如何做到这一点?TextBoxFor与列表<object>总是返回空值到控制器

检视:

<div> 
    @using (Html.BeginForm()) 
    { 
     //other form-groups 

     <div class="form-group"> 
      @Html.LabelFor(m => @Model.MediaRSSURL) 
      @Html.TextBoxFor(m => @Model.MediaRSSURL, "URL/URL", new {htmlAttributes = new {@class = "form-control"}}) 
     </div> 

     <input type="submit" value="Submit"/> 
    } 
</div> 

类具有列表<对象>:

public class Media 
{ 
    //other properties 

    public List<URL> MediaRSSURL { get; set; } 
} 

类对象的:

public class URL 
{ 
    [Key] 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public int Id { get; set; } 
    public string strURL { get; set; } 
    public int MediaId { get; set; } 
    public Media Media { get; set; } 

} 

EditorTemplate:

驻留在共享/ EditorTemplates/URL

@model Domain.Entities.URL 

<div class="form-group"> 
    @Html.TextBoxFor(m => Model.strURL, new {htmlAttributes = new {@class = "form-control"}}) 
    @Html.HiddenFor(m => Model.Id) 
    @Html.HiddenFor(m => Model.MediaId) 
</div> 

控制器POST方法:

 [System.Web.Mvc.HttpPost] 
    public RedirectToRouteResult AddMedia([FromBody] Media media) 
    { 

     if (ModelState.IsValid) 
     { 
      _iMediaRepository.SaveMedia(media); 
     } 

     return RedirectToAction("GetAllMedia"); 
    } 

控制器GET方法:

 [System.Web.Mvc.HttpGet] 
    public ViewResult AddMedia() 
    { 

     return View(); 
    } 

UPDATE:


填充的收集和传递模型查看:

 [System.Web.Mvc.HttpGet] 
    public ViewResult AddMedia() 
    { 
     var model = new Media(); 
     var newUrl = new URL(); 
     model.MediaRSSURL = new List<URL>(); 
     model.MediaRSSURL.Add(newUrl); 

     return View(model); 
    } 

改变了编辑模板:

@model IEnumerable<Domain.Entities.URL> 

<div class="form-group"> 

    @foreach (URL url in Model) 
    { 
     @Html.EditorFor(m => url.strURL, new { htmlAttributes = new { @class = "form-control" } }) 
     @Html.HiddenFor(m => url.Id) 
     @Html.HiddenFor(m => url.MediaId) 
     @Html.HiddenFor(m => url.Media) 
    } 

</div> 
+0

你不能一个文本框绑定到复杂对象的集合。你有一个'EditorTemplate',所以使用'@ Html.EditorFor(m => m.MediaRSSURL)',它将根据模板为你的集合中的每个对象生成html。 –

+0

@StephenMuecke我试过了,如果使用EdirorFor –

+0

,我会得到null然后这意味着你没有收集任何物品!显示你的GET方法,以及如何填充集合并将其传递给视图 –

回答

-1

你不能这样工作。因为MediaRSSURL以List的形式返回,但是您将其用作textboxfor。如果你可以使用它作为列表框,它将起作用,或者你应该将它作为字符串返回。所以,这种方式

@model List<Domain.Entities.URL> 

@foreach(在模型项)

@ Html.TextBoxFor(M => item.strURL,新的{htmlAttributes =新{@class = “形式控制”}} ) @ Html.HiddenFor(M => item.Id) @ Html.HiddenFor(M => item.MediaId)
+0

你可能想阅读[这个答案](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) –

0

SOLUTION:

感谢@ stephen-muecke和他的建议,我设法使它工作!

查看页:

一个不错的article懂得如何处理复杂的类型。从上面的链接中得到的article也帮助我确定了我的一个错误。 动态地添加我使用的技术从this post输入(选项2)

@model Media 

<div> 
    @using (Html.BeginForm()) 
    {  
     <div class="form-group"> 
      @Html.LabelFor(m => m.MediaRSSURL) 

      <div id="URLContainer"> 
       @Html.EditorFor(m => m.MediaRSSURL) 
      </div> 
      <div id="URLfield" style="display: none"> 
       <!--You need to look in HTML how is your input formed and just copy it to make a fake input. Then replace the numbers in attributes by hashtag. For example name="MediaRSSURL[0].strURL" to name="MediaRSSURL[#].strURL"--> 
       <div class="form-group"> 
        <input class="form-control text-box single-line" id="MediaRSSURL_#__strURL" name="MediaRSSURL[#].strURL" type="text" value="" data-cip-id="MediaRSSURL_#__strURL"> 
       </div> 

      </div> 

     </div> 
     <div> 
      <input type="button" value="Add URL" id="addURL" onclick="addURLs()"/> 
     </div> 

     <input type="submit" value="Submit"/> 
    } 
</div> 

@section scripts 
{ 

    <script> 
     //On click you clone the fake input and replace all # signs to a number from counter and append new input to your container 
     var counter = 1; 
     $('#addURL') 
      .click(function() { 
       var clone = $('#URLfield').clone(); 
       // Update the indexer and Index value of the clone 
       clone.html($(clone).html().replace(/#/g, counter)); 
       clone.html($(clone).html().replace(/"%"/g, '"' + counter + '"')); 
       $('#URLContainer').append(clone.html()); 
       counter++; 
      }); 

    </script> 
} 

类具有列表<对象>:

public class Media 
{ 
    // Other properties 

    public List<URL> MediaRSSURL { get; set; } 
} 

对象的类:

没有变化

编辑模板:

这里是我犯了一个错误,并花了几个小时去理解它: 第一模板放置在浏览/共享/ EditorTemplates/URL/URL ,我需要它有这样的位置:浏览次数/ NameOfYourController/EditorTemplates/URL 在我更改了编辑模板的位置之后,在HTML中开始生成诸如name =“MediaRSSURL [0] .strURL”等属性,让我知道最后来自输入的数据会到达列表。此外,我不必在EditorFor不在模型中指定模板地址(通过[UIHint(“URL”)]) - 它没有这些地址。

@model Domain.Entities.URL 

<div class="form-group"> 
     @Html.EditorFor(m => m.strURL, new { htmlAttributes = new { @class = "form-control" } }) 
</div> 

控制器POST方法:

没有大的变化。无需[FromBody],但目前无影响。

控制器GET方法:

这是我做的第二个错误 - 我第一次没有通过模型来查看。 除了我初始化一)模型B)其名单<对象>C)和对象

[System.Web.Mvc.HttpGet] 
public ViewResult AddMedia() 
{ 
    URL newUrl1 = new URL(); 
    var model = new Media(); 
    model.MediaRSSURL = new List<URL>(); 
    model.MediaRSSURL.Add(newUrl1); 

    return View(model); 
} 
相关问题