ASP.NET MVC 表单集合文本区域

发布于 2024-11-30 09:21:28 字数 1720 浏览 2 评论 0原文

我有一个代表描述字段的文本区域。描述中有逗号,因此当尝试拆分字段的描述时,数据无法正确解析。如何正确获取每一行的描述。

     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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

等你爱我 2024-12-07 09:21:28

尝试使用这个有用的功能

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

try with this useful function

ValueProviderResult Match=FormCollection.GetValue("ValueProvider");
深爱不及久伴 2024-12-07 09:21:28

当您有多个具有相同 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;
    }
}

编辑
我仍然对你的 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>

When you have multiple fields with the same name attribute, they'll come back into your FormCollection as an array. So upon posting a view like this:

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

you could do something like this in your action

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

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

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.

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

EDIT
I'm still assuming a lot about your ViewModel but perhaps try this:

<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>
许久 2024-12-07 09:21:28

你也可以尝试,

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

You could also try ,

   string Match=FormCollection.GetValue("ValueProvider").AttemptedValue;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文