我试图绑定自定义模型,看起来大致如下:
[ReadOnly(true)]
[DisplayName("User ID")]
public String ID { get; set; }
[DisplayName("User Name")]
[Required]
public String Name { get; set; }
[DisplayName("Change Password")]
[Required]
public bool ChangePassword { get; set; }
[DisplayName("New password")]
[Required]
public String NewPassword { get; set; }
public Dictionary<String,Dictionary<String,bool>> Navigation { get; set; }
为了使输入表单看起来更好,我使用div面板分隔不同的导航项:
<div class="row">
<div class=".small-12.large-3.large-centered.columns">
<h2>EditUser</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.Partial("ValidationSummary", @ViewData.ModelState)
<div>
<legend>User details</legend>
@Html.HiddenFor(model => model.ID)
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.NewPassword)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.NewPassword)
@Html.ValidationMessageFor(model => model.NewPassword)
</div>
<div class="editor-field">
<div class="switch">
<input id="switch-off" type="radio" name="switch-x" checked />
<label for="switch-off">Keep</label>
<input id="switch-on" type="radio" name="switch-x" />
<label for="switch-on" id="offClick">Change</label>
<span></span>
</div>
</div>
@{
int m = 0;
}
@{int k = 0;}
@foreach (var item in Model.Navigation)
{
<div class="panel">
@{String name = names.Keys.ToArray()[m];}
<h3>@name</h3>
<div class="editor-field">
@for (int i = 0; i < item.Value.Count; i += 3)
{
@:<div id="btnset_@k">
for (int j = 0; j < 3; j++)
{
if (i + j < item.Value.Count)
{
String id = item.Key + "_" + item.Value.Keys.ToArray()[i + j];
String value = item.Value.Values.ToArray()[i + j] ? "checked=\"checked\"" : String.Empty;
<input type="checkbox" id="@id" @value />
<label for="@id">@names[name][i + j]</label>
}
}
@:</div>
k++;
}
</div>
</div>
m++;
}
<p>
<input type="submit" value="Save" class="button" />
</p>
</div>
}
问题是当我派生Default DefaultModelBinder时,controllerContext.HttpContext.Request.Form不包含单选按钮组的键,我在表单中有。我做错了什么?
答案 0 :(得分:1)
您没有设置绑定所需的名称属性。