Asp.net mvc ASP.NET MVC FormCollection文本区域

Asp.net mvc ASP.NET MVC FormCollection文本区域,asp.net-mvc,formcollection,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 t

我有一个表示描述字段的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.
我的视图是这样填充的:

    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>
<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>
if(Model!=null&&Model.Tenant!=null&&Model.Tenant.Site!=null&&Model.Tenant.Site.Count()>0)
{
@LabelFor(item=>item.Site.Title)
@LabelFor(item=>item.Site.Description)
@foreach(Model.Tenant.Sites中的var项)
{
@Html.HiddenFor(modelItem=>Item.SiteId)
@EditorFor(modeleItem=>Item.Title)
@Html.TextAreaFor(modeleItem=>Item.Description,新的{@width=“400”})
}
如您所见,此站点表是租户对象的子记录。此子记录不会使用此方法自动更新,但租户数据会自动更新。这就是我尝试FormCololction的原因。
我是否缺少一些东西来完成这项工作?

当您有多个字段具有相同的
名称
属性时,它们将作为数组返回到您的
FormCollection
中。因此,在发布这样的视图时:

    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>
<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>
我必须注意,这不是处理已发布数据的推荐方法。相反,您应该使用视图模型中的数据构建视图,并使用强类型html帮助程序呈现控件。这样,在发布时,您的操作可以将ViewModel作为参数。它的属性将自动绑定,您将拥有一个很好的玩物

[HttpPost]
public ActionResult MyAction(MyViewModel viewModel) 
{
    foreach (var row in viewModel.Rows)
    {
        string description = row.Description;
    }
}
编辑
我仍然对您的ViewModel有很多假设,但也许可以尝试以下方法:

<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>

@LabelFor(model=>model.Site.Title)
@LabelFor(model=>model.Site.Description)
@对于(var i=imodel.Tenants.Sites[i].SiteId)
@Html.EditorFor(model=>model.Tenants.Sites[i].Title)
@Html.TextAreaFor(model=>model.Tenants.Sites[i].Description,new{@width=“400”})
}

当您有多个字段具有相同的
名称
属性时,它们将作为数组返回到您的
FormCollection
中。因此,在发布如下视图时:

    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>
<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>
我必须注意,这不是处理已发布数据的推荐方法。相反,您应该使用视图模型中的数据构建视图,并使用强类型html帮助程序呈现控件。这样,在发布时,您的操作可以将ViewModel作为参数。它的属性将自动绑定,您将拥有一个很好的玩物

[HttpPost]
public ActionResult MyAction(MyViewModel viewModel) 
{
    foreach (var row in viewModel.Rows)
    {
        string description = row.Description;
    }
}
编辑
我仍然对您的ViewModel有很多假设,但也许可以尝试以下方法:

<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>

@LabelFor(model=>model.Site.Title)
@LabelFor(model=>model.Site.Description)
@对于(var i=imodel.Tenants.Sites[i].SiteId)
@Html.EditorFor(model=>model.Tenants.Sites[i].Title)
@Html.TextAreaFor(model=>model.Tenants.Sites[i].Description,new{@width=“400”})
}

试试这个有用的功能

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

尝试使用这个有用的函数

ValueProviderResult Match=FormCollection.GetValue("ValueProvider");
你也可以试试

   string Match=FormCollection.GetValue("ValueProvider").AttemptedValue;
你也可以试试

   string Match=FormCollection.GetValue("ValueProvider").AttemptedValue;

你能发布一个模型和输入的例子吗?没有理由使用
FormValues['whatever']
。你应该将模型提交回控制器。你可以发布你的视图吗?谢谢。我刚刚更新了我的问题。我想你是在给你的用户在一个视图/页面中编辑多个站点的描述的能力。你遇到了集合绑定问题,这是新MVC开发人员常见的问题。这如果你决定一次只允许用户编辑一个站点,那么很容易避免。而且,依我看,这也会提供更好的用户体验。你能发布一个模型和输入的示例吗?没有理由使用
FormValues['which']
。你应该将模型提交回控制器。你可以发布你的视图吗?谢谢。我刚刚更新了我的问题。我想你是在给你的用户在一个视图/页面中编辑多个站点的描述的能力。你遇到了集合绑定问题,这是新MVC开发人员常见的问题。这如果你决定一次只允许用户编辑一个站点,那么这很容易避免。而且,依我看,它也会提供更好的用户体验。我该怎么做呢?当我执行以下操作时,它已经用逗号分隔了描述:foreach(FormValues.Keys中的var key){if(key.ToString()=“Item.Description”){var value=FormValues[key.ToString()];}哦,我明白了,我以为你的意思是用户应该在文本区域中输入逗号分隔的数据。这个问题变得非常困难,因为你没有为你的视图包含代码。用一些可能有用的东西更新了我的答案…?我该怎么做?在我执行以下操作时,它已经有逗号分隔的描述:foreach(FormValues.Keys中的var key){if(key.ToString()==“Item.Description”){var value=FormValues[key.ToString()];}哦,我明白了,我以为你的意思是用户应该在数据库中输入逗号分隔的数据