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