MVC Razor read only TextBoxFor - asp.net-mvc-3

In a MVC 3 Razor Project, I have defined DisplayFormat in ViewModel to format a DateTime Property
[DisplayFormat(DataFormatString = "{0:dd-MMM-yyyy}", ApplyFormatInEditMode = true)]
[Display(Name = "Posted Date")]
public DateTime PostedDate { get; set; }
And I need to make the field Read Only in View
#Html.TextBoxFor(model => model.PostedDate, new { #readonly = "readonly" })
But when displaying the date in TextBoxFor it does not apply the DisplayFormat. If I Change TextBoxFor to a EditorFor DisplayFormat is applied but can not apply the readonly CSS property.
How to apply the date formatting & make the text box readonly?

you can still use editor for when the editor for renders I believe based on your case it should still render to the text-box after it renders you can use a simple jquery function to add an attribute read-only to the rendered text-box
$("#PostedDate").attr("readonly", "readonly");
not sure if it the best solution but you can try it

Related

MVC Validation Attributes Kendo Grid don't work with UIHint

In my model I have
[Required(ErrorMessage = "BIC Code is required")]
[RegularExpression("^[a-zA-Z0-9]{11}$",ErrorMessage = "11 alphanumeric characters expected")]
[UIHint("BicCode")]
public string BicCode { get; set; }
The Template corresponding to the BicCode
#model string
#(Html.Kendo().MaskedTextBox().Name("BicCode").Mask("AAAAAAAAAAA") )
During my kendo grid edition all my validation attribute are used except the ones from properties using UIHint.
How to deal with UIHint Validation and Kendo Grid?
As suggested by Steve Greene in his comment.
Use MaskedTextBoxFor(m => m) instead of just MaskedTextBox() solved my problem.

how to bind model property to html helper textboxfor in mvc3

i want to tightly bind my property using html helper for TextBoxFor but i am not able to do so,i have simply binded using Textbox but i want to get data assign to textbox on httpPost
below is how i have done using simple HtmlHelper textbox
<%: Html.TextBox("RenewalDate", (string.Format("{0:yyyy/MM/dd}", Model.RenewalDate)), new { id = "txtRenewalDate", maxlength = 20, tabindex = 3, #class = "date" })%>
i dont want to use FormCollection that's why i want to bind tightly with TextBoxFor so that on httpPost my model has the value assigned to the Model.RenewalDate
please help....
Use an editor template, it's much easier:
<%= Html.EditorFor(x => x.RenewalDate) %>
and you could decorate your view model property with the DisplayFormat attribute to specify the desired format:
[DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}", ApplyFormatInEditMode = true)]
public DateTime RenewalDate { get; set; }
and then your POST controller action will take the view model as action parameter.
[HttpPost]
public ActionResult SomeAction(MyViewModel model)
{
...
}
And in order to apply the HTML attributes such as class, tabindex and maxlength to this editor template you could write a custom metadata provider as shown in the following article.
Also since the date is using the yyyy/MM/dd it is possible that the default model binder is not able to parse the value back because the default model binder uses the current culture settings. To resolve this issue you could write a custom model binder as I showed in this thread.

Issue To fetch date format At Edit() In ASP.NET MVC 3

When I edit my one of the form sin ASP.NET MVC 3 in this when I edit a user registration form then I got date in startdate is 21-Mar-12 12:00:00 AM in text box but I need 21-Mar-12.
So how can I format textbox date like that?
You could decorate the DateTime property on your view model with the [DisplayFormat] attribute which allows you to specify a given format:
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-MMM-yy}")]
public DateTime SomeDate { get; set; }
and in your strongly typed view use the EditorFor helper to render the corresponding input field:
#Html.EditorFor(x => x.SomeDate)
As far as binding the value back to a DateTime field when posting back is concerned you could write a custom model binder that will use this format. I have shown an example here.

mvc 3 view data validation

i'm trying to put DropDownList validation to work.
in model:
[Required(ErrorMessage = "this field is required")]
public int ObjectTypeID { get; set; }
in view:
<div class="editor-field">
#Html.DropDownList("ObjectTypeID", string.Empty)
#Html.ValidationMessageFor(model => model.ObjectTypeID)
</div>
if the user leaves the selection empty i expect client side validation to alarm. but this does not happen.
what can be done?
The behavior of system types is that they must have a value when initalized. An integer has a value of "0". Change your model to accept a nullable int:
public int? ObjectTypeID { get; set; }
Just wondering, but why not use DropDownListFor?
For client side validation to work I think you need to turn on ClientValidationEnabled & UnobtrusiveJavaScriptEnabled in the web.config for your project, I believe you also need to reference the jquery.validate.unobtrusive.min.js script on your page?
1) You are not loading your dropdownlist
2) Use DropDownListFor in order to match validation with ddl

MVC 3 - Change Html.TextBox to Html.TextBoxFor

I am using html.textbox for 2 of my datetime field because I need to format them in a specific format but i don't know how to do it by html.textboxfor.
However, I realise i need to have the textboxfor for the validation in my model class to work:
[Required(ErrorMessage = "Storage Date is required")]
[DataType(DataType.DateTime, ErrorMessage = "Please input a valid date")]
public DateTime StorageDate { get; set; }
Any idea how can I change my Html.Textbox below into Html.TextBoxFor with the same setting??
#Html.TextBox("expirydate", String.Format("{0:ddd, d MMM yyyy}", DateTime.Now), new { id = "expirydate" })
#Html.ValidationMessageFor(model => model.ExpiryDate)
Appreciate any help... Thanks...
You don't really need to use TextBoxFor() for validation to work. If your TextBox has the same id as a field in the model, the model binder will pick it up. If you're talking about to get the unobtrusive validation features, you can always manually add the data-* attributes to your TextBox.
However, in this case it sounds like what you really want is a custom editor, using EditorFor(). It's a bit more work, but it will allow you to actually enforce the date/time formatting by giving the user something like a date/time picker control. The basic idea is:
Create a partial view called DateTime.cshtml that is bound to model of type Nullable<DateTime>, and put it into the Shared/EditorTemplates view folder.
Use jQuery and jQueryUI to put an HTML textbox that is styled as a date/time picker into the partial view.
Decorate the property on your model with the [DataType(DataType.DateTime)] attribute
Use Html.EditorFor(model => model.WhateverProperty)
Fortunately, date/time pickers are probably the most popular custom MVC3 editor, so there are plenty of examples to pick from; the code from this question works fine, just make sure to note the suggestion in the answer and replace this line in the partial view:
#inherits System.Web.Mvc.WebViewPage<System.DateTime>
with this:
#model System.DateTime?

Resources