ASP.NET MVC 表单集合文本区域
我有一个代表描述字段的文本区域。描述中有逗号,因此当尝试拆分字段的描述时,数据无法正确解析。如何正确获取每一行的描述。
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>
如您所见,此站点表是租户对象的子对象。使用此方法不会自动更新此子记录,但租户数据会自动更新。这就是我尝试使用 FormColelction 的原因。 我是否缺少一些东西来使这项工作正常进行?
I have a textarea that represents a description field. The descriptions have commas so when trying to split the field's descriptions the data is not parsed correctly. How can I get each row's description correctly.
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.
It seems like Microsoft designed the FormsCollection without the textarea control in mind. A text area with commas will not work when trying to access each value. What is interesting is that the _entriestables property has it in the perfect format but they chose to make it a private property. Very frustrating.
`
Here is the important part of my viewmodel.
public class TenantViewModel
{
public Tenant Tenant { get; set; }
public Site Site { get; set; }
}
My view is populated like this:
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>
As you see this site table is a child of Tenant object. This child record does not get automatically updated using this method but the Tenant data does automatically get updated. This is the reason I tried the FormColelction instead.
Is there something I am missing to make this work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用这个有用的功能
try with this useful function
当您有多个具有相同
name
属性的字段时,它们将作为数组返回到您的FormCollection
中。因此,在发布这样的视图后:您可以在操作中执行类似的操作,
我必须注意,这不是处理已发布数据的推荐方法。您应该使用视图模型中的数据构建视图,并使用强类型 html 帮助程序来呈现控件。这样,当您发布时,您的操作可以将 ViewModel 作为参数。它的属性将被自动绑定,你将有一个很好的对象可以玩。
编辑
我仍然对你的 ViewModel 有很多假设,但也许可以尝试这个:
When you have multiple fields with the same
name
attribute, they'll come back into yourFormCollection
as an array. So upon posting a view like this:you could do something like this in your action
I must note that this is not the recommended way of dealing with posted data. You should instead be building your view using data from a view model and using strongly typed html helpers to render your controls. That way when you post, your action can take the ViewModel as a parameter. Its properties will be automatically bound and you will have a nice object to play with.
EDIT
I'm still assuming a lot about your ViewModel but perhaps try this:
你也可以尝试,
You could also try ,