mvc DataAnnotations how to make field no editable in 3.5 - asp.net

I have a few field in my entity that i wish to be non-editable. Looking in the docs it seems like "EditableAttribute" would do the trick. However this is only 4.0
Just wondering if there are other attributes that would have the desire effect. So be clear,
i have a field called "DateRegistered" i wish to display this as string not text field using "Html.EditorFor"

The [ReadOnly] attribute should work in 3.5.

Why are you using an editor template for something that should be read only? Display templates and the <%= Html.DisplayFor(x => x.DateRegistered) %> method seem more appropriate in this case.

Related

Override Date field template with razor pages and ASP.NET Core 2.1

I want to override the template for the date field input. I am using Visual Studio 2017, ASP.NET Core 2.1 and Razor Pages. I read on different pages to put the template in
\Views\Shared\EditorTemplates\Date.cshtml
but this file will be ignored.
At the Moment the Standard template inserts a field, that results in HTML5 Standard date selector which is terrible in Edge, especially for birthdates. I want to replace it with the jquery datepicker.
As long as the type is date the HTML5 selector will be displayed together with the datepicker, which is not usable.
I want to Change the type in the template and the class. I create a Date.cshtml:
#model DateTime
Test Date Box
#Html.TextBox("", Model.Date, new
{
#class = "datePicker",
#readonly = "readonly"
}))
and put this in \Pages\Shared\EditorTemplates but it didn't work. Even in \Views\Shared\EditorTemplates it did not work.
Does anyone have an idea?
Best regards
David
you may use classic HTML inputs instead of HTML helpers and use other date picklers like jquery etc.

Produce a "type" attribute in the "script" tag when using Scripts.Render in ASP.NET MVC 4

Is it possible to configure Scripts.Render in ASP.NET MVC 4 to produce a "type" attribute in the "script" tag?
If so, how?
Thanks in advance,
Ryan
The 1.1-alpha1 update has support doing your own tag formatting with the Scripts/Styles helpers.
There's a new DefaultTagFormat property which is by default set to:
"<script src="{0}"></script>"
There's also a RenderFormat method which takes in the tag format as well. You should be able to add the type attribute via either of these two mechanicsms.

How to get "Title" attribute from the "Page" object

I use SilverStripe as the CMS and I'm stuck now and don't know how to access "Title" attribute from the "Page" object.
I tried:
$Event.Trainer.Title
But it doesn't work. The "Trener" is the "TrenerPage" object. How can I access to Trener->Title attribute?
You can't traverse three levels in SilverStripe templates (at least in version 2.x). Two is the maximum.
What you need is something like this:
<% control Event %>
$Trainer.Title
<% end_control %>
Your question seems to inconsistently switch between "Trainer" and "Trener", I'm guessing one of those is a typo?
If the template is for the page you wish to display the title of, all you need to use is $Title in your template and it will output the title of the rendering page.
If the template is NOT for the page you wish to display the title of, then like xeraa said, you should use a control block.
The title is directly within the Page object.
Just using $Title should do the trick. To help you with all the methods available in the Page object go to:
http://doc.silverstripe.org/sapphire/en/reference/built-in-page-controls
Since the question is not very clear, I'll take a shot at another answer.
If you derived the Trainer_Page from the Page object it still inherit the $Title attribute directly. Unless you overider the $Title attribute yourself in the Trainer_Page object, PHP will default it back to the parent class. In that case just use $Title.
Beware of the case as $title and $Title are not the same.
Good luck.

How can I style invalid text field content in Spring MVC (JSP)

The Spring MVC JSP tag library has a tag for rendering form errors. This makes it easy to render an error message next to, say, an input text field. However, it is common practice on many websites to also style the input text field itself (with a red border maybe) to highlight a validation error.
Is there any way of doing this with the Spring JSP tags or will I have to bake my own solution?
I have never used Spring MVC JSP tags but looking at the documentation it looks like cssErrorClass is the way to go:
<form:input path="userName" cssErrorClass="error"/>
Equivalent to "class" - HTML Optional Attribute. Used when the bound field has errors.
Obviously you can now define input.error class in your CSS stylesheet.
You can use spring:bind tag around the form:input tag. In between the spring:bind tag you can use something like ${status.error ? 'error' : ''} for your style class.
status.error will be true if errors. form input field is available

ASP.NET MVC2 Not replacing underscores with dashes in HtmlAttributes

I've heard from a couple of different sources that when using HTML helpers in ASP.NET MVC2, one can create custom attributes with dashes in them (e.g. <a data-rowId="5">) by using an underscore in place of the dash, and when the HTML is written to the page the underscores will be replaced by dashes.
So, something like this:
<%= HtmlActionLink(Model.Name, "MyView", null, new {data_rowId = Model.id}) %>
should render as
<a data-rowId="0" href="myURL">Row Name</a>
But... it's not. I think that maybe this feature is only enabled in the MVC3 Beta preview (as it's mentioned in the MVC3 preview release notes), but this thread is about the same thing, and it's regarding MVC2.
I know I can use the other solution presented in that thread, but I'd rather not have to resort to using the dictionary if a more elegant solution exists.
Anyone know if there's something simple I can do to get this particular thing working?
Not exactly the most elegant solution but probably acceptable:
<%= Html.ActionLink(
Model.Name,
"MyView",
null,
new Dictionary<string, string> { { "data-rowId", Model.id } }
) %>
On a side note: data-rowId is a totally invalid attribute in HTML according to standard doctypes so maybe the most elegant solution would be to get rid of it :-)
System.Web.Mvc.HtmlHelper provides a static method that does what you're looking for:
<%= Html.ActionLink(
Model.Name,
"MyView",
null,
HtmlHelper.AnonymousObjectToHtmlAttributes(new { data_row_id: Model.id })
) %>
This method will will substitute underscores with hyphens - as somebody pointed out though, be sure to use all-lowercase attribute names, which are HTML5-compliant.
Consider what ASP.NET MVC is designed to do. It is designed to give you tight control over the HTML that you produce. If something is not accessible, you won't have to jump through hoops to fix it. What you are trying to do is create invalid HTML, or XHTML. Instead of trying to make HTML that is invalid simplify to just using an ID. #1 on the accessibility guidelines is that the HTML is valid for the declared type.
As long as the ID is unique within the document you will have satisfied HTML conformance--and it will make it much easier to manipulate with jQuery.
If I may, why are you trying to create invalid HTML? Or is this a proprietary XML format?
BTW, MVC 3 RC came out today.
Edit:
Playing with HTML5 features, while "shiny" and kool, are unstable. Many parts of the specification are changing as often as weekly. What is considered good now may not be good when the final spec is released. MVC 3 does have some things that will make your job more friendly, for now you have to use a dictionary.

Resources