I have my data annotation as below:
[RegularExpression("Basement|LL|Concourse|Plaza|1|2|3|4|5|6|7|8|9|10|14|15|16|17", ErrorMessage = "Enter Floor #, or Basement, LL, Concourse, Plaza")]
[Display(Name = "Floor")]
public string Floor { get; set; }
The page element:
#Html.TextBoxFor(m => m.Floor, new { #class = "form-control", id = "employeeFloor" })
Validation fails when I enter 10, 14, 15, 16, or 17. What am I doing wrong here?
TIA
Related
I am looking forward to get a linq query for populating list of teachers and their respective divisons.
Here I have 2 classes Teacher and Division which are related by DivisionGroupID - GroupID
public class Teacher
{
public int ID { get; set; }
public string Name { get; set; }
public List<Division> lstDivison {get;set;}
public int DivisionGroupID { get; set; }
}
public class Division
{
public int GroupID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
In main method List of both Teacher and Division will be populated
static void Main(string[] args)
{
Teacher obj = new Teacher { ID = 1, DivisionGroupID = 11, Name = "abcd" };
Teacher obj1 = new Teacher { ID = 2, DivisionGroupID = 12, Name = "efgh" };
List<Teacher> objList = new List<Teacher>();
objList.Add(obj);
objList.Add(obj1);
Division dv = new Division { GroupID = 11 ,Name="Division1",Description="first" };
Division dv1 = new Division { GroupID = 11, Name = "Division2", Description = "second" };
Division dv2 = new Division { GroupID = 11, Name = "Division3", Description = "third" };
Division dv3 = new Division { GroupID = 12, Name = "Division4", Description = "fourth" };
Division dv4 = new Division { GroupID = 12, Name = "Division5", Description = "fifth" };
Division dv5 = new Division { GroupID = 12, Name = "Division6", Description = "sixth" };
List<Division> lstDiv = new List<Division>();
lstDiv.Add(dv);
lstDiv.Add(dv1);
lstDiv.Add(dv2);
lstDiv.Add(dv3);
lstDiv.Add(dv4);
lstDiv.Add(dv5);
}
The requirement here is to get the list of teachers and populate the sublist of divisions each teachers holding. I got the solution based on 2 approaches.
Using sub query approach :
var upd = from teacher in objList
select new Teacher
{
ID = teacher.ID,
Name = teacher.Name,
lstDivison = (from div in lstDiv
where div.GroupID == teacher.DivisionGroupID
select new Division
{
Name = div.Name,
Description = div.Description
}).ToList()
};
Using Foeach loop through Teacher collection(objList) and updating the lstDivision
objList.ForEach(x => x.lstDivison = lstDiv
.Where(y => y.GroupID == x.DivisionGroupID)
.Select(p => new Division { Name = p.Name, Description = p.Description })
.ToList());
Both of these approaches will give me the result. But i am looking forward a better approach in as part of my project requirement which has to improve the query performance. Could you please suggest which is the best approach to handle this situation?
use yours teacher object to populate list of divisions under it. as my understanding that how it was designed class structure.
//populate property in object
objList.ForEach(x => {
x.lstDivison = lstDiv.Where(w=> w.GroupID == x.DivisionGroupID).ToList();
});
objList.Dump();
I want to do a whats app style chat app.
So, I have a message table as columns like
senderName, receiverName, message, messageTime
and I have Users table as columns like
ID, UserName
I want to show whats app style window that shows last messages with names.
So user can click it and see all messages from this user.
For example
John
Hi, Iam here. (date)
Gabriel
That was true. (date)
db.Messages
.Where(x => x.senderName == sessionName|| x.receiverName == sessionName).OrderByDescending(x =>
x.Id).ToList()
.GroupBy(m => new { V = m.senderName , V1 = m.receiverName })
.Select(x => x.First());
I have tried this. But this has problems.
The following is a working example of what you are looking for; showing the latest message (by highest id) for a sender for a particular session
void Main()
{
var messages = GetMessages();
var latest = messages
.GroupBy(m => m.SessionName)
.Select(grp => grp.OrderByDescending(g => g.Id).First())
.ToList();
latest.ForEach(l => Console.WriteLine($"{l.SenderName}\r\n{l.MessageText} ({l.SendDate:yyyy/MM/dd HH:mm:ss})"));
}
// Define other methods and classes here
public class Message
{
public int Id { get; set; }
public string SessionName { get; set; }
public string SenderName { get; set; }
public string ReceiverName { get; set; }
public string MessageText { get; set; }
public DateTime SendDate { get; set; }
}
public IEnumerable<Message> GetMessages()
{
return new[]
{
new Message
{
Id = 1,
SessionName = "Savage",
SenderName = "James T. Kirk",
ReceiverName = "Kahn",
MessageText = "Why would a Starfleet admiral ask a three-hundred-year-old frozen man for help?",
SendDate = new DateTime(2320, 5, 19, 15, 0, 0)
},
new Message
{
Id = 2,
SessionName = "Savage",
SenderName = "Kahn",
ReceiverName = "James T. Kirk",
MessageText = "Because I am better.",
SendDate = new DateTime(2320, 5, 19, 15, 0, 10)
},
new Message
{
Id = 3,
SessionName = "Savage",
SenderName = "James T. Kirk",
ReceiverName = "Kahn",
MessageText = "At what?",
SendDate = new DateTime(2320, 5, 19, 15, 0, 20)
},
new Message
{
Id = 4,
SessionName = "Savage",
SenderName = "Kahn",
ReceiverName = "James T. Kirk",
MessageText = "Everything. Alexander Marcus needed to respond to an uncivilized threat in a civilized time, and for that, he needed a warrior's mind - my mind - to design weapons and warships.",
SendDate = new DateTime(2320, 5, 19, 15, 0, 30)
}
};
}
Product and Company are in a many-to-one child-parent relationship:
[ElasticType(Name = "product", IdProperty = "ProductId")]
internal class Product
{
public int ProductId { get; set; }
public string Title { get; set; }
}
[ElasticType(Name = "company", IdProperty = "CompanyId")]
public class Company
{
public int CompanyId { get; set; }
public string CompanyName { get; set; }
}
In the mapping of Product, I did:
Func<PutMappingDescriptor<Product>, PutMappingDescriptor<Product>> descriptor = m => m
.MapFromAttributes()
.AllField(a => a.Enabled(false))
.SetParent<Company>();
I created a parent and child:
var company = new Company {
CompanyId = 1,
CompanyName = "XYZ Company"
};
var p2 = new Product{
ProductId = 2,
Title = "ABC Product"
};
es.Index(company);
The problem is then, how do I index p2? With only the Index method, I can only do es.Index(p2). But where do I indicate that p2 should be indexed under the parent company?
Basically I wanted a NEST solution to PUT /myindex/product/2?parent=1.
The closest answer I found so far is in https://stackoverflow.com/a/23953742/1124270. But the answer uses bulk inserts like the following where you have a .Parent method in the chaining to specify the parent's ID:
var bulkResponse = _client.Bulk(b => b
.Index<Address>(bd => bd.Object(new Address { Name = "Tel Aviv", Id = 1 }).Index("test"))
.Index<Person>(bd => bd.Index("test").Object(new Person {Id = 5, Address = 1, Name = "Me"}).Parent(1)));
If you are looking for PUT /myindex/product/2?parent=1 request.
You can do this in the NEST in a such way:
var indexResponse = client.Index(p2, descriptor => descriptor
.Parent(company.CompanyId.ToString()));
which generates below request to elasticsearch
StatusCode : 400,
Method : PUT,
Url : http : //localhost:9200/indexname/product/2?parent=1,
Request : {
"productId" : 2,
"title" : "ABC Product"
}
I've got this Kendo Scheduler that is displayed in the View but without any data.
The Scheduler on the View:
#(Html.Kendo().Scheduler<ProjName.Models.ScheduleInspectionModel>()
.Name("scheduler")
.Views(views =>
{
views.DayView();
views.WorkWeekView();
views.WeekView();
views.MonthView(mv => mv.Selected(true));
views.AgendaView();
})
.Timezone("Etc/UTC")
.DataSource(d => d
.Read("ControllerName", "GetScheduleInspections")
)
)
The datasource invokes the controller method below:
public ActionResult GetScheduleInspections([DataSourceRequest]DataSourceRequest request)
{
ScheduleInspectionModel sim = new ScheduleInspectionModel();
var gsio = sim.getScheduleInspections();
List<ScheduleInspectionModel> list = new List<ScheduleInspectionModel>();
if (gsio.scheduleinspections != null)
{
foreach (wsScheduleInspection.scheduleInspectionOutput scheduleInspection in gsio.scheduleinspections)
{
ScheduleInspectionModel sim2 = new ScheduleInspectionModel
{
GlobalEquipConditionId = scheduleInspection.globalEquipmentCondition.id,
Description = scheduleInspection.globalEquipmentCondition.code,
Start = DateTime.Now,
End = DateTime.Now.AddHours(2),
Title = scheduleInspection.globalEquipmentCondition.code,
IsAllDay = true
};
list.Add(sim2);
}
}
return Json(list.ToDataSourceResult(request));
}
But this method is never run, despite being on the Scheduler Datasource property. It should run that method and return a list of inspections. I don't know why isn't the method being hit. With a Kendo Grid, for example, the method on the Datasource Read is hit as soon as the page is loaded.
Try making sure your definition has these two items as I think they are required.
.Date(new DateTime(2013, 6, 13))
.StartTime(new DateTime(2013, 6, 13, 7, 00, 00))
EDIT
I was able to get the following code to work:
Model
// NOTE: It's important that your model class implements ISchedulerEvent
public class TaskViewModel : ISchedulerEvent
{
public string Title { get; set; }
public string Description { get; set; }
public bool IsAllDay { get; set; }
public DateTime Start { get; set; }
public DateTime End { get; set; }
public string StartTimezone { get; set; }
public string EndTimezone { get; set; }
public string RecurrenceRule { get; set; }
public string RecurrenceException { get; set; }
}
SchedulerController.cs
public class SchedulerController : Controller
{
// GET: Scheduler
public ActionResult Index()
{
var model = new SchedulerViewModel();
// In this case, it doesn't matter what this model is really since we're using AJAX binding
return View(model);
}
// I usually have my binding methods for Kendo use HttpPost
[HttpPost]
public ActionResult GetData([DataSourceRequest] DataSourceRequest request)
{
var data = new List<TaskViewModel>
{
new TaskViewModel
{
Start = new DateTime(2014, 12, 1, 8, 0, 0),
End = new DateTime(2014, 12, 1, 17, 0, 0),
Title = "Task 1"
}
};
return Json(data.ToDataSourceResult(request));
}
}
Index.cshtml (view)
#(Html.Kendo().Scheduler<TaskViewModel>()
.Name("scheduler")
.Views(views =>
{
views.DayView();
views.WorkWeekView();
views.WeekView();
views.MonthView(mv => mv.Selected(true));
views.AgendaView();
})
.Timezone("Etc/UTC")
.DataSource(d => d
.Read("GetData", "Scheduler")
))
If this doesn't work for you, I would make sure your versions (for Kendo, jQuery, etc) are correct. Hope this helps.
Yes, Scheduler read is called as soon as it is loeded. But it may not be getting data in proper format as it expects. So it is not able to bind the data. If you can check for these modification:
1) Define the "Model" in "DataSource" of scheduler as defined in this example.
2) Also the action method should return object of "MyModel" class(model on which scheduler is defined)
not "ScheduleInspectionModel" class.
Newbie to LINQ, and trying to write the following query...
select
f.Section_ID,
f.Page_ID,
f.SortOrder,
f.Type
from
(
select
Section_ID,
min(SortOrder) as minSortOrder
from
ContentPages
group by
Section_ID
) as x
inner join
ContentPages as f on
f.Section_ID = x.Section_ID and
f.SortOrder = x.minSortOrder;
Notes:
'Section' has many 'ContentPages'
Sections are ordered by a 'SortOrder' field
ContentPages are also ordered by a 'SortOrder' field
Table: Section
Section_ID....Name.......SortOrder
....1.........One..........1......
....2.........Two..........3......
....3.........Three........2......
Table: ContentPage
Page_ID.......Section_ID.......Title..............SortOrder
....11.............1.......... Page One.............1......
....12.............1...........Page Two.............3......
....13.............2...........Page Three...........2......
....16.............2.......... Page Four............4......
....17.............2...........Page Eight...........5......
....18.............1...........Page Ten.............6......
The above query could possibly be written another way, so here's what I'm trying to do:
I need to return a list of the first ContentPage within each Section (when sorted by ContentPage.SortOrder)
Sort results by Section.SortOrder
Show Section.Name (join on Section_ID?) in the result as well
Last 2 points are not covered by the sql query above and are more of a 'nice to have'...
Desired Result
Page_ID.......Section_ID...SectionName.....Title..............SortOrder
....11.............1.........One......... Page One.............1......
....13.............2.........Two..........Page Three...........2......
Any help is appreciated. Thanks!
Here's my first attempt at it:
from sectionPage in pages
group sectionPage by sectionPage.Section_ID into sectionGroup
join page in pages on sectionGroup.Key equals page.Section_ID
where page.SortOrder == sectionGroup.Min(p => p.SortOrder)
orderby page.SortOrder
select page;
What happens is first we create a group on the section id so that we can get the minimum sort order later. Next, we join a new reference to pages in on the section id, and filter by SortOrder being the minimum from the section group. Note, for simple expressions like the Min() call, I prefer the inline lambda expression over another query.
Finally, we add an orderby to order the pages, and we return the page (note you can change this to certain fields if you prefer).
I think this is what you're looking for...
internal class Section
{
public int SectionId { get; set; }
public string Name { get; set; }
public int SortOrder { get; set; }
}
internal class ContentPage
{
public int PageId { get; set; }
public int SectionId { get; set; }
public string Title { get; set; }
public int SortOrder { get; set; }
}
static void Main(string[] args)
{
List<Section> sections = new List<Section>();
sections.Add(new Section() { SectionId = 1, Name = "One", SortOrder = 1 });
sections.Add(new Section() { SectionId = 2, Name = "Two", SortOrder = 3 });
sections.Add(new Section() { SectionId = 3, Name = "Three", SortOrder = 2 });
List<ContentPage> contentPages = new List<ContentPage>();
contentPages.Add(new ContentPage() { PageId = 11, SectionId = 1, Title = "Page One", SortOrder = 1 });
contentPages.Add(new ContentPage() { PageId = 12, SectionId = 1, Title = "Page Two", SortOrder = 3 });
contentPages.Add(new ContentPage() { PageId = 13, SectionId = 2, Title = "Page Three", SortOrder = 2 });
contentPages.Add(new ContentPage() { PageId = 16, SectionId = 2, Title = "Page Four", SortOrder = 4 });
contentPages.Add(new ContentPage() { PageId = 17, SectionId = 2, Title = "Page Eight", SortOrder = 5 });
contentPages.Add(new ContentPage() { PageId = 18, SectionId = 1, Title = "Page Ten", SortOrder = 6 });
var items = from section in sections
orderby section.SortOrder
join contentPage in
(from contentPage in contentPages
orderby contentPage.SortOrder
group contentPage by contentPage.SectionId into grp
select grp.FirstOrDefault())
on section.SectionId equals contentPage.SectionId
select new
{
PageId = contentPage.PageId,
SectionId = section.SectionId,
SectionName = section.Name,
Title = contentPage.Title,
SortOrder = section.SortOrder
};
foreach (var newItem in items)
{
Console.WriteLine(string.Format("{0}\t{1}\t{2}\t{3}\t{4}", newItem.PageId, newItem.SectionId, newItem.SectionName, newItem.Title, newItem.SortOrder));
}
}
Note that the sample data you provided shows a sort order of 3 for section 2, but your sample results list its sort order as 2.