What's wrong with my Kendo UI ClientTemplate code? - kendo-ui

columns.Bound(e => e.CreateDate).ClientTemplate("# if (CreateDate != null){#"
+ #Html.Action("ChangeDate", "Home", new {date = "#= CreateDate #"}) +
"#} else if ((CreateDate) == null)" +
"{#-#}#");
The ChangeDate action add year to date, but I got the following error:
The parameters dictionary contains a null entry for parameter 'date' of non-nullable type 'System.DateTime' for method 'System.String ChangeDate(System.DateTime)'
as error says the date value is null .

If CreateDate can be null, you need to check for data.CreateDate instead.
columns.Bound(e => e.CreateDate).ClientTemplate("# if (data.CreateDate != null){#"
+ #Html.Action("ChangeDate", "Home", new {date = "#= CreateDate #"}) +
"#} else if ((data.CreateDate) == null)" +
"{#-#}#");

Related

System.InvalidOperationException: 'Sequence contains no matching element'

May I know why that error keeps pointing to "String isEmailVerified ...."?
public JsonResult GetMemberCounts([FromBody] ChartFilterRequest filter)
{
DateTime startDate = DateTime.Parse(filter.MainFilter.First(m => m.Name == "startDate").Value as string);
DateTime endDate = DateTime.Parse(filter.MainFilter.First(m => m.Name == "endDate").Value as string);
String isEmailVerified = filter.MainFilter.First(m => m.Name == "isEmailVerified").Value as string;
var data = _dashboardComponent.GetMemberCount(startDate, endDate, isEmailVerified);
return new JsonResult(data);
}
Try using FirstOrDefault and LastOrDefault instead of First and Last, these methods will return the default value of the type they are invoked for if no elements match the lambda expression you provide as a parameter.
In your project, You just use filter.MainFilter.First(xxxx) to select the data, So if no elements match the lambda expression you provide as a parameter,First() will throw an exception, So here will report this error.

value cannot be null with htmlagility pack selectnodes

I have error "Value cannot be null" when taking data from div with htmlagility pack,
my code is:
var varitem = doc.DocumentNode.SelectNodes("//*[#id='title']")
.SelectMany(x => x.Descendants("div"));
I tryed to chek if value is null with code:
if (object.ReferenceEquals(doc.DocumentNode.SelectNodes("//*[#id='title']")
.SelectMany(x => x.Descendants("div")), null))
but again I get same error.

How do you format a datetime field in a SelectList in MVC?

I'm creating a new SelectList object and concatenating 2 fields, one is a string and the other is a DateTime. Basically I'm building a dropdownlist displaying the name and date of an event. When I try running the code I get the error ' LINQ to Entities does not recognize the method 'System.String ToShortDateString()' method, and this method cannot be translated into a store expression.'
Any help would greatly be appreciated.
ViewBag.EventKey = new SelectList((from e in db.Events where e.Active == true select new { e.PKey, EventInfo = e.Name + " " + e.EventDate.ToShortDateString() }), "Pkey", "EventInfo");
The problem is that you're calling .ToShortDateString in statement that is going to be translated to DB query. Try following:
ViewBag.EventKey = new SelectList((from e in db.Events where e.Active == true select e).ToArray().Select(e => new { e.PKey, EventInfo = e.Name + " " + e.EventDate.ToShortDateString() }), "Pkey", "EventInfo");

how to show more than one item with SelectList (it's a dropdown) , im using mvc3

this is my code:
ViewBag.idprofesor = new SelectList(db.Profesor, "IDProfesor", "nombre");
this code generate a dropdown that only shows the name (nombre) of the teachers (profesor) in the database, id like the dropdown to show the name and the lastname of the teacher.
You may have to manually create a ist of SelectListItems that manually specify the fields you want. Like so:
List<SelectListItem> professors = new List<SelectListItem>();
foreach(var professor in db.Professors) {
professors.Add( new SelectListItem() { Text = professor.Fn + " " + professor.Ln, Value = professor.Id } );
}
ViewVag.Professors = professors;
Then in your view:
Html.DropDownListFor( m => m.Professor, ViewBag.Professors );
You just have to pass an IEnumerable of SelectListItem to the constructor. Using Linq to select from the Professor collection:
#{
var mySelectList =
db.Professor.Select(prof => new SelectListItem(
Text = prof.lastName + ", " + prof.nombre,
Value = prof.IDProfessor
)
).AsEnumerable();
}
Using Razor, you should be able to create the drop down list like this:
#Html.DropDownList("mydropdownlist", mySelectList)
To bind it to a property in your model, the syntax is slightly different:
#Html.DropDownListFor(model => model.someproperty, mySelectList)

Dealing with a null datetime element within xml using linq

HI
I have an example document that looks like
<ItemEntry>
<PurchaseDate>2010-03-18T20:36:32.81108+13:00</PurchaseDate>
<StoreGUID>0a0324ad-5f99-486a-a2d0-870bc6991e9f</StoreGUID>
<ExpiryDate />
<CardID>111111</CardID>
<PurchaseAmount>0</PurchaseAmount>
<RedeemedAmount />
<EntryType>1</EntryType>
<RedeemedDate />
<SalesAssistantID>0</SalesAssistantID>
</ItemEntry>
As you can see there are couple of elements ExpiryDate and RedeemedDate are are empty.
var q = from c in xml.Elements("ItemEntry")
select new mdDetail {
PurchaseDate = (DateTime)c.Element("PurchaseDate"),
StoreGUID = (Guid)c.Element("StoreGUID"),
ExpiryDate = (DateTime?)c.Element("ExpiryDate")??DateTime.MinValue,
CardID = (int)c.Element("CardID"),
PurchaseAmount = (double)c.Element("PurchaseAmount"),
RedeemedAmount = (double?)c.Element("RedeemedAmount"),
EntryType = (int)c.Element("EntryType"),
RedeemedDate = (DateTime?)c.Element("RedeemedDate") ??DateTime.MinValue,
SalesAssistantID = (int)c.Element("SalesAssistantID"),
}
;
foreach (var item in q)
{
}
I am not sure how to deal with the null element value,
I have tried ??DateTime.MinValue and ??null however both give me a "
String was not recognized as a valid DateTime." error.
Any suggestions?
Thank you
ExpiryDate = String.IsNullOrEmpty((string)c.Element("ExpiryDate"))?
DateTime.MinValue : DateTime.Parse((string)c.Element("ExpiryDate"))
"You could also use null instead of DateTime.MinValue if ExpireyDate is
declared to be nullable"
#Gabe, you can't just use null - you need to use (DateTime?)null because the compiler won't know how to convert null into a DateTime object
So if you want the value to just be a blank (null) this would be the final code:
ExpiryDate = String.IsNullOrEmpty(c.Element("ExpiryDate").Value)?
(DateTime?)null : DateTime.Parse(c.Element("ExpiryDate").Value)
Assuming DateTime is a declared a nullable (DateTime?)

Resources