Как отправить данные вашей формы в базу данных при использовании asp. net mvc? - PullRequest
0 голосов
/ 07 апреля 2020

Я хочу найти способ, чтобы представить детали пользовательской информации в базу данных. У меня есть имя таблицы (пустые столбцы) с хранимой процедурой для добавления полей после сохранения в базе данных. Я использую ASP. NET MVC, все эти детали в форме находятся на контроллере, модели. Я поделюсь своим кодом на стороне клиента для понимания того, что именно должно делать мое приложение на внутренней стороне с контроллером. Я не использую Entity Framework, мой источник данных настроен на web.config и использует миграцию данных для доступа к схеме в моей модели (ApplicationDbContext).

<connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=GcobaniM-L\SQLEXPRESS;Initial Catalog=eNtsaRegistration;Integrated Security=True" providerName="System.Data.SqlClient" />
  </connectionStrings>

@using eNtsaRegistrationTraining.Models
@model RoleViewModel

@{
    ViewBag.Ttile = "Dashboard";
    Layout = "~/Views/Shared/_HomeLayout.cshtml";
}

<!---This form is for registration within a Dashbaord layout-->
<center>
    <div class="z-inex" style="width:1000px;">
        <div class="register-logo">

        </div>

        <div class="card">
            <div class="card-body register-card-body">
                <center>
                    <img src="~/Images/eNtsa.png" />
                </center>
                <center>
                    <h1>
                        <p><b>Training Course Registration:Motion MasterClass</b></p>
                    </h1>
                    <hr />
                    <p>Course date: 25-27 February 2020</p>
                    <hr />
                    <p>
                        Note:Please note your registration is submit approval as seats for this course is limited. Once your registration is confirmed, you will receive a quote for subject to your confirmation
                        followed by an invoice.
                    </p>
                </center>
                <hr />
                @using (Html.BeginForm("editRegForm", "Home", FormMethod.Post, new { @role = "form" }))
                {
                    @Html.AntiForgeryToken()

                    <!--All controls for send data to the database be created-->
                    <div class="row">
                        <label for="Title">Title:</label>
                        <div class="input-group col-md-3 col-md-offset-3 col-sm-3 col-xs-3 ">
                            <div class="input-group pull-right">
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                @Html.TextBoxFor(m => m.HomeMainModel.Title, new { @class = "form-control", type = "text", id = "title", autofocus = "autofocus", placeholder = "Title", required = "required" })
                                <div class="input-group-append">
                                    <div class="input-group-text">

                                    </div>
                                </div>
                            </div>

                        </div>
                    </div>
                    <hr />
                    <form>
                        <div class="input-group mb-2">
                            <div class="input-group-prepend">

                            </div>
                            <label for="firstName">Name:</label>
                            <div class="input-group col-md-7 col-md-offset-7 col-sm-7 col-xs-7">
                                <div class="input-group pull-right">
                                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                    @Html.TextBoxFor(m => m.HomeMainModel.FirstName, new { @class = "form-control", type = "text", id = "firstname", autofocus = "autofocus", placeholder = "First Name", required = "required" }) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

                                    @Html.TextBoxFor(m => m.HomeMainModel.LastName, new { @class = "form-control", type = "text", id = "lastname", autofocus = "autofocus", placeholder = "Last Name", required = "required" })
                                    <div class="input-group-append">
                                        <div class="input-group-text">
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </form>

                    <hr />

                    <div class="row">
                        <label for="Position">Position</label>
                        <div class="input-group col-md-3 col-md-offset-3 col-sm-3 col-xs-3">
                            <div class="input-group pull-right">
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                @Html.TextBoxFor(m => m.HomeMainModel.Position, new { @class = "form-control", type = "text", id = "position", autofocus = "autofocus", placeholder = "Position", required = "required" })
                                <div class="input-group-append">
                                    <div class="input-group-text">

                                    </div>
                                </div>
                            </div>
                        </div>

                    </div>
                    <hr />
                    <div class="row">
                        <label for="Address">Company</label>
                        <div class="input-group col-md-3 col-md-offset-3 col-sm-3 col-xs-3">
                            <div class="input-group pull-right">
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                @Html.TextBoxFor(m => m.HomeMainModel.Company, new { @class = "form-control", type = "text", id = "company", autofocus = "autofocus", placeholder = "Company", required = "required" })
                                <div class="input-group-append">
                                    <div class="input-group-text">

                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <hr />
                    <div class="row">
                        <label for="Address">Address</label>
                        <div class="col-md-6 ">
                            <div class="input-group pull-right">
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

                                @Html.TextBoxFor(m => m.HomeMainModel.Address, new { @class = "form-control", type = "text", id = "inputFormVal", autofocus = "autofocus", placeholder = "Street Address", required = "required" })
                                <div class="input-group-append">
                                    <div class="input-group-text">

                                    </div>
                                </div>

                            </div>

                        </div>
                        <div class="col-md-6">
                            <label id="labelMessageBx" class="text-danger" style="display:none"></label>
                        </div>
                    </div>

                    <hr />
                    <div class="row">
                        <div class="col-md-6 ">
                            <div class="input-group pull-right">
                                &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                @Html.TextBoxFor(m => m.HomeMainModel.Address, new { @class = "form-control", type = "text", id = "inputFormVal", autofocus = "autofocus", placeholder = "Street Address Line 2", required = "required" })

                                <div class="input-group-append">
                                    <div class="input-group-text">
                                    </div>
                                </div>
                            </div>

                        </div>
                        <div class="col-md-6">
                            <label id="labelMessageBx" class="text-danger" style="display:none"></label>
                        </div>
                    </div>

                    <hr />
                    <div class="form-group">
                        <div class="input-group mb-2">

                            <div class="input-group-prepend">

                            </div>

                            <div class="input-group col-md-7 col-md-offset-7 col-sm-7 col-xs-7">
                                <div class="input-group pull-right">
                                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                    @Html.TextBoxFor(m => m.HomeMainModel.City, new { @class = "form-control", type = "text", id = "inputFormVal", autofocus = "autofocus", placeholder = "City" })&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                    @Html.TextBoxFor(m => m.HomeMainModel.Code, new { @class = "form-control", type = "text", id = "inputFormVal", autofocus = "autofocus", placeholder = "Province" })

                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <label id="labelMessageBx" class="text-danger" style="display:none"></label>
                        </div>
                        <hr />
                        <!--Zip code for postal code-->
                        <div class="input-group mb-2">

                            <div class="input-group-append">

                            </div>
                            <div class="input-group col-md-7 col-md-offset-3 col-sm-2 col-xs-2">
                                <div class="input-group pull-right">
                                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                    @Html.TextBoxFor(m => m.HomeMainModel.Code, new
                               {
                                   @class = "form-control",
                                   type = "text",
                                   id = "inputFormVal",
                                   autofocus = "autofocus",
                                   placeholder = "Postal/Zip Code"

                                    })
                                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                    @Html.DropDownListFor(m => m.HomeMainModel.SelectedCountryId, this.ViewBag.CountryList as SelectList, new { @class = "form-control" })

                                    <div class="input-group-append">
                                        <div class="input-group-text">

                                        </div>
                                    </div>

                                </div>
                            </div>
                            <div class="col-md-6">
                                <label id="labelMessageBx" class="text-danger" style="display:none"></label>
                            </div>
                        </div>
                    </div>
                    <hr />
                    <div class="row">
                        <div class="input-group col-md-4 col-md-offset-2 col-sm-2 col-xs-2">
                            <div class="input-group pull-right">
                                <label for="Email">Email:</label>
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                <input class="form-control" type="email" id="inputEmail" placeholder="Email address" />
                                <div class="input-group-append">

                                    <div class="input-group-text">

                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <label id="labelMessage" class="text-danger" style="display:none">This field is required</label>
                        </div>
                    </div>

                    <hr />
                    <div class="row">
                        <div class="input-group col-md-4 col-md-offset-2 col-sm-2 col-xs-2">
                            <div class="input-group pull-right">
                                <label for="AlternativeCellNumber">Cell Number:</label>
                                &nbsp;&nbsp;&nbsp;&nbsp;
                                <input class="form-control" type="text" id="cellNumber" placeholder="Cell Number" />
                                <div class="input-group-append">

                                    <div class="input-group-text">

                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6 col-sm-10 col-xs-10">
                            <label id="labelMessagec" class="text-danger" style="display:none">This field is required</label>
                        </div>
                    </div>

                    <hr />
                    <form>
                        <div class="row">
                            <label for="Please verify that you are a human">Please verify that you are a human</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

                            <div class="input-group col-md-4 col-md-offset-2 col-xs-2">
                                <div class="input-group pull-left">
                                    @using (Html.BeginForm("FormSubmit", "Home", FormMethod.Post))
                                    {
                                        <div class="g-recaptcha" data-sitekey="6LezHOIUAAAAANq0FvFWWbvBlb6Vc1Y2YwnixVqJ">

                                        </div>



                                    }
                                </div>
                            </div>

                        </div>
                        <hr />

                        <div class="form-group">
                            <div class="input-group mb-2">
                                <label for="Dietary_Req">Dietary Requirement</label>
                                <div class="input-group-append">
                                </div>
                                <div class="input-group col-md-offset-3 col-sm-7 col-xs-7">
                                    <div class="input-group pull-right">
                                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                        @Html.CheckBoxFor(m => m.DietaryModel.None)
                                        @Html.LabelFor(m => m.DietaryModel.None)

                                    </div>
                                    <hr />
                                    <div class="input-group col-md-offset-3 col-sm-7 col-xs-7">
                                        <div class="input-group pull-right">
                                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                            @Html.CheckBoxFor(m => m.DietaryModel.Vegetarian)
                                            @Html.LabelFor(m => m.DietaryModel.Vegetarian)
                                        </div>
                                    </div>
                                    <hr />
                                    <div class="input-group col-md-offset-3 col-sm-7 col-xs-7">
                                        <div class="input-group pull-right">
                                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                            @Html.CheckBoxFor(m => m.DietaryModel.Vegan)
                                            @Html.LabelFor(m => m.DietaryModel.Vegan)
                                        </div>
                                    </div>
                                    <hr />
                                    <div class="input-group col-md-offset-3 col-sm-7 col-xs-7">
                                        <div class="input-group pull-right">
                                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                            @Html.CheckBoxFor(m => m.DietaryModel.Halaal)
                                            @Html.LabelFor(m => m.DietaryModel.Halaal)
                                        </div>
                                    </div>
                                    <hr />
                                    <div class="input-group col-md-offset-3 col-sm-6 col-xs-6">
                                        <div class="input-group pull-right">
                                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                            @Html.CheckBoxFor(m => m.DietaryModel.Other)
                                            @Html.TextBoxFor(m => m.DietaryModel.Other, new
                                            {
                                                @class = "form-control",
                                            type = "text",
                                            autofocus = "autofocus",
                                            placeholder = "Other"
                                            })
                                        </div>
                                    </div>
                                </div>

                            </div>
                        </div>
                    </form>

                    <hr />

                    <!--Submit form button to allow insert data to db-->
                    <div class="form-group">
                        <hr />
                        <button type="button" value="Save" class="btn btn-primary">Submit</button>
                        @ViewBag.Message
                    </div>

                }
            </div>
        </div>
    </div>




</center>

<!----Javascript for form validation including recapture on Google-->

<script src="https://www.google.com/recaptcha/api.js" type="text/javascript">
</script>


<!---Handling of error handling for the form-->

<script type='text/javascript'>
    $(function () {
        //When the blur event occurs from your Textbox (you lose focus)
        $('#inputEmail').blur(function () {
            var email = document.getElementById("inputEmail").value;
            var expr = /^([\w-\.]+)@@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
            if (!expr.test(email)) {
                document.getElementById("labelMessage").style.display = "inline";
            }
            else {
                document.getElementById("labelMessage").style.display = "none";
            }
        });

    });

    // function when leaving texbox for cell number.
    $(function () {
        $('#cellNumber').blur(function () {
            var cell = document.getElementById("cellNumber").value;
            var expr = /^([\w-\.]+)@@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
            if (!expr.test(cell)) {
                document.getElementById("labelMessagec").style.display = "inline";
            }
            else {
                document.getElementById("labelMessagec").style.display = "none";
            }
        });
    });

    // function when leaving a caption.
    $(function () {
        $('#capTion').blur(function () {
            var captionz = document.getElementById("#capTion").value;
            if (!expr.test(captionz)) {
                document.getElementById("labelMessageG").style.display = "inline";
            }
            else {
                document.getElementById("labelMessageG").style.display = "none";
            }

        });

    });

    // function when leaving texbox for city, street address, street_address_line2, state_province.

    $(function () {
        $('#inputFormVal').blur(function () {
            var city = document.getElementById("inputFormVal").value;
            var expr = /^([\w-\.]+)@@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
            if (!expr.test(city)) {
                document.getElementById("labelMessageBx").style.display = "inline";
            } else {
                document.getElementById("labelMessageBx").style.display = "none";
            }
        });
    });




</script>
...