If the user does not find the desired item in the dropdown
list, it can include a new item, for this I used the prompt
function of javascript
, and then the user types the name and clicking Ok
, if the object is not actually registered in the database (Sometimes for lack of attention, and we do not find the item in the list), the object is registered. After the registration the object is added to the list, however it goes to the end of it.
So I wanted the object to be sorted again after the .append()
, leaving it in the position that would be ideal (in alphabetical order).
For this I thought about creating a script that would look in the bank all the items (again) and be inserted again, but I think it is a waste of code. This is the% used%
$('#NewBrand').click(function () {
var name;
name = prompt("Qual a marca?");
var url = '@Url.Action("CreateBrand", "Ajax")';
if (name != null) {
$.ajax({
type: 'POST',
url: url,
data: { name: name },
dataType: 'json',
success: function (data) {
if (data.Name == name) {
alert("A mcarca " + name + " foi cadastrado com sucesso!");
$('#Brand').append('<option value="' + data.BrandID + '">' + data.Name + '</option>');
} else if (data == false) {
alert("Não foi possível cadastrar a marca " + name + "!");
} else {
alert("A marca " + data.Name + " já está cadastrada");
}
}
});
}//if
});
and here the script
of Dropdownlist
:
<div class="form-group">
@Html.Label("Tipo", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("EquipmentTypelID", null, htmlAttributes: new { @class = "form-control",id="Type", style = "float:left;" })<div class="btn btn-primary" id="NewType" title="Novo Tipo" style="float:left;">+</div>
@Html.ValidationMessageFor(model => model.EquipmentTypeID, "", new { @class = "text-danger" })
</div>
</div>
Is there any way to sort directly by View
?