2012-11-07 24 views
6

我已经在我看来,以下位:HTML.TextBoxFor禁用不及格值

@Html.TextBoxFor(model => model.PersonnelId, new { disabled = "disabled" }) 

在我控制我有这样的:

if (ModelState.IsValid) 
{ 
    PersonnelFacade.SavePerson(person); 
    return RedirectToAction("Index"); 
} 

现在,当我检查person.PersonnelId是空。 当我删除{disabled =“disabled”}它工作正常,但我可以更改PersonnelId,这不是我想要做的。

我在做什么错?

+0

你可以使用'HiddenFor' +'DisplayFor'代替,或者只是HiddenFor'加上'以你已经拥有。无论如何,在浏览器级别依靠这种保护是没有意义的。任何人都可以发送伪造的POST查询并将其'PersonnelID'更改为他们想要的。 – GSerg

+0

这些都不提供与禁用的文本框相同的功能吗? – Liam

+0

在我的示例中,我将它与PersonnelId一起使用。但是我想为CreationDate使用相同的内容,因此用户可以查看创建人员的时间,或者在最后一次更改时显示ModificationDate。 DisplayFor不会传递该值。隐藏的,我可以用于PersonnelId,因为你不是真的应该看到它。但是我不能将它用于CreationDate或ModificationDate,因为它们应该被显示。 –

回答

5

尝试使其只读。

@Html.TextBoxFor(model => model.PersonnelId, new { readonly= "readonly" }) 
+1

“只读”工作正常,但文本框仍处于启用状态。当我添加“禁用”时,它会再次丢失值。 –

9
尝试

2个值

  1. PersonnelIdForDisplay
  2. PersonnelId

然后在您的视图

  1. @ Html.TextBoxFor(型号=> model.PersonnelIdForDisplay,新{disabled =“禁用”})
  2. @ Html.HiddenFor(P => p.PersonnelId)
+0

谢谢..它对我有帮助.. –