If-else statement Issue in MVC - asp.net-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>

Related

add new value from text box in select and option in html

I have some dropdown values in select type in an html page. I want to add some extra values to this from users, through a text box. When user enters the value and press the button the value should be added to the dropdown select box.
<table>
<tr>
<td> Blood Test: </td>
<td>
<select name="DragCompany">
<option value="ACI Ltd.">ACI</option>
<option value="Apollo Pharmaceutical Laboratories Ltd.">Appolo</option>
<option value="Beximco Pharmaceuticals Ltd">Beximco</option>
<option value="Beacon Pharmaceuticals Limit">Beacon</option>
<option value="Delta Pharma Limited">Delta</option>
<option value="Globe Pharmaceuticals Ltd">Globe</option>
</select>
</td>
</tr>
</table>
Try this:
var s = document.getElementById('DragCompany');
var inputContainer = document.getElementById('newinput');
function addtoselect() {
if (inputContainer.value) {
s.options[s.options.length] = new Option(inputContainer.value);
inputContainer.value = '';
}
}
<table>
<tr>
<td> Blood Test: </td>
<td>
<select id="DragCompany" name="DragCompany">
<option value="ACI Ltd.">ACI</option>
<option value="Apollo Pharmaceutical Laboratories Ltd.">Appolo</option>
<option value="Beximco Pharmaceuticals Ltd">Beximco</option>
<option value="Beacon Pharmaceuticals Limit">Beacon</option>
<option value="Delta Pharma Limited">Delta</option>
<option value="Globe Pharmaceuticals Ltd">Globe</option>
</select>
</td>
</tr>
</table>
<input type="text" id="newinput">
<button onclick="addtoselect()">Add</button>

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

Ajax.BeginForm alternatives to onchange=this.form.submit()

I have a table with a Ajax.BeginForm on each row. This works good to pass the data to the controller.
However, it wont fire the Request.IsAjaxRequest() on the controller because of the form post(thanx HTX9 for pointing that out!). The real problem is that it returns a partial view instead of updating it in the current view.
Below you can see two fieldsets. The first is the one i want to update and the one below it is the one which sends data to the controller with the Ajax.BeginForm.
With other words: When I choose something in a dropdownlist it updates the table above but it opens it in a new partial view.
I have the unobtrusive ajax js in the _layout view and a similar setup works in an other part of the application from with a post data with a button instead.
<fieldset>
<legend>Direktbyten</legend>
<div id="container-grid2">
#Html.Partial("_RatingList2", Model.Models2)
</div>
</fieldset>
<fieldset>
<legend>Omarkerade byten(#ViewBag.DirectCount)</legend>
<div id="RatingList">
<table>
<thead>
<tr>
<td>Se hela</td>
<td>Hyra</td>
<td>Rum</td>
<td>Kvm</td>
<td>Gata</td>
<td>Område</td>
<td>Deras prio</td>
<td>Totaltvärde</td>
<td>Min prio</td>
</tr>
</thead>
<tbody>
#foreach (var item in Model.Models1)
{
using (Ajax.BeginForm("UpdateRatingListRow", new AjaxOptions { UpdateTargetId = "container-grid2" }))
{
#Html.Hidden("RatingListId", item.RatingListId)
<tr>
<td>#Html.ActionLink("Till annons", "ViewAd", "Ad", new { id = item.CustomerId }, null)
</td>
<td>#item.Rent</td>
<td>#item.Rooms</td>
<td>#item.SquareMeters</td>
<td>#item.Street</td>
<td>#item.Area</td>
<td>#item.TheirRating</td>
<td>#item.TotalRating</td>
<td>
#Html.Raw("<div id='" + item.RatingListId + "'>")
<select name="SelectedValue" onchange="this.form.submit();">
<option value="0"#if (item.MyRating == "00")
{
#Html.Raw("selected")
}>Ny</option>
<option value="10" #if (item.MyRating == "10")
{
#Html.Raw("selected")
}>Inget intresse</option>
<option value="50"#if (item.MyRating == "50")
{
#Html.Raw("selected")
}>Svagt intressse</option>
<option value="60"#if (item.MyRating == "60")
{
#Html.Raw("selected")
}>Litet intresse-vill ej ha kontakt</option>
<option value="70"#if (item.MyRating == "70")
{
#Html.Raw("selected")
}>Intresserad-Vill ha kontakt</option>
<option value="80"#if (item.MyRating == "80")
{
#Html.Raw("selected")
}>Varit på visning-Vill gå vidare</option>
<option value="90"#if (item.MyRating == "90")
{
#Html.Raw("selected")
}>Intresserad-Vill ha kontakt</option>
<option value="100"#if (item.MyRating == "100")
{
#Html.Raw("selected")
}>Avvaktar värdar</option>
</select>#item.MyRating
#Html.Raw("</div>")
</td>
</tr>
}
}
</tbody>
</table>
</div>
</fieldset>
Here is the Controller:
[HttpPost]
public ActionResult UpdateRatingListRow()
{
if (Request.IsAjaxRequest())
{
//Do stuff, it does not hit this part because of the isAjaxRequest().
return this.PartialView("_RatingList2", myRatingListMarked);
}
return this.PartialView("_RatingList2", null);
}
Generated Html:
Here is my "fulhack"(ugly hack in swedish), added a button with each row id and then i call it for submit. Still hoping for a clean solution without having to rewrite a lot of code.
using (Ajax.BeginForm("UpdateRatingListRow", new AjaxOptions { UpdateTargetId = "container-grid2", OnSuccess = "OnSuccess('" + item.RatingListId + "')" }))
{
<input type="submit" id="#item.RatingListId" style="position: absolute; top: -1000px">
#Html.Hidden("RatingListId", item.RatingListId)
<tr>
<td>#Html.ActionLink("Till annons", "ViewAd", "Ad", new { id = item.CustomerId }, null)</td>
<td>#item.Rent</td>
<td>#item.Rooms</td>
<td>#item.SquareMeters</td>
<td>#item.Street</td>
<td>#item.Area</td>
<td>#item.TheirRating</td>
<td>#item.TotalRating</td>
<td>#Html.Raw("<div id='" + item.RatingListId + "'>")
<select name="SelectedValue" onchange="document.getElementById(#item.RatingListId).click();">
<option value="0"#if (item.MyRating == "00")

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