I'm developing a web project with service and repository layers in ASP.NET MVC 2.
I'm using Entity Framework.
I have two generated EF classes. Namely, Company and User.
Company has two fields CompanyID {int} and CompanyName {string}.
User has three fields UserID{int}, UserName{string}, BirthMonth{smallint,can be null} and CompanyID{int, this is a foreign key of Company}
I have a view for displaying UserName, Month and CompanyName. And also, this same view should include another model, namely Messages (MessageTitle, RecievedDate, ReceivedFrom) I would like to view BirthMonths as month names (January, February, etc.)
I'm stuck at converting short? to short and displaying months as names (ie. January, February) other than numbers.
Give this a try(I didn't test it, but found similiar in a few web searches).
string month = String.Empty;
if(BirthMonth.HasValue)
{
DateTime date = new DateTime(1,(int)BirthMonth.Value,1);
month = date.ToString("MMMM");
}
else
{
// month = some default value here;
}
Edit to add:
To convert short? to short specifically:
short myShort = BirthMonth.HasValue ? BirthMonth.Value : 0;
short? to short can be done using GetValueOrDefault(); to get the non-nullable form, and to get the month name, write a quick converter to convert the number to month name... There may be something within DateTime.ToString to give this to you too.
Related
If I have a Defined Tag Type, called "published date" for a Stereotype called "wiki article" with:
Type=DateTime;
BaseStereotype=wikiarticle;
Then Sparx pulls the date format from the User Desktop preferences of the Individual user and then stores a string in the t_objectproperties table. The result is a mix of different date types like:
1/9/2017
02/02/2018
2018-02-21
Where only the last one (ISO 8601 is unambiguous).
Is there way I can enforce these data formats the in Sparx?
Its not possible within Enterprise Architect.
But for one othe the customer we did it through an addin.
All you need to do is define a tagged value with Type=AddinBroadcast
and in your addin add the broad cast event EA_OnElementTagEdit
public void EA_OnElementTagEdit(EA.Repository Repository, int ObjectID, String TagName, String TagValue, String TagNotes)
{
DatetimePickerForm form = new DatetimePickerForm(); //Form designed for time picker
form.ShowDialog();
// Assign values for TagName,TagValue and TagNotes
}
And if you clicked that tagged value a datetime picker similar to EA will be displayed.
Format of the datetime picker can be customized.Please refer here for full list of formats
I'm having a terrible time adding a date to a view. I want the user to be able to type in a date like 6/30/15 or 6/30/2015, but the validation keeps failing if I use the 2 digit date. I've tried to "tell" MVC to accept a two digit date, but it always fails. Can someone explain how to get this right?
My view is set up like this (including just the troublesome date field for brevity):
#model Models.CompanyContracts
#Html.EditorFor(m => m.BegDate)
I started out with a simple model with a date field defined like this:
public DateTime? BegDate { get; set; }
I got an error saying "The field BegDate must be a date" when I entered the date as 6/30/15 or 06/30/15; I did not get the error when I entered 6/30/2015 or 06/30/2015.
So I tried to add a type and display attribute as describe
Here: Format datetime in asp.net mvc 4
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:M/d/yy}", ApplyFormatInEditMode = true)]
I get the same error trying to enter 6/30/15. I tried several different formats, including a datatype and not a displayformat, including a displayformat and not a datatype, but I get the "The field BegDate must be a date" error everytime.
I tried a regular expression validation and it also failed (both the MVC validation and the regular expression; obviously the regex I got from http://regexlib.com/DisplayPatterns.aspx?cattabindex=4&categoryId=5&AspxAutoDetectCookieSupport=1 needs some work):
[RegularExpression(#"^((0?[13578]|10|12)(-|\/)(([1-9])|(0[1-9])|([12])([0-9]?)|(3[01]?))(-|\/)((19)([2-9])(\d{1})|(20)([01])(\d{1})|([8901])(\d{1}))|(0?[2469]|11)(-|\/)(([1-9])|(0[1-9])|([12])([0-9]?)|(3[0]?))(-|\/)((19)([2-9])(\d{1})|(20)([01])(\d{1})|([8901])(\d{1})))$", ErrorMessage = "Invalid Date")]
public DateTime? BegDate { get; set; }
Can someone tell me how to get MVC to accept entering dates with 2 or 4 digits?
One other note, no external date pickers. I need to get this working with a simple entering of text. Once I can prove that can be done, my boss might allow me to spend some time figuring out a date picker. Thanks!
-- Update 7/20/15 --
Anyone have suggestions for this? I haven't found anything online that's helped me. I'd appreciate any feedback at this point to give me somewhere else to look for a solution.
-- Update 7/29/15 --
Still looking for suggestions. Any ideas at all?
I think it's simply not possible without some kind of twist.
How is the engine/code suppose to know which year it is if you enter just 15 as the year.
Is it 2015 ? 1915 ? 2115 ?
You need to pass the 4 digits to create the dates, if you only have 2 digits you cannot create the date.
So in your case what I would try is to default and hide the '20' prefix (assuming you can only enter a date for 2000's) to your 2 digits year input (or add it somehow using a bit a javascript).
I need to search for data and show it on the page. I want a code in the index to be something similar to this
If Not String.IsNullOrEmpty(searchString) Then
students = students.Where(Function(s) s.LastName.ToUpper().Contains(searchString.ToUpper()) _
Or s.FirstMidName.ToUpper().Contains(searchString.ToUpper()))
but instead of string I am searching for ID which is integer. How can I do that?
I am creating a asp.net mvc web application using vb.net
Since it is an integer you may just check for equality.
students = students.Where(Function(s) s.Id = searchId)
I want do have my DateTime get displayed without the time.
So I use
item.AD_Date.Date
to cut off the time.
In the Model ( I use MVC) AD_Date is set up like this:
public System.DateTime AD_Date { get; set; }
But I get my date like this:
01.03.2013 00:00:00
What did I do wrong?
The Date property just returns a DateTime with the same date, but at midnight. There's no difference between "a DateTime at midnight" and "a DateTime just representing a date" (unfortunately).
You need to change how your DateTime is formatted to only show the date - either by annotating the model or by changing the view.
(Alternatively, use my Noda Time library which has different types for the different kinds of data you want to represent. I haven't tried using it with MVC, but at least for the formatting side it should work okay...)
If someone is facing the same problem, here is my formatting :
#String.Format("{0:yyyy-MM-dd}", item.AD_Date)
I want to use Data Annotations to validate DateTime fields, but I'm running into problems. According to documentation on MSDN (http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.rangeattribute.aspx), the following should do the job
[Range(typeof(DateTime), "1/2/2004", "3/4/2004",
ErrorMessage = "Value for {0} must be between {1} and {2}")]
However, this marks any date I enter as invalid!
At first I thought it was not picking up UK dates (when I tried 26/2/2004) but I can't even get it to use dates such as 2/2/2004.
I'm using the dataannotations within MVC2, and using the MicrosoftAjax framework for clientside validation.
Any suggestions?
Thanks
Well, a few years have gone past and I revisited this same issue with MVC4 and I can tell you that it has apparently been resolved.
I created a very simple default MVC4 site, and gave a date member the following attributes
[Required]
[DataType(DataType.Date)]
[Range(typeof(DateTime), "1/2/2004", "3/4/2004", ErrorMessage = "Value for {0} must be between {1} and {2}")]
public DateTime BlogDate { get; set; }
The validation now works perfectly under UK data system, disallowing a date 2/1/2004, allowing a date of 4/3/2004 or 26/3/2004.
The template I was using took advantage of code-first EF4, but I don't have any reason to suspect that it hasn't been fixed generally, since the javascript is working properly too.
So if you are using MVC2 this may still be a problem, but the best solution which I've found is to use MVC4 as long as it is available to you.
As far as i know the RangeAttribute can only validate number on client side, you'll have to write a custom javascript validator for this to work...
check out http://haacked.com/archive/2009/11/19/aspnetmvc2-custom-validation.aspx for an example on how to do this.