我得到以下代码:
业务对象控制器.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
}
创建.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>
如您所见,一开始我没有在 Create.cshtml 中定义任何模型,它可以是任何现有类型(来自模型)。
在 Create.cshtml 中,我根据指定类型的属性/属性创建标签和编辑器(文本框)。一切正常,但是在最后单击“创建”后,我从 BusinessObjectController 输入了第二个“创建”方法,我似乎无法访问任何属性值?(来自新创建的对象)
但是,如果我将类型从对象更改为“有效模型类型”——例如“汽车”,它会显示我输入的值:
[HttpPost]
public ActionResult Create(Car entity)
{
   //Can access any propertyvalues, but its not dynamic!
}
而且我动态需要它。我怎样才能获得这些属性值?还是我需要尝试以某种方式从 HTML-Response 中获取它们?有什么好办法,求大神帮忙。。