3
In case the user does not find the desired item in the list of dropdown
, it can include a new item, for this I used the function prompt
of javascript
, and then the user enters the name and click on Ok
, If the object is not actually registered in the database(Sometimes due to lack of attention, and we do not find the item in the list), the object is registered. After registration the object is added to the list, but it goes to the end of it.
So I wanted after the .append()
, an ordering of the object again, leaving it in the position that would be ideal (in alphabetical order).
For this I thought of creating a script that would search the bank for all items (again)and be inserted again, but I think it is a waste of code.
This is the script
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 Dropdownlist
of View
:
<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
?
Why duplicate? I had already seen this question, but did not understand anything.
– Fabio Souza
It is a possible duplicate, because the answers generated here will be similar to the one in the link question. However, this is just my opinion.
– Randrade