1

I have a few questions.

I want to make my textbox readonly so I put [ReadOnly(true)] on my view model but the textbox does not have the readonly tag in it.

public class ViewModel()
{
   [HiddenInput(DisplayValue = false)]
   [ReadOnly(true)]
   public int Id { get; set; }
}

in my razor page I got

@Html.EditorFor(x => x.Id)

I also noticed that when I use EditorFor it add classes like "text-box single-line". Is there away I can stop these from being generated or add my own class names to it?

Finally can you use a meta tag to tell the EditrFor to be empty instead of placing a value in it. Like in my cause it puts zero since that what the int holds. What if I just want it too look blank?

2 Answers 2

1

I am confused as to exactly what you want. You are asking if you can have a readonly field that does not display the actual value of the bound property? I assume you want the Id in your View so that when you post back you still have the value? If this is correct then just use a hidden field;

@Html.HiddenFor(model => model.Id)

That way when your form is posted back it will still bind the Id to model in the Controller

Sign up to request clarification or add additional context in comments.

4 Comments

I am using this for when a person would add something and when someone would edit something. So when someone adds something no Id exists so I rather have the textbox be blank then have a zero in it. If they are editing something then it would be filled in a number. This is more playing around. If I have it hidden then I don't need the textbox readonly however the point is that if I put readonly on something I expect the textbox to be disabled or am I miss understanding what readonly is?(as I believe this is the name they gave asp.net webform txtboxes prop).
The same goes for the textbox being empty. In this case since it is hidden again I guess it does not make a different if it is zero or blank but I am thinking more about visible ones. Say I got a textbox that contains a date. I don't want some default date shown I rather have it blank. How do I do that?
I see what you are saying. To be honest I would create a PartialView for New content, and another for Editing content. It is always best to seperate out PartialViews based on the operation instead of embedding seperation logic in your View. If you set this up then in your New PartialView you would have a standard @Html.TextBoxFor which would not display a value, then in your Edit PartialView @Html.EditorFor which will display a value. This would also solve your date problem as well. Always keep things as simple, it saves you trying to work around problems that otherwise would not be there
As you have already worked out, the ReadOnly property does not effect the html markup on your Views, as I understand it, it was meant more for the Controller accepting values from the View
0

You could create a custom editor for Int32 data types and easily point that property to the new editor with a Data annotation, and could do for all other Int values that you want to display in a similar manner throughout the application (keeping it nice and DRY). Your existing ViewModel class would change to this:

public class ViewModel()
{
   [UIHint("ReadonlyInt")]
   [ReadOnly(true)]
   public int Id { get; set; }
}

And then you create a simple new partial view under Views\Shared\EditorTemplates called ReadonlyInt.cshtml

like this:

@model System.Int32?

@Html.TextBox("", Model.GetValueOrDefault().ToString(), new { disabled = "disabled" })

1 Comment

What is Readonly then for? Like I mean I have to do this for every single type that I need to make a readonly textbox. For this example it seems kinda pointless to use these editor then. I might as well use a textbox then.

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.