I tell you that I'm doing a database where I register clients. And for the disabling of a client I use logical deletion, setting the property of Enabled = false to the client.
Then, it occurred to me, that when I loaded my Create.html view, I passed all the clients that I have in the database to that view, so that at the moment I was writing the name of a client I could verify if it is a disabled client and can autocomplete the text boxes to automate the process and be able to tell the customer "Here is registered as before the service was already used, it appears x address and phone, are they correct? Do you want to change them? "
To send my list of clients to the Create view, I do it like this:
// GET: Clientes/Create
public ActionResult Create()
{
// enviamos la lista de clientes para poder verificar si se trata de una renovación de cliente antiguo
return View(context.Clientes.ToList());
}
And in the view then I have:
@model IEnumerable<TiendaJuegos.Models.Cliente>
@{
ViewBag.Title = "Registro";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h3>Datos del nuevo cliente:</h3>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.FirstOrDefault().RazonSocial, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstOrDefault().RazonSocial, new { htmlAttributes = new { @class = "form-control", @id = "txtRazonSocial" } })
@Html.ValidationMessageFor(model => model.FirstOrDefault().RazonSocial, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.FirstOrDefault().Direccion, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstOrDefault().Direccion, new { htmlAttributes = new { @class = "form-control", @id = "txtDireccion" } })
@Html.ValidationMessageFor(model => model.FirstOrDefault().Direccion, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.FirstOrDefault().Telefono, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstOrDefault().Telefono, new { htmlAttributes = new { @class = "form-control", @id = "txtTelefono" } })
@Html.ValidationMessageFor(model => model.FirstOrDefault().Telefono, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.FirstOrDefault().RUT, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstOrDefault().RUT, new { htmlAttributes = new { @class = "form-control", @id = "txtRut" } })
@Html.ValidationMessageFor(model => model.FirstOrDefault().RUT, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Registrar" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Volver a la lista", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
<script type="text/javascript">
var x = $('#txtRazonSocial');
x.blur(function () {
// acá va el codigo para verificar que lo escrito en el textbox txtRazonSocial se trata de un cliente deshabilitado y se autocompletan los demás textbox con los datos de ese cliente
});
</script>
The @model is supposed to be fine, because I'm saying it's a Client list, and that's what I'm sending you to view.
But then you will see that I had to resort to putting in the editors "model = > model.FirstOrDefault ().", because it was wrong to simply put "model." since model is a list not a single client.
However, although this way with FirstOrDefault does not give an error, the view is also loaded with the textbox completed by the first Client of my model list.
Is that I need to tell EditFor or Validation what is 1 client, then I use FirstOrDefault, but then you see that that makes that First customer write in the textbox, and I do not want this last.
In addition to this, the most important thing is that I would not know how to use JavaScript to verify that it is a disabled client to autocomplete textboxes.
Any help?