I have the following code in my view
<td>#Html.DropDownListFor(e => e[0].Reason, new List<SelectListItem>
(
new[]
{
new SelectListItem { Text = " -Select- ", Value = "-Select-" },
new SelectListItem { Text = " Price ", Value = "Price" },
new SelectListItem { Text = " 3P ", Value = "3P" },
new SelectListItem { Text = " Freight Collect ", Value = "Freight Collect" },
new SelectListItem { Text = " Billing ", Value = "Billing" },
new SelectListItem { Text = " Business Closure ", Value = "Business Closure" },
new SelectListItem { Text = " Customer Service ", Value = "Customer Service" },
new SelectListItem { Text = " Quality ", Value = "Quality" },
new SelectListItem { Text = " Service ", Value = "Service" },
new SelectListItem { Text = " Business Relocate ", Value = "Business Relocate" },
new SelectListItem { Text = " Change in Relationships ", Value = "Change in Relationships" },
new SelectListItem { Text = " Different Account Code ", Value = "Different Account Code" },
new SelectListItem { Text = " Economic Downturn ", Value = "Economic Downturn" },
new SelectListItem { Text = " BIC ", Value = "BIC" },
new SelectListItem { Text = " Credit/Collections ", Value = "Credit/Collections" }
}
))</td>
Every time when the page loads selected value gets changes
Can you please tell me how to set the selected value.
Thanks in Advance.
Add your list to ViewBag as type IEnumerable-SelectListItem.
Then use this part of code.
#Html.DropDownListFor(e => e[0].Reason, new SelectList(ViewBag.YourSelectList, "Value", "Text","YourSelectedValue")
You can pass the selected value with model object. From the action method you can return the model object as follows
model obj=new model();
obj.Reason="Price";
return view(obj)
In the view the item with value "Price" get selected while loading itself
Or you can set the selected value using jquery
$(document).ready(function(){
$("#Reason").val("Price");
});
If you want set selected value in view:
#Html.DropDownListFor( e => e[0].Reason, new SelectList( new List<SelectListItem>(){
new SelectListItem { Text = " -Select- ", Value = "-Select-" },
new SelectListItem { Text = " Price ", Value = "Price" },
new SelectListItem { Text = " 3P ", Value = "3P" },
new SelectListItem { Text = " Freight Collect ", Value = "Freight Collect" },
new SelectListItem { Text = " Billing ", Value = "Billing" },
new SelectListItem { Text = " Business Closure ", Value = "Business Closure" },
new SelectListItem { Text = " Customer Service ", Value = "Customer Service" },
new SelectListItem { Text = " Quality ", Value = "Quality" },
new SelectListItem { Text = " Service ", Value = "Service" },
new SelectListItem { Text = " Business Relocate ", Value = "Business Relocate" },
new SelectListItem { Text = " Change in Relationships ", Value = "Change in Relationships" },
new SelectListItem { Text = " Different Account Code ", Value = "Different Account Code" },
new SelectListItem { Text = " Economic Downturn ", Value = "Economic Downturn" },
new SelectListItem { Text = " BIC ", Value = "BIC" },
new SelectListItem { Text = " Credit/Collections ", Value = "Credit/Collections" }
} , "Value", "Text", e[0].Reason ) )
For set selected value in controller, check answer: https://stackoverflow.com/a/17184481/1643075
ok, veena,
I left a comment suggesting how I would do it if it were me. However, you are looking for an answer based on your current implementation, so here's my take on that for now:
<td>
#{ var reasonList = new List<SelectListItem>(new[] {
new SelectListItem {
Text=" -Select-",
Value="-Select- "
},
new SelectListItem {
Selected = Model[0].Reason.Equals("Price"),
Text=" Price",
Value="Price"
},
new SelectListItem {
Selected = Model[0].Reason.Equals("3P"),
Text=" 3P",
Value="3P"
}
// etc etc...
});
}
#Html.DropDownListFor(m => m[0].Reason, reasonList)
</td>
However, I'd strongly suggest that you move this logic up into your controller/service code as the view really becomes littered when this kind of code is included. I'd also strongly question the rationale for examining the index elements in the way that you do. I'm presuming that you have ommitted indexes [1], [2] etc for brevity. If this is the case, then you should really use a foreach loop and go thro the elements sequentially in your code. Below is a very quick example of that too (the syntax may be slightly incorrect as i'm sending via ipad, so is untested):
#foreach(var item in Model) {
#: <td>
var reasonList = new List<SelectListItem>(new[] {
new SelectListItem {
Text=" -Select-",
Value="-Select- "
},
new SelectListItem {
Selected = item.Reason.Equals("Price"),
Text=" Price",
Value="Price"
},
new SelectListItem {
Selected = item.Reason.Equals("3P"),
Text=" 3P",
Value="3P"
}
// etc etc...
});
#Html.DropDownListFor(m => item.Reason, reasonList)
#: </td>
}
The solution is to put the selected value into the forth parameter of the SelectList object.
List<string> TheValuesYouWantToPresent = new List<string> { "one", "two", "three" };
string SelectedValue = "two";
List<SelectListItem> TheSelectListItems = TheValuesYouWantToPresent.Select(x => new SelectListItem { Text = x, Value = x}).ToList();
ViewBag.MyDropDownListDataWithASelectedItem = new SelectList(TheSelectListItems, "Text", "Value", SelectedValue);
Then in your MVC view, you do this:
#Html.DropDownListFor(model => model.DataBaseFieldToPopulate, ViewBag.MyDropDownListDataWithASelectedItem, "Invitation to select something from the list", new { #class = "a_CSS_class_name_of_your_choosing" })
Related
I'm using Adaptive Card version 1.2 and I'm trying to add style for Adaptive Column. The style is getting reflected in Adaptive Designer(Web Chat). But the same if I try to check in hosted Web Chat styles are not getting reflected. Also I would like to mention that I'm creating Adaptive card using C#.
But it is looking like this
The i'm using this for webchat (https://cdn.botframework.com/botframework-webchat/latest/webchat.js)
Here is my C# code
AdaptiveCard card = new AdaptiveCard(new AdaptiveSchemaVersion(1, 0));
card.Body.Add(new AdaptiveTextBlock()
{
Text = $"Hello User",
HorizontalAlignment = AdaptiveHorizontalAlignment.Center
});
card.Body.Add(new AdaptiveTextBlock()
{
FontType = AdaptiveFontType.Monospace,
Text = "Would you like to rate us",
Color = AdaptiveTextColor.Good,
Weight = AdaptiveTextWeight.Bolder,
HorizontalAlignment = AdaptiveHorizontalAlignment.Center,
Separator = true
});
card.Body.Add(new AdaptiveColumnSet()
{
Separator = true,
Columns = new List<AdaptiveColumn>()
{
new AdaptiveColumn()
{
Bleed = true,
Width = AdaptiveColumnWidth.Stretch,
Style = AdaptiveContainerStyle.Emphasis,
SelectAction = new AdaptiveSubmitAction()
{
Title = "1",
DataJson = "{ \"Type\": \"1\" }"
},
Items = new List<AdaptiveElement>()
{
new AdaptiveTextBlock()
{
Text = "1",
}
}
},
new AdaptiveColumn()
{
Bleed = true,
Width = AdaptiveColumnWidth.Stretch,
Style = AdaptiveContainerStyle.Accent,
SelectAction = new AdaptiveSubmitAction()
{
Title = "2",
DataJson = "{ \"Type\": \"2\" }"
},
Items = new List<AdaptiveElement>()
{
new AdaptiveTextBlock()
{
Text = $"2"
}
}
},
new AdaptiveColumn()
{
Bleed = true,
Width = AdaptiveColumnWidth.Stretch,
Style = AdaptiveContainerStyle.Good,
SelectAction = new AdaptiveSubmitAction()
{
Title = "3",
DataJson = "{ \"Type\": \"3\" }"
},
Items = new List<AdaptiveElement>()
{
new AdaptiveTextBlock()
{
Text = $"3"
}
}
},
new AdaptiveColumn()
{
Bleed = true,
Width = AdaptiveColumnWidth.Stretch,
Style = AdaptiveContainerStyle.Attention,
SelectAction = new AdaptiveSubmitAction()
{
Title = "4",
DataJson = "{ \"Type\": \"4\" }"
},
Items = new List<AdaptiveElement>()
{
new AdaptiveTextBlock()
{
Text = $"4"
}
}
}
}
});
Please help me on this
Can someone please help me how can I change the contents of Formats dropdown in richtexteditor ej2 syncfusion?
At the moment, default values are: Paragraph, Code, Quotation, Heading 1 etc.
I want to remove the Code, Quotation and add a new custom Format called "My Paragraph".
I have gone through the documentation and apparently, it is not listed.
Help will be appreciated.
Here is what my current configuration is:
#Html.EJS().RichTextEditor("table").ToolbarSettings(e => e.Items((object)ViewBag.tools)).Value((string)ViewBag.value).QuickToolbarSettings(e => { e.Table((object)ViewBag.table); }).InsertImageSettings(new RichTextEditorImageSettings() { Path = "/Uploads/", SaveUrl = "/Home/Save" }).ShowCharCount(true).MaxLength(2000).Created("created").Render()
Controller method return configuration in viewbag
var tools = new
{
tooltipText = "Custom Tools",
template = "<button class='e-tbar-btn e-btn' tabindex='-1' id='custom_tbar' style='width:100%'><div class='e-tbar-btn-text rtecustomtool' style='font-weight: 500;'> Custom Tools</div></button>"
};
ViewBag.tools = new object[] {
"Bold", "Italic", "Underline", "StrikeThrough",
"FontColor", "BackgroundColor",
"LowerCase", "UpperCase", "|",
"Formats", "Alignments", "OrderedList", "UnorderedList",
"Outdent", "Indent", "CreateTable","|",
"CreateLink", "Image", "|", "ClearFormat", "Print",
"SourceCode", "FullScreen", tools,"|", "Undo", "Redo"
};
ViewBag.table = new[] {
"tableRows","tableColumns","tableCellVerticalAlign","tableCellHorizontalAlign","backgroundcolor"
};
ViewBag.value="";
You can modify the existing “format” option using Format property as shown below.
[View]
#Html.EJS().RichTextEditor("default").Format((object)ViewBag.types).ActionBegin("onBegin").Render()
<script>
function onBegin(e) {
alert(e.element.textContent + " is Selected");
}
</script>
[Controller]
public ActionResult Index()
{
object format1 = new { text = "Paragraph", value = "P" };
object format2 = new { text = "My Paragraph", value = "BlockQuote" };
object format3 = new { text = "Heading 1", value = "H1" };
object format4 = new { text = "Heading 2", value = "H2" };
ViewBag.types = new { width = "40px",
types = new[] { format1, format2, format3, format4 }
};
return View();
}
If the newly added item has any predefined format, you can mention that format in value. Else if you want to perform custom action, then you can get the selected item through actionBegin event of RTE and perform required action there. Now the items will be displayed in toolbar as shown below
Sample
I am following few tutorials for creating MVC3 application.
I saw example of adding dropdownlist bound to a model.
#Html.DropDownListFor(model => model.Vehicle.Model, new List<SelectListItem>(), String.Empty, new { style = "width: 100px;", size = "1" })
But i just want to add few contant values i.e. not coming from the DB, but just want to add few items to dropdownlist in code, how can i do it?
#{
var modellist = new List<VehicleModels>()
{
new VehicleModels() { ID= 1, Name="A"},
new VehicleModels() { ID= 2, Name="B"},
new VehicleModels() { ID= 3, Name="C"},
new VehicleModels() { ID= 4, Name="D"}
}
}
#Html.DropDownListFor(model => model.Vehicle.Model, new SelectList(modellist, "ID" , "Name"), String.Empty, new { style = "width: 100px;", size = "1" })
I have this array property in my model and would like to see it in my view as a dropdown list. Here's the array property:
public string[] weekDays = new string[5] { "monday", "Tuesday", "Wednesday", "Thursday", "Friday" };
public string[] WeekDays
{
get { return weekDays; }
}
I've look for hours with no simple explanation or examples. Please help.
You can use DropDownList() html helper.
Html.DropDownList("weekDays",
Model.WeekDays.Select(s => new SelectListItem { Text = s }))
If you want to read the selected value you can use DropDownListFor() helper.
Html.DropDownListFor(model => model.SelectedWeekDay, //a property to assign the value
Model.WeekDays.Select(s => new SelectListItem { Text = s, Value = s }))
Here's how I solved it.
#{
var wekdys = new Enrollment();
#Html.DropDownList("weekDays", wekdys.WeekDays.Select(s => new SelectListItem { Text = s.ToString(), Value = s.ToString() }))
}
this allows me to have a DropDownList outside of the foreach loop
I have a property in controller like
public int NumberOfPersons
{
get { return _NumberOfPersons; }
set { _NumberOfPersons = value; }
}
private int _NumberOfNights = 7;
and I want to create a cascade dropdown with 7 numbers. Eg.
<select>
<option>1</option>
...
<option>7</option>
</select>
Can you please tell me how could I do?
Tanks
In your View you can write this:
#Html.DropDownListFor(m => m.NumberOfPersons,
Enumerable.Range(1,7).Select(i => new SelectListItem
{
Value = i.ToString(),
Text = i.ToString()
}))
Here is a quick example utilizing ViewData for this SelectList constructor
ViewData["mylist"] = new SelectList(new[] { "1", "2", "3", "4", "5", "6", "7" }.Select(x => new { value = x, text = x }), "value", "text", "1");
In your view:
#Html.DropDownList("myList")