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

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

Related

Drop down item not being selected by default in Razor view

I have the following code written in Razor view in MVC in C#. I want to select one of the item based on value but it is not working. I confirmed that the variable Facilities contain value Un-Funded but it doesn't select this item.
<select id="facilities" class="form-control" style="width:200px;">
<option #{ if (Facilities == "Funded") { Response.Write(" selected "); } }>Funded</option>
<option #{ if (Facilities == "Un-Funded") { Response.Write(" selected "); } }>Un-Funded</option>
</select>
Here's screenshot of how HTML looks like. Notice it is not printing selected for any of the option.
I am using Response.Write which doesn't work in Razor view. Using #Html.Raw("some string") fixed the issue. So the code will look like this:
#{ if(Facilities == "Funded") { #Html.Raw("selected='selected'") } }
You can try to use js,so that if you have many options,you don't need to check on each option:
<select id="facilities" class="form-control" style="width:200px;">
<option value="Funded">Funded</option>
<option value="Un-Funded">Un-Funded</option>
</select>
<script>
$(function () {
$("#facilities").val("#Facilities");
//Or you can use $("#facilities").val(#Html.Raw(Json.Encode(Facilities)));
})
</script>
With MVC 5 you can use this method #: to insert your selected:
<select id="facilities" class="form-control" style="width:200px;">
<option value="Funded" #{ if (Facilities == "Funded") { #: selected="selected"
}}>
Funded
</option>
<option value="Un-Funded" #{ if (Facilities == "Un-Funded") { #: selected="selected"
}}>
Un-Funded
</option>
</select>
for older version you can check this link :
setting-the-selected-option-in-mvc3

How to return select option value?

This is my Html
<div class="form-row">
<div class="col-md-3">
<div class="position-relative form-group">
<label class="">Status</label>
<select class="form-control" id="txtStatus" name="Status">
<option value="A">Active</option>
<option value="I">Inactive</option>
</select>
</div>
</div>
</div>
how to do something like this is asp.net mvc?
<option value="A" <%if Status="A" then%>Selected
<%end if%>>Active</option>
<option value="I" <%if Status="I" then%>Selected
<%end if%>>Inactive</option>
#Model.Status is my value.
How to Do like this :
<option value="A" <%if #Model.Status="A" then%>Selected
<%end if%>>Active</option>
The proper way to do is to use the #Html.DropDownListFor helper method. In the Model we need to introduce a property for available statuses that can be selected.
public class ViewModel
{
public string Status { get;set;}
public IEnumerable<SelectListItem> Statuses { get;set;}
public ViewModel()
{
Statuses= new List<SelectListItem>
{
new SelectListItem {Text = "-- Select Status --", Value = ""},
new SelectListItem {Text = "Active", Value = "A"},
new SelectListItem {Text = "InActive", Value = "I"}
};
}
}
and in view then we can write :
#Html.DropDownListFor(x => x.Status, Model.Statuses,
new { #class ="form-control" , id ="txtStatus"})
It will generate html something similar to :
<select class="form-control" id="txtStatus" name="Status">
<option value="A">Active</option>
<option value="I">Inactive</option>
</select>
UPDATE:
You can do with plain html like:
<option value="A" #(Model.Status =="A" ? "selected" : "")>Active</option>
<option value="I" #(Model.Status =="I" ? "selected" : "")>Inactive</option>

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>

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>

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

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.

Resources