所以这是我的情况,我有一个模型:
public class Label
{
[Key]
[MaxLength(20, ErrorMessageResourceType = typeof(Validation), ErrorMessageResourceName = "MaxLength")]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public string ID { get; set; }
public string en { get; set; }
public string fr { get; set; }
public string es { get; set; }
public string ar { get; set; }
}
控制器动作如下所示:
public ActionResult Edit(int id = 0)
{
Criteria criteria = db.Criteria.Find(id);
if (criteria == null)
{
return HttpNotFound();
}
Label label = dbl.Labels.Where(l => l.ID == criteria.Label).FirstOrDefault();
return View(label);
}
并发布:
[HttpPost]
public ActionResult Edit(Label label)
{
if (ModelState.IsValid)
{
Label L = dbl.Labels.Find(label.ID);
L.en = label.en;
dbl.SaveChanges();
}
return View(label);
}
和一个看起来像这样的视图:
@model RAPID.Models.Translation.Label
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Criteria</legend>
<div class="editor-label">
@Html.LabelFor(model => model.ID)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ID)
@Html.ValidationMessageFor(model => model.ID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.en)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.en)
@Html.ValidationMessageFor(model => model.en)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.es)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.es)
@Html.ValidationMessageFor(model => model.es)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.fr)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.fr)
@Html.ValidationMessageFor(model => model.fr)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ar)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ar)
@Html.ValidationMessageFor(model => model.ar)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
我遇到的问题是由于某种原因,当我在 Edit(get) 中检查 label.ID 时,该值是与该字段关联的正确字符串值。现在,当我在帖子上检查相同的值 label.ID 时,该值已更改为似乎与表中记录的顺序相对应的整数值。同样在编辑视图中,显示的值是数字值与正确的字符串值。所以我认为它与模型绑定有关,但不知道如何解决它。
想知道是否有人知道问题可能是什么。
提前致谢