Send to a View the list of Customers in my database

1

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?

    
asked by Edu 01.11.2018 в 04:53
source

2 answers

1

My recommendations

  • TOPIC 1: That the model of Create is exactly the model that you are creating : That is, this case Client and not a list of Customers

  • TOPIC 2: Validate a "Text" based on a business logic with Remote Validation : Idem what they say to you with ajax, that's what they use but some help comes in ASP.NET MVC for these validations on the server

We're going by part

TOPIC 1: That the Create model is exactly the model that you are    creating

Here as you already have them armed it is a view to create with Editor directly with the values of the model that in your case it should be

   @model TiendaJuegos.Models.Cliente

That is to say you would have in the part of Social Reason

        <div class="form-group">
        @Html.LabelFor(model => model.RazonSocial, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.RazonSocial, new { htmlAttributes = new { @class = "form-control", @id = "txtRazonSocial" } })
            @Html.ValidationMessageFor(model => model.RazonSocial, "", new { @class = "text-danger" })
        </div>
    </div>

And so with the rest of the properties of a Client class (Game Store.Models.Client) that are those that are requested when creating

We go to the part you request that is VALIDATE!

While there are validations that can be done in the client, for example if required, some format. Not all the validations are convenient to do them in the client directly speaking. Imagine in a proposed solution that you have 10 million disabled clients. It is practically not performant to say it elegantly to obtain at the moment of creating those clients and to take them to the sight. Just to verify it. We would have an extensive HTML or js, and it would be very slow to transmit said HTML.

What would be a way to validate closer to the data?

That is, how could we validate that I do not enter a value that is already in the DB? Well take the validation as close to the DB. In other words, go to the server with the data written in the Social Reason input html. You could even there

  • Validate that the entry does not correspond to a Social Reason text of a disabled client
  • Validate that the text entry does not correspond to an already entered client

For that we can go to the DB in the way that you already have models / providers of EntityFramework with LINQ, stored procedures, direct with commands to the DB.

Well suppose you have a method in a controller that is

TOPIC 2: Validate a "Text" based on a business logic with Remote Validation

Here is to make an ajax request to a method in the controller, api, etc. We are going to use the validation attribute remote validation

You could have a method in the Client driver called ValidarRazonSocial that goes to the business and then to the data layer (here put in the code necessary to validate in the DB that the social name we send is valid)

public class ClienteController : Controller  {

public JsonResult ValidarRazonSocial(string razonSocial)
{
    var esValida = _clienteNegocio.ValidarRazonSocial(razonSocial);

    return Json(esValida, JsonRequestBehavior.AllowGet)
}

}

We will have to decorate the client object to validate

[Remote("ValidarRazonSocial","Cliente", ErrorMessage="Ya esta utilizado el campo {0}")]
public string RazonSocial { get; set; }

Here is the configuration of the code, you should also see in the appSettings that you have these settings true.

<appSettings>  
   <add key="ClientValidationEnabled" value="true" />  
   <add key="UnobtrusiveJavaScriptEnabled" value="true" />  
</appSettings>  

and that you have the javascript libraries

  • jquery
  • jquery.validate
  • jquery.validate.unobtrusive

Links that can help you

I hope it will help or guide you

    
answered by 08.11.2018 в 15:26
0

hi edu I think you should do all that from the controller, and as @Gonzalo Pigni says you should have some data for filtral or paging not to send, for example, 1200 clients at the same time.

public ActionResult Index(string sortOrder, string currentFilter, string 
 searchString, int? page)
{
 ViewBag.CurrentSort = sortOrder;
 ViewBag.RazonSocialSortParm = String.IsNullOrEmpty(sortOrder) ? "RazonSocial_desc" : "";
 ViewBag.DireccionSortParm = sortOrder == "Direccion" ? "Direccion_desc" : "Direccion";

if (searchString != null)
{
   page = 1;
}
else
{
   searchString = currentFilter;
}

 ViewBag.CurrentFilter = searchString;

 var clientes = from c in context.Clientes
              select c;
 switch (sortOrder)
 {
   case "RazonSocial_desc":
     clientes = clientes.OrderByDescending(s => s.RazonSocial);
     break;
   case "Direccion":
     clientes = clientes.OrderBy(s => s.Direccion);
     break;
   case "Direccion_desc":
     clientes = clientes.OrderByDescending(s => s.Direccion);
     break;
  // case "RUT": //*** asi sucesivamente con otros campos
  //  clientes = clientes.OrderBy(s => s.RUT);
  //  break;
}

int pageSize = 3;
int pageNumber = (page ?? 1);
return View(clientes.ToPagedList(pageNumber, pageSize));
}

I think a solution would be something like that, analyze it and comment if that is what can help you

    
answered by 08.11.2018 в 15:12