Showing posts with label MVC Razor. Show all posts
Showing posts with label MVC Razor. Show all posts

Monday, August 21, 2017

C# date comparison MVC Razor

Issue:
I had a problem with a conditional test which was failing.  I was trying to compare two dates but the condition was not firing in my view.




claimPayments.Payment_Date.Value.ToString("dd-MMM-yyyy") == temp.ToString("dd-MMM-yyyy")


changed it to


claimPayments.Payment_Date.Value.Ticks.Equals(temp.Ticks)


This fixed the problem.  Using the ticks function gets around the issue of trying to match date formats.



Friday, March 24, 2017

DateTime DisplayFormat seems to have no effect (MVC5, Razor)

Issue:

I had declared in my model the following

[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime? AuditDate { get; set; }

  @Html.EditorFor(model => model.AuditDate, new { htmlAttributes = new { @class = "form-control" } })
           
But it was not working!

Then I noted that the Default Displayformat is defined to work with @Html.Display or @Html.DisplayFor and inorder to get it working with Editor controls you need to apply ApplyFormatInEditMode = true.



Source:

https://forums.asp.net/t/1857087.aspx?DateTime+DisplayFormat+seems+to+have+no+effect+MVC4+Razor+

Tuesday, October 4, 2016

MVC Razor Syntax: Can't implicitly convert bool? to bool

Problem:

My model had several variables which were nullable Booleans.  My issue was how to bind them in the Razor syntax without getting an error.


I had tried
@Html.CheckBoxFor(model=>(bool) model.LengthConfinementLife)


which will compile but fails on a runtime error.


Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.

Solution:



@Html.CheckBox("LengthConfinementLife", Model.LengthConfinementLife?? false)
@Html.Label("LengthConfinementLife", "My Dates are Flexible")


Source:

http://stackoverflow.com/questions/6849774/mvc3-creating-checkbox-for-nullable-boolean