1

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?

5
  • can you post an example of your model and input? Commented Aug 19, 2011 at 19:27
  • There's no reason to be using FormValues['whatever']. You should be submitting the model back to the Controller Commented Aug 20, 2011 at 0:57
  • Can you post what your view looks like? Commented Aug 20, 2011 at 0:57
  • I guess you are giving your users the ability to edit the description of a number of sites in one view/page. And you ran into the collection binding problem, which is a common one for new MVC developers. This could easily be avoided if you simply decide you will only allow the user to edit only one Site at a time. And, IMHO, it will provide much better user experience too. Commented Aug 20, 2011 at 22:13
  • It is a limited list and works for what I am trying to do. And I know that your recommendation is the way it is done but it is not the best approach for what I am doing. Yeah, let's simply edit it in a single page by clicking the edit button on the row because the MVC design cant handle that approach that you want. Commented Aug 21, 2011 at 1:59

3 Answers 3

1

try with this useful function

ValueProviderResult Match=FormCollection.GetValue("ValueProvider");
Sign up to request clarification or add additional context in comments.

Comments

0

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>

2 Comments

How would I do that. It already has the descriptions comma delimited when I do the folowing: foreach (var key in FormValues.Keys) { if (key.ToString() == "Item.Description") { var value = FormValues[key.ToString()]; } }
Oh I see, I thought you meant the user was expected to enter comma delimited data into the textarea. This question is made very difficult by the fact that you haven't included code for your view. Updated my answer with something that may help...?
0

You could also try ,

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.