Error saving fields in a form

0

I have a form made in MVC ASP.NET, using Entity framework for persistence. I try to save the data, but when I click save, Visual Studio throws me an exception in the line ctx.Empleado.Add(em); of the Create action. Then when I click continue it says "Error related to the network or specific to the instance while a connection was established with the SQL Server, the server was not found or the server was not accessible, check that the name of the instance is correct and that SQL Server is configured to support remote connections. (provider: SQL Network Interfaces, error: 26 - Error searching the specified server or instance) ". what's happening?

Controller:

using Ejercicio06.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Ejercicio06.Controllers
{
    public class EmpleadoController : Controller
    {
        // GET: Empleado/Index
        public ActionResult Index()
        {
            return View();
        }

        // GET: Empleado/Crear
        public ActionResult Crear()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Crear(FormCollection form)
        {
            Context ctx = new Context();
            Empleado em = new Empleado();

            em.nombre= form["nombre"];
            em.apellido = form["apellido"];
            ctx.Empleado.Add(em);
            ctx.SaveChanges();

            return View();
        }
    }
}

Model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Ejercicio06.Models
{
    public class Empleado
    {
        public int Id { get; set; }
        public string Nombre { get; set; }
        public string Apellido { get; set; }
    }
}

View:

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Crear</title>
    <link href="~/Content/bootstrap.min.css" rel="stylesheet" />
</head>
<body>

    <div class="container">
        <div class="panel-body">
            <h2 class="text-center">Registrarse</h2><br>

            <form action="Crear" method="POST">
                <!-- INPUT NOMBRE -->
                <div class="input-group">
                    <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
                    <input class="form-control input-lg" type="text" name="nombre" placeholder="Ingresar Mail" />
                </div>
                <br>
                <!-- INPUT APELLIDO -->
                <div class="input-group">
                    <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
                    <input class="form-control input-lg" type="text" name="apellido" placeholder="Ingresar Apellido" />
                </div>
                <br>

                <!-- BOTON GUARDAR -->
                <div class="text-center">
                    <button type="submit" class="btn btn-success">Guardar</button>

                    <!-- LINK SALIR -->
                    <a href="../" class="btn btn-default">Cancelar</a>
                </div>
            </form>

        </div>
    </div> <!-- container -->

</body>
</html>
    
asked by Andrés Oporto 30.05.2017 в 18:32
source

1 answer

0

first check your string connection in Web.conf

<add name="DBContext" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\DBname.mdf;Initial Catalog=DBname;Integrated Security=True" providerName="System.Data.SqlClient" />

always remember that the name of the chain you have to define in your context since you use entity framework

public class AppContext : DbContext
{

    public AppContext() : base("DBContext")
    {
    }

    public DbSet<Empleado> Empleado { get; set; }


    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    }
}

If you are new to asp.net mvc and want to see how the forms work, it is fine but I do not recommend you use the asp scaffolds. mvc that will make your life more easy. I'll explain in an example.

Create a project and add your employee class to your models folder

 public class Empleado
{
    public int Id { get; set; }
    public string Nombre { get; set; }
    public string Apellido { get; set; }
}

You compile the application or give it rebuild. Then in your folder folder, right click add controller and select the option MVC 5 Controller with views, using Entity Framework. In controller name "EmployeeController", in model class you select your model class, in this case used. And since you do not have a context still defined in the Data context class box, click on the "new data context" button and you will generate an automatic context, change the name if you want but always leave the surname Context. In this way you have already done a CRUD with Entity Framework in a very simple way. Run your browser and you'll see

    
answered by 30.05.2017 в 19:04