2011-08-19 132 views
1

我有一个代表描述字段的textarea。描述中有逗号,所以当试图拆分字段的描述时,数据没有被正确解析。我怎样才能正确地得到每一行的描述。ASP.NET MVC FormCollection TextArea

 var DescList = FormValues["Item.Description"].Split(',').Select(item => item).ToList<string>(); 
//will not work for obvious reasons. Comma delimited FormCollection has commas to identify separate row data. 

看起来像微软设计的FormsCollection没有考虑textarea控件。尝试访问每个值时,包含逗号的文本区域将不起作用。有趣的是,_entriestables财产拥有完美的格式,但他们选择将其作为私有财产。非常令人沮丧。

`

Here is the important part of my viewmodel. 
public class TenantViewModel 
{ 
    public Tenant Tenant { get; set; } 
       public Site Site { get; set; } 

    } 

我的看法是填充像这样:

if (Model != null && Model.Tenant != null && Model.Tenant.Site != null && Model.Tenant.Site.Count() > 0) 
    {<div class="detailsbox_view"> 
     <table id="tblTenantSites"> 
      <tr> 
       <th>@Html.LabelFor(item => item.Site.Title)</th> 
       <th>@Html.LabelFor(item => item.Site.Description)</th> 

      </tr> 
     @foreach (var Item in Model.Tenant.Sites) 
      { 
      <tr> 
       @Html.HiddenFor(modelItem => Item.SiteId) 


       <td> 
        @Html.EditorFor(modelItem => Item.Title) 
             </td> 
       <td> 
        @Html.TextAreaFor(modelItem => Item.Description, new {@width="400" }) 

       </td> 

      </tr> } 
     </table> 

正如你看到这个网站表是租户对象的孩子。此子记录不会使用此方法自动更新,但Tenant数据会自动更新。这就是我尝试使用FormColelction的原因。 有什么我失踪,使这项工作?

+0

你可以发布你的模型和输入的例子吗? –

+0

没有理由使用FormValues ['whatever']。您应该将模型提交给控制器 –

+0

您可以发布您的视图的样子吗? –

回答

0

当您有多个字段具有相同的name属性时,它们将作为数组返回到您的FormCollection。所以,在发布这样的观点:

<form action="/Home/MyAction"> 
    <textarea id="row_one_description" name="description"> 
     First row's description 
    </textarea> 
    <textarea id="row_two_description" name="description"> 
     Second row's description 
    </textarea> 

    <input type="submit" value="Submit" /> 
</form> 

你可以做这样的事情在你的行动

[HttpPost] 
public ActionResult MyAction(FormCollection collection) 
{ 
    var descriptionArray = collection["description"]; 

    string firstRowDescription = descriptionArray[0]; 
    string secondRowDescription = descriptionArray[1]; 
} 

我必须指出,这是不是在处理数据发布的推荐方式。您应该使用视图模型中的数据构建视图,并使用强类型html助手来呈现控件。这样,当你发布时,你的动作可以将ViewModel作为参数。它的属性会自动绑定,你将有一个很好的对象来玩。

[HttpPost] 
public ActionResult MyAction(MyViewModel viewModel) 
{ 
    foreach (var row in viewModel.Rows) 
    { 
     string description = row.Description; 
    } 
} 

编辑
我还假设了很多关于您的视图模型,但也许试试这个:

<table id="tblTenantSites"> 
    <tr> 
     <th>@Html.LabelFor(model => model.Site.Title)</th> 
     <th>@Html.LabelFor(model => model.Site.Description)</th> 
    </tr> 

    @for (var i = i < Model.Tenants.Sites.Count(); i++) { 
    <tr> 
     @Html.HiddenFor(model => model.Tenants.Sites[i].SiteId) 

     <td> 
      @Html.EditorFor(model => model.Tenants.Sites[i].Title) 
     </td> 
     <td> 
      @Html.TextAreaFor(model => model.Tenants.Sites[i].Description, new { @width="400" }) 
     </td> 

    </tr> 
    } 

</table> 
+0

我该怎么做。它已经有描述逗号分隔时,我做以下:foreach(var键在FormValues.Keys) { if(key.ToString()==“Item.Description”) { var value = FormValues [key。的ToString()]; } } – Keith

+0

哦,我明白了,我以为你的意思是用户需要输入逗号分隔的数据到textarea。由于您未包含您的视图的代码,因此这个问题变得非常困难。更新我的答案,可能有助于...? – ajbeaven

+0

有关更多详细信息,请参阅更新后的问题。谢谢 – Keith

1

尝试用这个实用的功能

ValueProviderResult Match=FormCollection.GetValue("ValueProvider"); 
0

您也可以尝试,

string Match=FormCollection.GetValue("ValueProvider").AttemptedValue; 
相关问题