Using Ajax.BeginForm MVC 4 aspx c #

2

I'm a novice with MVC 4 My question is; How can I send all the information that the user captured in HTML to a function of my controller ?. That this function inserts my data sql server with Ajax.BeginForm there are not many examples since most use Razor

Code C # Controller

   using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Configuration;
using System.Data.SqlClient;
using RequerimientosABCusuarios.Models;

namespace RequerimientosABCusuarios.Controllers
{
public class RequerimientoController : Controller
{
    public ActionResult Registros()
    {
        return View("Home");
    }
    [HttpPost]
    public ActionResult Registro()
    {
        //PropiedadesUsu UsuRe = new PropiedadesUsu();
        var constring = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
        var Conn = new SqlConnection(constring);
        try
        {
            Conn.Open();            
            string sqlconsult = "insert into Registros values('" + Request["solicitud"] + "', '" + Request["nombre"] + "','" + Request["apellido"] + "'," + Request["numero"] + ",'" + Request["fecha"] + "','" + Request["departamento"] + "','" + Request["puesto"] + "','" + Request["hotel"] + "'," +1+ ")";
            var command = new SqlCommand(sqlconsult, Conn);
            command.ExecuteNonQuery();
            Conn.Close();
            return RedirectToAction("Home", "Home");
     
        }catch(Exception e)
        {
        
        }       
   }
}
}
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<RequerimientosABCusuarios.Models.PropiedadesUsu>" %>


      <!DOCTYPE html>
      <html>

      <head id="Head1" runat="server">
        <meta name="viewport" content="width=device-width">
        <title>REQUERIMIENTO DE ALTA-CAMBIOS-BAJA DE USUARIOS</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
        <script src="../../Script/Validaciones.js"></script>
        <link href="../../Content/Home.css" rel="stylesheet" />
        <script src="../../Script/DatosSelect.js"></script>



      </head>

      <body>
        <% using (Ajax.BeginForm( "Registro", "Requerimiento", new AjaxOptions { })) {%>


          <div class="titulo">
            <h1>Requerimiento De Alta-Cambio-Baja De Usuarios</h1>

            <div class="Personal">
              <div class="Datos">
                <label for="solicitud">Solicitud</label>
                <select name="solicitud">
                  <option>Alta</option>
                  <option>Cambio</option>
                  <option>Baja</option>
                </select>

                <label for="nombre">Nombre(s)</label>
                <input type="text" id="nombre" name="nombre">

                <label for="numero">Numero De Colaborador</label>
                <input type="text" id="numero" name="numero">

                <label for="departamento">Departamento</label>
                <input type="text" id="departamento" name="departamento">

              </div>

              <div class="Datos2">
                <label for="apellido">Apellidos(s)</label>
                <input type="text" name="apellido">

                <label for="fecha">Fecha De Solicitud</label>
                <input type="date" name="fecha">

                <label for="puesto">Puesto</label>
                <input type="text" name="puesto">
              </div>

              <div class="hoteles"><span></span>
                <label>Hotel - Empresa</label>
                <br />
                <label for="GC"><span>GC</span>
                </label>
                <input type="checkbox" name="hotel" id="GC">
                <label for="GP"><span>GP</span>
                </label>
                <input type="checkbox" name="hotel" id="GP">
                <label for="TRP"><span>TRP</span>
                </label>
                <input type="checkbox" name="hotel" id="TRP">
                <label for="HZLC"><span>HZLC</span>
                </label>
                <input type="checkbox" name="hotel" id="HZLC">
                <label for="HZVC"><span>HZCV</span>
                </label>
                <input type="checkbox" name="hotel" id="HZVC">
                <label for="corporativo"><span>Corporativo</span>
                </label>
                <input type="checkbox" name="hotel" id="corporativo">
              </div>
            </div>
          </div>

          <div class="Subtitulo">
            <h1>Accesos-Aplicaciones-Roles</h1>



            <input type="button" onclick="pasar_parametro('NomSistemas','RecSistemas')" value=">" />
            <input type="button" onclick="pasar_parametro('RecSistemas', 'NomSistemas')" value="<" />

          </div>

          <div class="button">
            <input type="submit" class="BtnGuardar" id="BtnGuardar" name="BtnGuardar" />

          </div>


          <% } %>

            
      </body>

      </html>
    
asked by Dannuu Gomez 01.07.2016 в 18:25
source

1 answer

4

Welcome to the SOes family and to the world of software development, from what I see, I recommend you learn Entity Framework + LinQ ( link ) since it will be very useful for you to play with the databases because it is built in the top of ADO.net and it brings many advantages (you save a good amount of time). On the other hand the query that you generate is vulnerable to inyecciones sql , check this link link so that you expand on security issues in the web and the software that you build has greater security. You will find very few examples about ASP.net View Engine compared to Razor because it is much cleaner, here a comparative table: link and if you want to learn Razor a while ago I did this tutorial, I hope you find it useful: link

And well, with respect to your code, it seems that you need to determine the options of Ajax :

using (Ajax.BeginForm("Registro", "Requerimiento", new AjaxOptions
{
    HttpMethod = "POST",
    InsertionMode = InsertionMode.Replace,
    UpdateTargetId = "ElIDdelFormulario"

}, new { @id="ElIDdelFormulario" }))

Finally I recommend you visit Channel9 ( link ) and the Microsoft Virtual Academy (MVA) link where they have free Full Technology courses Microsoft : ASP.net MVC dictated by high level professionals, these courses are in Spanish and English, and if you want to learn the English: Duolingo ( link ), Memrise ( link ) and EngVid ( link ).

Today is the theme of organizing times rather than money, give all the power 2.0 to the development!

    
answered by 01.07.2016 / 19:23
source