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+

Wednesday, March 22, 2017

MVC attribute DataFormatString

Issue

Needed to format dates in an MVC model.  I wanted to use the attribute decorators for the properties.
I just could not remember the syntax for DataFormatString

  [Display(Name = "Date Notified")]
  [DisplayFormat(DataFormatString = "{0:dd-MMM-yyyy} ")]
  public DateTime? Received_Date { get; set; }

{0:dd MMMM yyyy}    -    gives 24 February 2006
{0:MMM dd}    -    gives Feb 24 (substitue MMM with MMMM for the full month name instead of abbreviation)
{0:dd/MM/yy}    -    gives 24/02/06
{0:dd/MM/yyyy}    -    gives 24/02/2006


Source

https://social.msdn.microsoft.com/Forums/windows/en-US/bb8b1e0e-bd41-4206-9895-4e79463b9bc6/dataformatstring-for-datetime-column?forum=winformsdatacontrols

Monday, March 20, 2017

Remove Timestamp from a DateTime variable in MVC Razor

Issue

I needed to format some dates on a View and could not remember the syntax.

Define in your model a readonly variable

public String startDateFormatted { get { return String.Format("{0:dd/MM/yyyy}", startDate); } }
Then in the razor syntax just use DisplayFor

DisplayFor(modelItem => item.startDateFormatted)

OR

You can use the assign the date formatting attributes on the property of the model

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

Source

http://stackoverflow.com/questions/11122171/asp-net-mvc-razor-remove-time-from-datetime-property

Friday, March 17, 2017

AJAX Controller Action Method from a Razor View


Issue

How to call to a controller action from jquery?

Method 1 : Using jQuery Ajax Get call (partial page update).
Suitable for when you need to retrieve jSon data from database.
Controller's Action Method
[HttpGet]
public ActionResult Foo(string id)
{
    var person = Something.GetPersonByID(id);
    return Json(person, JsonRequestBehavior.AllowGet);
}
Jquery GET
function getPerson(id) {
    $.ajax({
        url: '@Url.Action("Foo", "SomeController")',
        type: 'GET',
        dataType: 'json',
        // we set cache: false because GET requests are often cached by browsers
        // IE is particularly aggressive in that respect
        cache: false,
        data: { id: id },
        success: function(person) {
            $('#FirstName').val(person.FirstName);
            $('#LastName').val(person.LastName);
        }
    });
}
Person class
public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
Key to note here is that the data line id must match the name of the variable in the Action result parameter list.

Source

http://stackoverflow.com/questions/14049817/in-asp-net-mvc-all-possible-ways-to-call-controller-action-method-from-a-razor
http://stackoverflow.com/questions/1194104/jquery-ajax-ajax-passing-parameters-to-callback-good-pattern-to-use
http://stackoverflow.com/questions/15576548/how-to-pass-parameters-in-get-requests-with-jquery

Handling Nullable Guid


Issue

How to cast nullable Guid inline?

 return source ?? Guid.Empty;

Source

http://stackoverflow.com/questions/5498528/how-to-convert-guid-to-guid

Thursday, March 9, 2017

Linq to SQL where clause against collection of list values

Problem:

I was working on an EF Model with Linq 2 SQL.  
I sometimes depend on the SQL sp's which depend upon subqueries

select x.value1,x.value2 from myTable x
where
x.primarykey in (select foreignkey from myTable2 where name="jump")

How do you accomplish this in Linq?


Solution:

var innerquery = (from iq in myTable2
where iq.name.Equals("jump")
select iq.id);

var Ids = innerquery.ToList();

var query = 
from mt in myTable
where
Ids.Any(qq => mt.Id.Equals(qq)
select mt);

Source:

http://stackoverflow.com/questions/1075540/linq-to-sql-how-to-do-where-column-in-list-of-values

.NET: Combining two generic lists

 

Problem:

I had created two List collections based on a some predicate searches against an EF model.
I need to blend the 2 lists together


Solution:

The AddRange method did the trick
List<Type> list1;
List<Type> list2;

List<Type> combined;
combined.AddRange(list1);
combined.AddRange(list2);

Source:

http://stackoverflow.com/questions/2002770/net-combining-two-generic-lists


Wednesday, March 1, 2017

Convert nullable bool? to bool

I was trying to set up a complex query screen in MVC.  I have some checkboxes which may or may not be used in the query.  I needed to cast the nullable bool to bool


bool newBool = x.HasValue ? x.Value : false;

Source:
http://stackoverflow.com/questions/6075726/convert-nullable-bool-to-bool