7

I have the following bit in my view :

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

In my controller I have this :

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

Now when I check the person.PersonnelId it is empty. When I remove the {disabled = "disabled" } it works fine but then I can change the PersonnelId which is not what I want to do.

What am I doing wrong?

3
  • You could use HiddenFor + DisplayFor instead, or just add HiddenFor to what you already have. Anyway, it doesn't make sense to rely on this sort of protection on browser level. Anyone can send a forged POST query with PersonnelID changed to what they want. Commented Nov 7, 2012 at 10:29
  • Neither of these offer the same functionality as a disabled textbox? Commented Nov 7, 2012 at 10:30
  • In my sample I'm using it with PersonnelId. But I want to use the same for CreationDate, so users can see when the person was created or ModificationDate to display when the last change was made. The DisplayFor doesn't pass the value. THe HiddenFor, I can use for the PersonnelId, since you are not really supposed to see it. But I can't use it for CreationDate or ModificationDate, since those should be displayed. Commented Nov 7, 2012 at 10:39

2 Answers 2

10

Try 2 values

  1. PersonnelIdForDisplay
  2. PersonnelId

Then on your view

  1. @Html.TextBoxFor(model => model.PersonnelIdForDisplay, new { disabled = "disabled" })
  2. @Html.HiddenFor(p => p.PersonnelId)
Sign up to request clarification or add additional context in comments.

Comments

6

Try making it readonly instead.

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

1 Comment

The "readonly" works ok, except that the textbox is still enabled. When I add the "disabled", it looses the value again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.