简体   繁体   English

获取Create(HttpPost)中的PropertyValues - 非强类型

[英]Get the PropertyValues inside the Create (HttpPost) - Not strongly typed

I got following code: 我得到以下代码:

BusinessObjectController.cs: 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: 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). 如您所见,我在开始时没有在Create.cshtml中定义任何模型,它可以是任何现有类型(来自模型)。

In the Create.cshtml I'm creating the Labels and Editors (Textboxes), based on the attributes/properties of the specified type. 在Create.cshtml中,我正在创建标签和编辑器(文本框),基于指定类型的属性/属性。 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? 一切正常,但在我点击“创建”结束后,我从我的BusinessObjectController输入第二个“创建”方法,我似乎无法访问任何属性值? (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: 但是,如果我将类型从对象更改为“有效模型类型” - 例如“Car”,它会显示我输入的值:

[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? 或者我是否需要尝试以某种方式从HTML响应中获取它们? Whats the best way, please help.. 什么是最好的方式,请帮助..

Try using a FormCollection 尝试使用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) + ", ";
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM