[英]Create dynamic fields in asp.net mvc
我的表user
其他自定義字段中的字段由admin添加。 sql單擊此處以圖說明
所以我想用用戶字段和自定義字段創建Register
頁面。 我得到FiledType
和FieldName
列出。 在控制器中:
public ActionResult Index()
{
ViewBag.CustomeFields = userCustomeFields.GetCustomeField();
return View();
}
UserField模型:
public class UserField
{
public string FieldName;
public string FieldTypeName;
}
所以我想在cshtml文件中創建動態字段。 我寫了這段代碼: 更新:
@foreach (var item in ViewBag.CustomeFields)
{
<div class="form-group">
@*@Html.LabelFor(i=>item.FieldTypeName , htmlAttributes: new { @class = "control-label col-md-2" })*@
<div class="col-md-10">
@if (item.FieldTypeName == "Textbox")
{
@Html.TextBox(item.FieldName)
}
</div>
</div>
@*@Html.Editor(item.FieldName, new { htmlAttributes = new { @class = "form-control" } })*@
}
但我給錯誤! HtmlHelper<User>' has no applicable method named 'TextBox' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.
我不知道我的方式正確嗎?
如果您不想將其標記為正確答案,這里再次回答您的第一個問題^^
你必須像這樣鑄造它
@foreach(UserField item in ViewBag.CustomFields) {
要么
@Html.TextBox(((UserField)item).FieldName)
或@ Html.TextBox((string)item.FieldName)
對於屬性,您可以使用帶有new { ... }
的匿名類型對象
@Html.TextBox("nameOfTheTexbox", "initialValue", new { @class ="myCssClass myOtherCssClass", disabled = "disabled" })
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.