I have model which as a nullable Date field. Everything works as expected except in the circumstance where I am editing the model and I want to remove the date. Then validation is triggered:
How do I allow removal while also keeping validation on a partially filled in date? Is worth mentioning that are using a custom EditorTemplate:
Here is the code for the editor template:
#model DateTime?
#{
var months = new[] {
new { Value = "", Display="" },
new { Value = "1", Display = "01-Jan" },
new { Value = "2", Display = "02-Feb" },
new { Value = "3", Display = "03-Mar" },
new { Value = "4", Display = "04-Apr" },
new { Value = "5", Display = "05-May" },
new { Value = "6", Display = "06-Jun" },
new { Value = "7", Display = "07-Jul" },
new { Value = "8", Display = "08-Aug" },
new { Value = "9", Display = "09-Sep" },
new { Value = "10", Display = "10-Oct" },
new { Value = "11", Display = "11-Nov" },
new { Value = "12", Display = "12-Dec" }
};
var monthSelect = new SelectList(months, "Value", "Display", Model.HasValue ? Model.Value.Month.ToString() : "");
var days = new List<String>();
days.Add("");
days.AddRange(Enumerable.Range(1, 31).Select(s => s.ToString()));
var daySelect = new SelectList(days.Select(x => new KeyValuePair<string, string>(x, x)), "Key", "Value", Model.HasValue ? Model.Value.Day.ToString() : "");
}
#Html.DropDownList("Month", monthSelect)
#Html.DropDownList("Day", daySelect)
#Html.TextBox("Year", (Model.HasValue ? Model.Value.ToString("yyyy") : string.Empty), new { style="width:75px;" })
The code on the edit view is dead simple:
#Html.EditorFor(model => model.MyDate)
Related
I'm using BOT Framework v 3 i have an adaptive card that takes input from the user and i want the values in Dropdown to be dynamic is it possible.here is the adaptive card design code as you can see I have entered the choices manually instead it want it to be dynamic from the database
var card = new AdaptiveCard()
{
Body = new List<CardElement>()
{
new TextBlock()
{
Color = TextColor.Attention,
Weight = TextWeight.Bolder,
Size = TextSize.Medium,
Text = "Select a title",
},
new ChoiceSet()
{
Id = "title",
Style = ChoiceInputStyle.Compact,
IsRequired = false,
IsMultiSelect = false,
Value = "1",
Choices = new List<Choice>()
{
new Choice()
{
Title = "Swiss cargo",
Value = "Swiss cargo",
},
new Choice()
{
Title = "ticket booking",
Value = "ticket booking",
},
},
},
},
};
Assuming you can get your data into a list of strings, your Adaptive Card can easily be constructed dynamically using Linq. If you want to keep using the same Adaptive Cards library, it would look like this:
var data = new List<string> { "Swiss cargo", "ticket booking" };
var card = new AdaptiveCard()
{
Body = new List<CardElement>()
{
new TextBlock()
{
Color = TextColor.Attention,
Weight = TextWeight.Bolder,
Size = TextSize.Medium,
Text = "Select a title",
},
new ChoiceSet()
{
Id = "title",
Style = ChoiceInputStyle.Compact,
IsRequired = false,
IsMultiSelect = false,
Value = "1",
Choices = data.Select(item => new Choice { Title = item, Value = item }).ToList(),
},
},
};
I am having a hard time converting this generic template of facebook to C#. I am not sure if i converted it right. Below is the code i tried but is not rendering on messenger. Thank you.
curl -X POST -H "Content-Type: application/json" -d '{
"recipient":{
"id":"<PSID>"
},
"message":{
"attachment":{
"type":"template",
"payload":{
"template_type":"generic",
"elements":[
{
"title":"Welcome!",
"image_url":"https://petersfancybrownhats.com/company_image.png",
"subtitle":"We have the right hat for everyone.",
"default_action": {
"type": "web_url",
"url": "https://petersfancybrownhats.com/view?item=103",
"webview_height_ratio": "tall",
},
"buttons":[
{
"type":"web_url",
"url":"https://petersfancybrownhats.com",
"title":"View Website"
},{
"type":"postback",
"title":"Start Chatting",
"payload":"DEVELOPER_DEFINED_PAYLOAD"
}
]
}
]
}
}
}
}' "https://graph.facebook.com/v2.6/me/messages?access_token=<PAGE_ACCESS_TOKEN>"
This is what i tried in c# but it is not working. I am not sure if i converted it the proper way. Any help would be appreciated thank you.
Activity previewReply = stepContext.Context.Activity.CreateReply();
previewReply.ChannelData = JObject.FromObject(
new
{
attachment = new
{
type = "template",
payload = new
{
template_type = "generic",
elements = new
{
title = "title",
subtitle = "subtitle",
image_url = "https://thechangreport.com/img/lightning.png",
buttons = new object[]
{
new
{
type = "element_share,",
share_contents = new
{
attachment = new
{
type = "template",
payload = new
{
template_type = "generic",
elements = new
{
title = "x",
subtitle = "xx",
image_url = "https://thechangreport.com/img/lightning.png",
default_action = new
{
type = "web_url",
url = "http://m.me/petershats?ref=invited_by_24601",
},
buttons = new
{
type = "web_url",
url = "http://m.me/petershats?ref=invited_by_24601",
title = "Take Quiz",
},
},
},
},
},
},
},
},
},
},
});
await stepContext.Context.SendActivityAsync(previewReply);
The elements and buttons attributes need to be lists. Take a look at the example template below.
var attachment = new
{
type = "template",
payload = new
{
template_type = "generic",
elements = new []
{
new {
title = "title",
image_url = "https://thechangreport.com/img/lightning.png",
subtitle = "subtitle",
buttons = new object[]
{
new {
type = "element_share",
share_contents = new {
attachment = new {
type = "template",
payload = new
{
template_type = "generic",
elements = new []
{
new {
title = "title 2",
image_url = "https://thechangreport.com/img/lightning.png",
subtitle = "subtitle 2",
buttons = new object[]
{
new {
type = "web_url",
url = "http://m.me/petershats?ref=invited_by_24601",
title = "Take Quiz"
},
},
},
},
},
}
},
},
},
},
},
},
};
reply.ChannelData = JObject.FromObject(new { attachment });
Note, you only need to add a share_contents element to your template if your main template is different from the template you are trying to share. Otherwise, your button can just be new { type = "element_share" }, which makes the template far less complex.
Also, be sure to Whitelist all of your URLs and make sure all of the image URLs work properly - a couple of them weren't working properly. The template won't render if the URLs aren't Whitelisted and image links are broken.
Hope this helps!
This code which initializes an array with two hard-coded values is working perfectly fine:
var db = new GoogleGraph {
cols = new ColInfo[] {
new ColInfo { id = "", label = "Date", pattern ="", type = "string" },
new ColInfo { id = "", label = "Attendees", pattern ="", type = "number" }
}.ToList(),
rows = new List<DataPointSet>()
};
db.cols.AddRange(listOfValues.Select(p => new ColInfo { id = "", label = p, type = "number" }));
This code which attempts to add some dynamically generated values is not working:
var db = new GoogleGraph {
cols = new ColInfo[] {
new ColInfo { id = "", label = "Date", pattern ="", type = "string" },
new ColInfo { id = "", label = "Attendees", pattern ="", type = "number" },
listOfValues.Select(p => new ColInfo { id = "", label = p, type = "number" })
}.ToList(),
rows = new List<DataPointSet>()
};
How can I correctly implement the above snippet?
You can't pass an IEnumerable<T> to an initializer of T[] like that.
You can do what you want by putting the hard-coded objects in their own collection, then concatenating the dynamic ones:
var db = new GoogleGraph {
cols =
new ColInfo[] {
new ColInfo { id = "", label = "Date", pattern ="", type = "string" },
new ColInfo { id = "", label = "Attendees", pattern ="", type = "number" }
}
.Concat(listOfValues.Select(p =>
new ColInfo { id = "", label = p, type = "number" }))
.ToList(),
rows = new List<DataPointSet>()
};
Can someone please show me how to get a couple of dynamic select fields into the MVC Register Form that comes with Visual Studio 2013 MVC project?
I have modified the AspNetUsers table to include TitleId and SexId, I have also modified the registration form with just 2 extra fields:
#Html.TextBoxFor(m => m.TitleId, new { #placeholder = "Title *", #type = "number" })
#Html.TextBoxFor(m => m.SexId, new { #placeholder = "Sex *", #type = "number" })
The form works but I need TitleId and SexId to be Select fields with data from the respective Title and Sex tables.
Any help would be much appreciated.
On reflection it seems a waste to take trips to the database for these records, they will never change so I have added the following into the Register form:
#Html.DropDownListFor(m => m.TitleId, new List<SelectListItem> { new SelectListItem { Value = "", Text = "Title" }, new SelectListItem { Value = "1", Text = "Mr" }, new SelectListItem { Value = "2", Text = "Mrs" }, new SelectListItem { Value = "3", Text = "Miss" }, new SelectListItem { Value = "4", Text = "Dr" } })
#Html.DropDownListFor(m => m.SexId, new List<SelectListItem> { new SelectListItem { Value = "", Text = "Sex" }, new SelectListItem { Value = "1", Text = "Male" }, new SelectListItem { Value = "2", Text = "Female" }})
As a result the Register form generates and functions correctly.
I am having trouble this DropDownListFor()
I have test controller:
model.COUNTRYNAME = "Swizerland";
ViewBag.Selecter = new SelectList(new[]
{
new SelectListItem { Text = "USA", Value = "USA" },
new SelectListItem { Text = "Swizerland", Value = "Swizerland", Selected =true},
new SelectListItem { Text = "Russia", Value = "Russia" },
}, "Text", "Value", model.COUNTRYNAME);
in View
#Html.DropDownListFor(x => Model.COUNTRYNAME , (SelectList)ViewBag.Selecter)
The DropDownListFor does not have select value, it always select first value.
What is wrong?
If i use DropDownList
#Html.DropDownList("COUNTRYNAME" , (SelectList)ViewBag.Selecter)
It also don't work.
But if I use
#Html.DropDownListFor("AAAAAAA" , (SelectList)ViewBag.Selecter)
It's work fine and select 2nd value! What happens? I don't understand.
Thanks
Try like this:
model.COUNTRYNAME = "Swizeland";
ViewBag.Selecter = new[]
{
new SelectListItem { Text = "USA", Value = "USA" },
new SelectListItem { Text = "Swizeland", Value = "Swizeland" },
new SelectListItem { Text = "Russia", Value = "Russia" },
};
return View(model);
and in the view:
#Html.DropDownListFor(
x => x.COUNTRYNAME,
(IEnumerable<SelectListItem>)ViewBag.Selecter
)
but a better way is to use a view model:
model.SelectedCountry = "Swizeland";
model.Countries = new[]
{
new SelectListItem { Text = "USA", Value = "USA" },
new SelectListItem { Text = "Swizeland", Value = "Swizeland" },
new SelectListItem { Text = "Russia", Value = "Russia" },
};
return View(model);
and in the view:
#Html.DropDownListFor(x => x.SelectedCountry, Model.Countries)