1

I have an entity with a variable set of attributes named ExtendedProperty, these have a key and a value.

In my html razor view, I have this:

@if (properties.Count > 0)
{
    <fieldset>
        <legend>Extended Properties</legend>
            <table>
            @foreach (var prop in properties)
            {
                <tr>
                    <td>
                        <label for="[email protected]">@prop.Name</label>
                    </td>
                    <td>
                        <input type="text" name="[email protected]" 
                               value="@prop.Value"/>
                    </td>
               </tr>
            }
            </table>
        </fieldset>
}

How can I access this data on my controller once the user fills it in? Is there a way to do this so that I can use the model bindings instead of manual html?

EDIT = Please note that I'm still using a model, and there are other things in the form that do use things like @Html.EditFor(m => m.prop). But I couldn't find a way to integrate these variable properties in.

Thanks.

7
  • If you name the inputs Properties[0]....Properties[n], the model binder will convert this to an IEnumerable on the model named Properties Commented Jul 8, 2013 at 18:29
  • How can I integrate this with my model class? Commented Jul 8, 2013 at 18:30
  • 1
    @elite5472 Please, take a look at my answer at stackoverflow.com/questions/17450772/…. I believe that it's the same problem. Commented Jul 8, 2013 at 18:37
  • @AndreCalil Thanks, but there is an issue that I also have. I have other fields too which are static. Commented Jul 8, 2013 at 18:43
  • 1
    @elite5472 I don't understand what you mean by static fields. Could you provide more details about your model? Also, note that you can use the notation Property[n].Name and Property[n].Value Commented Jul 8, 2013 at 18:47

2 Answers 2

5

Have you tried using the FormCollection object passed to the controller method?

[HttpPost]
public ActionResult Index(FormCollection formCollection)
{
  foreach (string extendedProperty in formCollection)
  {
     if (extendedProperty.Contains("Property-"))
     {
       string extendedPropertyValue = formCollection[extendedProperty];
     }
  }

  ...
}

I would try traversing through the items in that collection.

Sign up to request clarification or add additional context in comments.

Comments

2

Let's suppose you have the following Model (ViewModel, I prefer):

public class ExtendedProperties
{
  public string Name { get; set; }
  public string Value { get; set; }

}

public class MyModel
{
  public ExtendedProperties[] Properties { get; set; }
  public string Name { get; set; }
  public int Id { get; set; }
}

You can bind this model to a view using a markup like:

@using (Html.BeginForm("YourAction", "YourController", FormMethod.Post))
{
  <input type="text" name="Name" />
  <input type="number" name="Id" />

  <input type="text" name="Properties[0].Name" />
  <input type="text" name="Properties[0].Value" />  
  ...
  <input type="text" name="Properties[n].Name" />
  <input type="text" name="Properties[n].Value" />  
}

Finally, your action:

[HttpPost]
public ActionResult YourAction(MyModel model)
{
  //simply retrieve model.Properties[0]
  //...
}

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.