Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取Create(HttpPost)中的PropertyValue-非强类型_C#_Asp.net Mvc 3_Reflection - Fatal编程技术网

C# 获取Create(HttpPost)中的PropertyValue-非强类型

C# 获取Create(HttpPost)中的PropertyValue-非强类型,c#,asp.net-mvc-3,reflection,C#,Asp.net Mvc 3,Reflection,我得到了以下代码: 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

我得到了以下代码:

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
}
   @{
        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:

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
}
   @{
        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>

我需要它。。如何获取这些属性值?或者我需要设法从HTML响应中获取它们吗?最好的办法是什么,请帮忙

尝试使用FormCollection

public ActionResult Create(FormCollection collection)
{
   //Can access any propertyvalues, but its not dynamic!
}
然后,您可以使用类似这样的方法访问这些值

    string s = string.Empty;
    foreach (var key in collection.AllKeys) {
        s += key + " : " + collection.Get(key) + ", ";
    }

看起来您需要编写一个自定义模型活页夹。我在下面的帖子中对此进行了说明:“最佳”方式?老实说,我从来没有见过这种类型名称而不是类型本身的绑定。我想您可以将
对象
强制转换为
汽车
的一个实例。不过,我假设它也可以是其他对象的实例?也许您可以让所有实例都实现一个接口,该接口包含实例类型的名称,然后首先转换到该接口以获取名称并转换到该名称的类型。不过很乱。您可以手动迭代一个
FormCollection
参数来获取发布的值。也许不那么凌乱?很难说。通过迭代这个集合,我只得到了“Propertynames”,而不是值。。似乎隐藏在某个地方。我已经更新了答案,以显示如何循环键并访问值。