0

I got following code:

BusinessObjectController.cs:

public ActionResult Create(string name)
{
    var type = viewModel.GetTypeByClassName(name);
    return View(Activator.CreateInstance(type));
}

[HttpPost]
public ActionResult Create(object entity)
{
   //Can't access propertyvalues
}

Create.cshtml:

   @{
        ViewBag.Title = "Create";
        List<string> attributes = new List<string>();
        int propertiesCount = 0;
        foreach (var property in Model.GetType().GetProperties())
        {
            //if (property.Name != "Id")
            //{
            //    attributes.Add(property.Name);
            //}
        }
        propertiesCount = Model.GetType().GetProperties().Length - 1; //-1 wegen Id 
    }

    <h2>Create</h2>

    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript">     </script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

    @using (Html.BeginForm()) {
        @Html.ValidationSummary(true)
        <fieldset>

        <legend>@Model.GetType().Name</legend>

        @for (int i = 0; i < propertiesCount; i++)
        {
            <div class="editor-label">
                @Html.Label(attributes[i])
            </div>

            <div class="editor-field">
                @Html.Editor(attributes[i])
                @Html.ValidationMessage(attributes[i])
            </div>
        }
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

As you see, I'm not defining any model in my Create.cshtml at the beginning, it can be any existing type (from the model).

In the Create.cshtml I'm creating the Labels and Editors (Textboxes), based on the attributes/properties of the specified type. Everything works fine, but after I click "Create" at the end, I enter the second "Create" Method from my BusinessObjectController and I don't seem to be able, to access any propertyvalues? (from the newly created object)

But if I change the type from object to by example a "valid modeltype" - for example "Car", it shows me the Values i typed in:

[HttpPost]
public ActionResult Create(Car entity)
{
   //Can access any propertyvalues, but its not dynamic!
}

And I need it dynamically.. How can I get these propertyvalues? Or do I need to try to get them from the HTML-Response somehow? Whats the best way, please help..

eMi
  • 5,540
  • 10
  • 60
  • 109
  • Looks like you need to write a custom model binder. I have illustrated this in the following post: http://stackoverflow.com/a/6485552/29407 – Darin Dimitrov Jul 10 '13 at 14:57
  • The "best" way? Honestly, I've never seen this sort of binding by the _name_ of a type instead of the type itself. I suppose you could *cast* the `object` to an instance of `Car`. I assume it can be instances of other objects as well, though? Perhaps you can have the instances all implement an interface which contains the name of the type of the instance, then cast first to that interface to get the name and cast to the type of that name. It's messy, though. You could manually iterate through a `FormCollection` argument to get the POSTed values instead. Maybe less messy? Hard to tell. – David Jul 10 '13 at 14:59

1 Answers1

2

Try using a FormCollection

public ActionResult Create(FormCollection collection)
{
   //Can access any propertyvalues, but its not dynamic!
}

You can then access the values with something like this

    string s = string.Empty;
    foreach (var key in collection.AllKeys) {
        s += key + " : " + collection.Get(key) + ", ";
    }
Mirsha
  • 118
  • 5
  • by iterating through this collection, I'm only getting the "Propertynames" instead of the Values.. seems to be hidden somewhere.. – eMi Jul 10 '13 at 15:04
  • I've updated the answer to show how you could loop over the keys and access the values. – Mirsha Jul 10 '13 at 15:28