1

説明フィールドを表すテキストエリアがあります。説明にはカンマが含まれているため、フィールドの説明を分割しようとすると、データが正しく解析されません。各行の説明を正しく取得するにはどうすればよいですか。

     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.

Microsoft は、テキストエリア コントロールを考慮せずに FormsCollection を設計したようです。各値にアクセスしようとすると、コンマを含むテキスト領域は機能しません。興味深いのは、_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 を試した理由です。この作業を行うために欠けているものはありますか?

4

3 に答える 3

1

この便利な機能を試してください

ValueProviderResult Match=FormCollection.GetValue("ValueProvider");
于 2011-10-21T03:38:25.190 に答える
0

あなたも試すことができます、

   string Match=FormCollection.GetValue("ValueProvider").AttemptedValue;
于 2015-09-22T11:54:57.103 に答える
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;
    }
}

EDIT
私はまだあなたの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>
于 2011-08-19T21:31:50.000 に答える