How to add dummy values to a dropdownlist in MVC3 - asp.net-mvc-3

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" })

Related

Kendo().ComboBox() in a template - how to set SelectedIndex

Have a grid-template with a kendo combobox in:
<script id="templateSample" type="text/kendo-tmpl">
#*Active holds the selected value*#
# alert(Active) # //
#(Html.Kendo().ComboBox()
.Name("ComboBoxSample")
.BindTo(new List<SelectListItem>()
{
new SelectListItem()
{
Value = "true",
Text = "Yes",
Selected = false,
},
new SelectListItem()
{
Value = "false",
Text = "No",
Selected = false,
}
})
.DataTextField("Text")
.DataValueField("Value")
.SelectedIndex(1)
.ToClientTemplate()
)
</script>
The template appears whenever a row is expanded (see http://demos.telerik.com/aspnet-mvc/grid/hierarchy).
I can't figure out how to set selected index with the item currently selected.
To set selected item, I can chose to use SelectListItem.Selected or Combobox.SelectedItem, but how to set a value from the item currently showing in the template ??
Thanks.
I have found a way to solve this problem using javascript.
Add an event to the grid:
.Events(e=>e.DetailInit("aftertemplateload"))
And the script:
function aftertemplateload(e) {
$("#ComboBoxSample" + e.data.Id).data("kendoComboBox").value(e.data.Active);
}
and remember to add the new id to the combobox:
#(Html.Kendo().ComboBox()
.Name("ComboBoxSample#=Id#")
.BindTo(new List<SelectListItem>()
{ etc...

Html.RenderAction not working with jQueryDataTable

Html Razor code
this code use in Edit view
#{Html.RenderAction("ActionNotes", "Prospects");}
Ajax code
write ajax code in ActionNotes view.
var $dt = #jQueryDataTable.Init("#tblActionNotes"
, new DataTableOption(Model.List.Items)
{
AjaxSource = Url.Action("ActionNotes"),
Dom = #IsOperator ? (((Model.UserRights & (int)CrmPermission.Add) == (int)CrmPermission.Add|| Model.TotalAccess) ? null : "<\'row-fluid\'<\'span6\'F><\'span6\'>><\'row-fluid\'r>t<\'row-fluid\'<\'span3\'i><\'span3\'l><\'span6\'p>>" ) : (((Model.RoleRights & (int)CrmPermission.Add) == (int)CrmPermission.Add) ? null : "<\'row-fluid\'<\'span6\'F><\'span6\'>><\'row-fluid\'r>t<\'row-fluid\'<\'span3\'i><\'span3\'l><\'span6\'p>>" ),
DeferLoading = Model.List.TotalItems,
CreateUrl = Url.Action("ActionNoteNew","Prospects", new { id = Model.ProspectId }),
Columns = new[] {
new DataTableColumnOption { Data="CreatedOn", Title="Date", Width="15%" },
new DataTableColumnOption { Data="CreatedByUserName", Title="Operator", Width="15%" },
new DataTableColumnOption { Data="ToOperatorName", Title="Assign Operator", Width = "15%"},
new DataTableColumnOption { Data="Note", Title="Note" },
new DataTableColumnOption { Data="FollowUpDate", Title="Followup Date", Width="15%" },
new DataTableColumnOption { Data="Type", Title="Type", Width="10%" },
new DataTableColumnOption { Data="Status", Title="Status", Width="10%" },
new DataTableColumnOption { Data="Id",Title = "Action", Width="80px", Render = "actionNoteActionRender", Sortable = false }
},
})
});
i use this code for call another controller and view for display two different view in single view, using this code second view is display properly and i can see data on edit view in (right click) page source but i can not display data on edit view page.
solve this issue using..
#{Layout = null;}
in Edit view before..
#{Html.RenderAction("ActionNotes", "Prospects");}

MultiSelectList does not highlight previously selected items

In a ASP.NET MVC (Razor) project, I'm using a ListBox with Multi Select option in a Edit View,
CONTROLLER
public ActionResult Edit(int id)
{
Post post = db.Posts.Find(id);
string selectedValues = post.Tags; //This contains Selected values list (Eg: "AA,BB")
ViewBag.Tagslist = GetTags(selectedValues.Split(','));
return View(post);
}
private MultiSelectList GetTags(string[] selectedValues)
{
var tagsQuery = from d in db.Tags
orderby d.Name
select d;
return new MultiSelectList(tagsQuery, "Name", "Name", selectedValues);
}
HTML
<div class="editor-field">
#Html.ListBox("Tags", ViewBag.Tagslist as MultiSelectList)
</div>
This loads the items (Tag List) in to ListBox, but does not highlight the items in the Selected Values list.
How to fix this issue?
Thanks in advance.
I suspect that your Post class (to which your view is strongly typed) has a property called Tags. You also use Tags as first argument of the ListBox helper. This means that the helper will look into this property first and ignore the selected values you passed to the MultiSelectList. So the correct way to set a selected value is the following:
public ActionResult Edit(int id)
{
Post post = db.Posts.Find(id);
ViewBag.Tagslist = GetTags();
return View(post);
}
private MultiSelectList GetTags()
{
var tagsQuery = from d in db.Tags
orderby d.Name
select d;
return new MultiSelectList(tagsQuery, "Name", "Name");
}
and in the view:
<div class="editor-field">
#Html.ListBoxFor(x => x.Tags, ViewBag.Tagslist as MultiSelectList)
</div>
And here's a full example that should illustrate:
public class HomeController : Controller
{
public ActionResult Index()
{
var post = new Post
{
Tags = new[] { "AA", "BB" }
};
var allTags = new[]
{
new { Name = "AA" }, new { Name = "BB" }, new { Name = "CC" },
};
ViewBag.Tagslist = new MultiSelectList(allTags, "Name", "Name");
return View(post);
}
}
Also I would recommend you using view models instead of passing your domain entities to the view. So in your PostViewModel you will have a property called AllTags of type MultiSelectList. This way you will be able to get rid of the weak typed ViewBag.

How can I display an array model property as a list in my view?

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

create a cascade dropdown in ASP.NET MVC 3 with C# for a given value

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")

Resources