开发者

Custom model in ASP.NET MVC controller: Custom display message for Date DataType

开发者 https://www.devze.com 2022-12-29 05:16 出处:网络
I have an ASP.NET MVC Page that i have to display the fields in customized Text. For that I have built a CustomModel RequestViewModel with the following fields.

I have an ASP.NET MVC Page that i have to display the fields in customized Text. For that I have built a CustomModel RequestViewModel with the following fields.

Description, Event, UsageDate

Corresponding to these my custom Model has the below code. So that, the DisplayName is displayed on the ASP.NET MVC View page.

Now being the Description and Event string Datatype, both these fields are displaying Custom DisplayMessage. But I have problem with Date Datatype. Instead of "Date of Use of Slides", it is still displaying UsageDate from the actualModel.

Anybody faced this issue with DateDatatype?

Appreciate your responses.

Custom Model:

[Required(ErrorMessage="Please provide a description")]
[DisplayName("Detail Description")]
[StringLength(250, ErrorMessage = "Description cannot exceed 250 chars")]
// also need min length 30
public string Description { get; set; }

[Required(ErrorMessage="Please specify the name or location")]
[DisplayName("Name/Location of the Event")]
[StringLength(250, ErrorMessage = "Name/Location cannot exceed 250 chars")]
public string Event { get; set; }

[Required(ErrorMessage="Please specify a date", ErrorMessageResourceType = typeof(DateTime))]
[DisplayName("Date of Use of Slides")]
[DataType(DataType.Date)]
public string UsageDate { get; set; }

ViewCode:

<p>
    <%= Html.LabelFor(model => model开发者_JS百科.Description) %>
    <%= Html.TextBoxFor(model => model.Description) %>
    <%= Html.ValidationMessageFor(model => model.Description) %>
</p>
<p>
    <%= Html.LabelFor(model => model.Event) %>
    <%= Html.TextBoxFor(model => model.Event) %>
    <%= Html.ValidationMessageFor(model => model.Event) %>
</p>
<p>
    <%= Html.LabelFor(model => model.UsageDate) %>
    <%= Html.TextBoxFor(model => model.UsageDate) %>
    <%= Html.ValidationMessageFor(model => model.UsageDate) %>
</p>


What is your intentions with ErrorMessageResourceType = typeof(DateTime)? I don't believe you are suppose to set that without using ErrorMessageResourceName. Have you tried removing it?


You have the attribute definition for [Required(ErrorMessage="Please specify a date", ErrorMessageResourceType = typeof(DateTime))] incorrect. I suspect this is causing the problem.

The ErrorMessageResourceType property is used in conjunction with ErrorMessageResourceName property. You can check out this link for more info about how to use them correctly (not directly related to your problem).

Change the code to this and you should be all set:

[Required(ErrorMessage="Please specify a date")]
[DisplayName("Date of Use of Slides")]
[DataType(DataType.Date)]
public string UsageDate { get; set; }

Not completely sure, but you may also want to drop the [DataType(DataType.Date)] seeing as you've already reformatted the date value into a string when populating the Model (I presume?).


Just a suggestion, but you may want to consider rather than converting the DateTime to a string, instead using the DisplayFormatAttribute and then modifying your UsageDate property to look something like this:

[Required(ErrorMessage="Please specify a date")]
[DisplayName("Date of Use of Slides")]
[DisplayFormat(DataFormatString = "{0:d}")]
public DateTime UsageDate { get; set; }

Where the value of DisplayFormatAttribute.DataFormatString corresponds to the desired output formatting of the DateTime. See this page for a complete list of formatting patterns.

With the above property definition you can then call <%=Html.DisplayFor(m => m.UsageDate)%> in your View and it will spit out the date in the format you specified with the attribute. Much cleaner and far more extensible than converting the DateTime to a string prior to sending it to the View- but of course it's entirely up to you. :)

Happy travels!

0

精彩评论

暂无评论...
验证码 换一张
取 消