Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.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# 表单中的动态属性_C#_Webforms - Fatal编程技术网

C# 表单中的动态属性

C# 表单中的动态属性,c#,webforms,C#,Webforms,如何为动态属性创建表单 所以我理解这就是动态获取对象属性的方式: ObjectName.GetType().GetProperty("nameofproperty") 我如何在表格中包含上述内容?我尝试过这样做: //loop through objects in model @Html.CheckBoxFor(model => m.ElementAt(i).Person.GetType().GetProperty("isAvailable"), new { @class = "pe

如何为动态属性创建表单

所以我理解这就是动态获取对象属性的方式:

ObjectName.GetType().GetProperty("nameofproperty")
我如何在表格中包含上述内容?我尝试过这样做:

//loop through objects in model

 @Html.CheckBoxFor(model => m.ElementAt(i).Person.GetType().GetProperty("isAvailable"), new { @class = "person-avail" })
在模型中,我将Person定义为如下属性:

public Person Person { get; set; }
@Html.CheckBoxFor(model => m.ElementAt(i).Person.isAvailable,
    new { @class = "person-avail" })
@Html.CheckBoxFor(model =>
    {
        Person person = m.ElementAt(i).Person;

        return (bool)person.GetType().GetProperty("isAvailable").GetValue(person);
    },
    new { @class = "person-avail" })
Person类有自己的属性(我要为其创建表单的属性):

因此,目前:

@Html.CheckBoxFor(model => m.ElementAt(i).Person.GetType().GetProperty("isAvailable"), new { @class = "person-avail" })
给我一个例外说:

Cannot implicitly convert type 'System.Reflection.PropertyInfo' to 'bool'

我理解问题所在,但我该如何着手解决它呢?

鉴于您发布的示例,我不清楚您为什么不能这样编写代码:

public Person Person { get; set; }
@Html.CheckBoxFor(model => m.ElementAt(i).Person.isAvailable,
    new { @class = "person-avail" })
@Html.CheckBoxFor(model =>
    {
        Person person = m.ElementAt(i).Person;

        return (bool)person.GetType().GetProperty("isAvailable").GetValue(person);
    },
    new { @class = "person-avail" })
也就是说,由于
Person
属性已经键入为
Person
,那么名为
的属性无论如何都应该存在


这就是说,如果出于某种原因,您确实必须出于某种未指定的原因进行反射,那么您发布的代码中存在的问题是反射只给您提供
PropertyInfo
对象。要获取实际值,需要将该对象应用于原始对象以检索属性值。例如:

@Html.CheckBoxFor(model => (bool)m.ElementAt(i).Person.GetType()
    .GetProperty("isAvailable").GetValue(m.ElementAt(i).Person),
    new { @class = "person-avail" })
换句话说,您必须将
Person
对象实例引用传递给
PropertyInfo.GetValue()
方法。由于该方法返回一个
对象
(即装箱的
bool
值),因此您还必须返回到
bool



注意:如果您可以缓存
m.ElementAt(i).Person
值,即用于调用
GetType()
和传递给
GetValue()
方法,这将是一个好主意。不幸的是,我对web表单的了解还不够深入,不知道该语法是什么样子,甚至不知道它是否可行。如果您可以使用C#的完整lambda功能,可以这样做:

public Person Person { get; set; }
@Html.CheckBoxFor(model => m.ElementAt(i).Person.isAvailable,
    new { @class = "person-avail" })
@Html.CheckBoxFor(model =>
    {
        Person person = m.ElementAt(i).Person;

        return (bool)person.GetType().GetProperty("isAvailable").GetValue(person);
    },
    new { @class = "person-avail" })
否则,我想你可以自己解决这个问题。:)


这个问题可能会对@MattC有所帮助,谢谢,但我已经找到了动态获取酒店信息的方法。问题是,当我创建表单时,我还不知道某个属性的值是多少,所以我的问题是如何将上述内容集成到表单中。@MattC这将如何添加表单中的用户输入?您是否尝试过-@Html.CheckBoxFor(model=>m.ElementAt(i).Person.GetType().GetProperty(“isAvailable”).GetValue(m.ElementAt(‌​i) .Person),新的{@class=“Person-avail”})哦,好的,我明白了,您希望对Web表单中的动态属性进行读写操作。如果您最初设置了该值,那么当表单发布回控制器时,复选框中的值应位于发送回的Person Model对象中。谢谢,我刚刚尝试了此操作,但在强制转换为布尔值时,我得到以下错误:模板只能用于字段访问、属性访问、一维数组索引、,或单参数自定义索引器表达式。我确实尝试过直接绑定属性,但它不会将数据保存回模型,所以我在寻找一个不同的解决方案,这就是为什么我想用反射来实现它