ASP.Net MVC3 Viewbag - set to selected index of dropdownbox - asp.net-mvc

I'm totally new to MVC. I would like to create a Viewbag to contain the selected index of the control. Can I set that within my .ascx file? or what would be the best way to capture this information?
<select id="accounttype" style="float: left;" autocomplete="off">
<%
if (Request.Url.AbsolutePath.ToUpper().StartsWith("/COMMERCIAL")) //Commercial
{
%>
<option value="C" selected="selected">eManager+</option>
<option value="C">Retirement Plans</option>
<option value="C">Brokerage Accounts</option>
<%
}
else if (Request.Url.AbsolutePath.ToUpper().StartsWith("/BUSINESS")) //Business
{
%>
<option value="B" selected="selected">eManager+</option>
<option value="B">Business Credit Card</option>
<option value="B">Retirement Plans</option>
<option value="B">Brokerage Accounts</option>
<%
}
else //Personal, root or other
{
%>
<option value="P" selected="selected">Online Banking</option>
<option value="P">Health Savings Account</option>
<option value="P">Paychek Plus!®</option>
<option value="P">Gift Cards</option>
<option value="P">Business Tax Manager</option>
<option value="P">Business Card Manager</option>
<option value="P">Business Credit Card</option>
<%
}
%>
</select>

You're doing things the hard way. In your controller do this:
if (Request.Url.AbsolutePath.ToUpper().StartsWith("/COMMERCIAL")) //Commercial
{
ViewBag.ListContents = new SelectList(new[] {new {name = "eManager+", value="C"},
{name = "Retirement Plans", value="C"},
{name = "Brokerage Accounts", value="C"}}, "value", "name");
}
// similar for your other if statements as well
ViewBag.DropDownID = selectedvalue;
return View();
then in your view
<% Html.DropDownListFor(m => m.DropDownID, ViewBag.ListContents); %>
The problem, however is that since you have multiple entries with the same value, you can only select the first one in code. You would be better to give them each unique values then on post determine which values are for each category.
For example, set that values to "C1" "C2" "C3" and then you can check if the string starts with C rather than equals C

I assume you have a form in there. Set the ViewBag in the controller's action code after you post the form.

Related

How to set an id to option in HTML.DropDownList

I have an HTML.DropDownList where I can set datavaluefield and datatextfield like the following,
<select>
<option value="Fruit">APPLE</option>
</select>
Even, I can able to set the ID to dropdown list using html attributes but Since I need to set the 'Id' to the option like following,
<select>
<option ID='1' value="Fruit">APPLE</option>
</select>
Can anyone share the idea on it...
You can use the id attribute to set the ID to the option tag and use this.value into the onchange callback with select element.
Below is the working example
document.getElementById("select1").onchange = function() {
if (this.value == 'Fruit') {
var optionID=document.getElementById('1');
alert(optionID.value);
}
if (this.value == 'Flower') {
var optionID=document.getElementById('2');
alert(optionID.value);
}
}
<select name="select01" id="select1" onchange="handleSelect()">
<option value="Fruit" id="1">Apple</option>
<option value="Flower" id="2">Rose</option>
</select>
Try this, It will alert your selected value
<select onchange="alert(this.value)">
<option id="1">APPLE</option>
<option id="2">Orange</option>
</select>

Cascade drop-down in MVC View without ajax?

I created one View Model with two Entities. I am passing this view model to my MVC Razor view which have two html drop-downs for each entity respectively.
<select class="form-control" id="Employees" name="Employees">
#foreach (var employee in Model.Employees)
{
<option value="#employee.Id"> #employee.name </option>
}
</select>
<select class="form-control" id="Tasks" name="Tasks">
#foreach (var task in Model.Tasks)
{
<option value="#task.Id"> #task.name </option>
}
</select>
Employee table is the parent of Task table. What I want is getting all the tasks which are related to particular employee only. e.g. In Employee drop-down I select John, then in Tasks drop-down I should get all the tasks which are relative to John. I know how to do this with ajax. I am looking for some other solution.
Is it possible to do something like this:
#foreach (var task in Model.Tasks.Where(x=>x.employeeId == 'Selected in previous dropdown'))
{
<option value="#task.Id"> #task.name </option>
}
Html block
<select id="Employees">
<option value="">Select Employee</option>
<option value="1">Employee1</option>
<option value="2">Employee2</option>
</select>
<select id="Tasks">
<option value="">Select Task</option>
<option value="1" data-employee="1">Employee1Task1</option>
<option value="2" data-employee="1">Employee1Task2</option>
<option value="3" data-employee="1">Employee1Task3</option>
<option value="1" data-employee="2">Employee2Task1</option>
<option value="2" data-employee="2">Employee2Task2</option>
<option value="3" data-employee="2">Employee2Task3</option>
</select>
Script scetion
<script>
$(document).ready(function () {
//on page ready hide all task option
$("#Tasks").find('option').hide();
// set task as empty
$("#Tasks").val('');
// onchange of employee Drop down
$("#Employees").on('change', function () {
var selectedEmployee = $("#Employees").val();
if (selectedEmployee != '') {
$("#Tasks").find('option').hide();
$("#Tasks option[value='']").show();
$('*[data-employee="' + selectedEmployee + '"]').show();
}
else {
// if employee not selected then hide all tasks
$("#Tasks").find('option').hide();
$("#Tasks").val('');
}
});
});
</script>
Please populate country and state drop down list using MVC way by for each loop and use above script. The mandatory case is you have to render all cascade options
<select class="form-control" id="Employees" name="Employees">
#foreach (var employee in Model.Employees)
{
<option value="#employee.Id"> #employee.name </option>
}
</select>
<select class="form-control" id="Tasks" name="Tasks">
#foreach (var task in Model.Tasks)
{
<option value="#task.Id" data-employee="#task.EmployeeId"> #task.name </option>
}
</select>

changes to &nbsp in asp.net mvc html.dropdownlist

I want to show items with indention in html.dropdownlist
this is code in controller
IEnumerable<ViewModels.testvm> test = db.Database.SqlQuery<ViewModels.testvm>(#"WITH tree (id, parentid, level, title, rn) as
(
some code
)
***********SELECT id,REPLICATE(' ',level) + title as title
FROM tree
order by RN");
ViewBag.ParentID = new SelectList(test, "ID", "Title");
as you can see I add &nbsp to dropdown items in replecate
but the problem is &nbsp shows in dropdown.
this is view > source result:
<select class="form-control" id="ParentID" name="ParentID"><option
value="10">Menu1</option>
<option value="11">&nbsp;Sub1</option>
<option value="14">&nbsp;Submenu1</option>
<option value="12">Menu2</option>
<option value="16">&nbsp;sub2</option>
<option value="13">Menu3</option>
<option value="15">&nbsp;sub3</option>
<option value="17">&nbsp;&nbsp;sub sub</option>
<option value="22">&nbsp;&nbsp;&nbsp;sub3 sub sub</option>
<option value="19">menu4</option>
<option value="20">&nbsp;sub4</option>
<option value="21">menu5</option>
</select>
as you can see converted to &nbsp;
I tried "\xA0" instead of &nbsp but it shows in dropdown too!
I think html.raw can solve this, but I don't know how can I use it with html.dropdownlist.
this is view
<div class="col-md-10">
#Html.DropDownList( "ParentID", null, htmlAttributes: new { #class = "form-
control" })
</div>
any Idea?
Edit
by changing view like this the problem solved:
I tried this after checked a post as answer
<select class="form-control" id="ParentID" name="ParentID">
<option value="0">First Level</option>
#foreach (var item in ViewBag.ParentID)
{
<option value="#item.Value">#Html.Raw(#item.Text)</option>
}
</select>
You can manually create the dropdown list assuming you place your collection in a ViewModel member called ParentIDs:
The View:
#if (Model.ParentIDs.Any())
{
<select class="form-control" id="ParentID" name="ParentID">
#foreach (var item in Model.ParentIDs)
{
if (Model.ParentID == item.id)
{
<option value="#item.id" selected>#item.title</option>
}
else
{
<option value="#item.id">#item.title</option>
}
}
</select>
}

If-else statement Issue in MVC

In my below mentioned MVC Code there is some MVC Syntax issue. Can please someone assist to fix code issue
If-else block.
<select id="VIPGuests" name="VIPGuests" style="width:200px" abindex="0"><option value="">Select VIP</option>
<% foreach (var authorizedGuest in Model.XYZ)
{ %>
<%if(Model.VIPGuests == authorizedGuest.Key )%>
{
<option selected="selected" value=<% = authorizedGuest.Key%>> <% = authorizedGuest.Value%> </option>
}
else
{
<option value=<% = authorizedGuest.Key%>> <% = authorizedGuest.Value%> </option>
}
<%} %>
</select>
I am getting issue as
Text is not allowed between opening and closing tags of Element
'Select'
Try this. All server side code put with in <% %>.
<select id="VIPGuests" name="VIPGuests" style="width:200px" tabindex="0"><option value="">Select VIP</option>
<% foreach (var authorizedGuest in Model.XYZ)
{
if(Model.VIPGuests == authorizedGuest.Key )
{%>
<option selected="selected" value=<% = authorizedGuest.Key%>> <% = authorizedGuest.Value%> </option>
<% }
else
{ %>
<option value=<% = authorizedGuest.Key%>> <% = authorizedGuest.Value%> </option>
<% }
} %>
</select>
Couldn't test it but something inline with this should work
<select id="VIPGuests" name="VIPGuests" style="width:200px" tabindex="0"><option value="">Select VIP</option>
#{
foreach (var authorizedGuest in Model.XYZ)
if(Model.VIPGuests == authorizedGuest.Key )
#:<option selected="selected" value=“#authorizedGuest.Key”> #authorizedGuest.Value </option>
else
#:<option value=“#authorizedGuest.Key”>#authorizedGuest.Value </option>
}
</select>

Active Select option in Razor

I am working on a mobile version of my website. I have the option Action working however when I select, for example "About" it will take me to the correct page but the navigation bar goes back to the "Home" option. How do I go about doing this? thanks in advance
<select class="navbar-nav" style="width:250px" onchange='location.href = this.value'>
<option value="#Url.Action("Index", "Home")">Home</option>
<option value="#Url.Action("About", "Home")">About</option>
<option value="#Url.Action("Products", "Home")">Products</option>
<option value="#Url.Action("Services", "Home")">Services</option>
<option value="#Url.Action("Contact", "Home")">Contact</option>
</select>
<option selected="#ViewBag.Home" value="#Url.Action("Index", "Home")">Home</option>
<option selected="#ViewBag.About" value="#Url.Action("About", "Home")">About</option>
<option selected="#ViewBag.Products" value="#Url.Action("Products", "Home")">Products</option>
<option selected="#ViewBag.Services" value="#Url.Action("Services", "Home")">Services</option>
<option selected="#ViewBag.Contact" value="#Url.Action("Contact", "Home")">Contact</option>
and in your Home.cshtml
#{
ViewBag.Home = true;
}
and the other pages.
You can do it like this :
#{
Dictionary<string, string> menu = new Dictionary<string, string>();
menu.Add("Home", #Url.Action("Index", "Home"));
menu.Add("About", #Url.Action("About", "Home"));
menu.Add("Products", #Url.Action("Products", "Home"));
menu.Add("Services", #Url.Action("Services", "Home"));
menu.Add("Contact", #Url.Action("Contact", "Home"));
}
<select class="navbar-nav" style="width:250px" onchange='location.href = this.value'>
#foreach (var m in menu)
{
<option value="#m.Value" #(m.Value == Request.Url.AbsoluteUri ? "selected='selected' " : "")>#m.Key</option>
}
</select>

Resources