Tl; Dr 我无法找到正确的命名约定来填充Property Object。
所以我有这个项目,我正在努力用户需要能够动态创建html输入。我为此创建了一个模型如下
public class Property
{
public String name { get; set; }
public List<String> dataTypes { get; set; }
public String dataType { get; set; }
public Guid PropertyID { get; set; }
public virtual Range valueRange { get; set; }
public int length { get; set; }
public String label { get; set; }
public String format { get; set; }
public String cssClasses { get; set; }
public Dictionary<string, string> selectOptions { get; set; }
public dynamic value { get; set; }
}
public Property(String name = "", dynamic value = null, List<String> dataTypes = null, Range valueRange = null, int length = 0, String format = "", String label = "")
{
this.name = name;
this.valueRange = valueRange == null ? new Range() : valueRange;
this.length = length;
this.format = format;
this.label = label == "" ? name : label;
this.dataTypes = dataTypes == null ? new List<String> {
GlobalConstants.COMMA,
GlobalConstants.TEXT,
GlobalConstants.NUMBER,
GlobalConstants.TEXTAREA,
GlobalConstants.SELECT,
GlobalConstants.DATEPICKER,
GlobalConstants.USERPICKER,
GlobalConstants.DOCUMENT } : dataTypes;
this.value = null;
}
我有另一个实体,它有一个包含这些属性的字典。像
这样的东西public class Template
{
public Dictionary<string,Property> DefaultProperties {get;set;}
public Template()
{
this.DefaultProperties = new Dictionary<string, Property>
{
{"name", new Property{ name="name", dataType= GlobalConstants.TEXT , label="Template Name", } },
{"parties", new Property{ name="parties", dataType= GlobalConstants.TEXT , label="Parties Filing the Grievance" } },
{"dateFiled", new Property{ name="dateFiled", dataType= GlobalConstants.TEXT , cssClasses ="datepicker", label="Date Filed" } },
{"reviewerGroup", new Property{ name="reviewerGroup", dataType= GlobalConstants.USERPICKER , cssClasses ="selectUsers", label="Reviewer Group" } },
{"approverGroup", new Property{ name="approverGroup", dataType= GlobalConstants.USERPICKER , cssClasses ="selectUsers", label="Approver Group" } },
{"governingAgreements", new Property{ name="governingAgreement", dataType= GlobalConstants.COMMA , label="Governing Agreement Sections" } },
{"department", new Property{ name="department", dataType= GlobalConstants.TEXT , label="Department Where Grievance was filed" } },
{"politicalJudgement", new Property{ name="politicalJudgement", dataType= GlobalConstants.SELECT , label="Political Impact", selectOptions = new Dictionary<string, string> { { "high", "High" }, { "medium", "Medium" },{ "low", "Low" } } } },
{"monetaryJudgement", new Property{ name="monetaryJudgement", dataType= GlobalConstants.SELECT , label="Monetary Impact", selectOptions = new Dictionary<string, string> { { "high", "High" }, { "medium", "Medium" },{ "low", "Low" } } } },
{"processJudgement", new Property{ name="processImpactJudgement", dataType= GlobalConstants.SELECT , label="Process Impact", selectOptions = new Dictionary<string, string> { { "high", "High" }, { "medium", "Medium" },{ "low", "Low" } } } }
};
}
}
所以在我看来,我有一些东西,其中Model是模板
@foreach (var property in Model.DefaultProperties)
{
<div class="form-group">
@MyHelpers.generateProperty(property.Value, Html, true, false, "Template.DefaultProperties")
</div>
}
一些魔法
@helper buildInput(FBT_GRASP.Models.GrievanceProperty property, String type = "text", String classes = "", Dictionary<string, string> extraProperties = null, string parentObject = "")
{
<input data-field="@Json.Encode(property)"
name="@(parentObject+"['"+property.name+"']")"
id="@property.name"
type="@type"
value="@property.value"
class="form-control @if (classes != "") {<text>@classes</text> } @{if (property.cssClasses != null) { <text> @property.cssClasses</text> } }"
@{if (extraProperties != null) {
foreach (KeyValuePair<string, string> kv in extraProperties) {
<text>data-@(kv.Key)="@(kv.Value)"</text>
}
}
}
@{if (property.placeholders.ContainsKey(property.dataType)) { <text> placeholder="@property.placeholders[property.dataType].placeholder" </text> } }
@{if (property.placeholders.ContainsKey(property.dataType)) { <text> pattern="@property.placeholders[property.dataType].pattern" </text> } } />
}
成品看起来像:
<input name="Template.DefaultProperties['name']" id="name" class="form-control valid" placeholder="Free form text field" pattern="">
我已尝试命名字段
我似乎无法让它发挥作用。当它被回发后,字典会填充每个项目。但是值Property Property是null。我可以很好地设置控制器中的值,但我不知道是否有更好的方法来实现它。
感谢任何帮助。
编辑:哦控制器看起来像 [HttpPost]
public ActionResult Create(Template Template)
{
db.Templates.Add(template);
db.SaveChanges();
return Redirect("/template/edit/" + template.templateID);
}