0

For the first time, I want to send a value by AJAX.

I wrote code with the skills I learned, but even the values are not sent to the controller.

This is my HTML tags:

<div class="container">
        <div class="card marginAuto" id="draggable">
            <div class="card-header">فرم ثبت اطلاعات</div>
            <div class="card-body">

                <form>
                    <div class="form-group">
                        <div class="input-group">
                            <div class="input-group-prepend" id="input-group-prepend-Email">
                                <span class="input-group-text" id="inputGroupPrepend3"><img src="~/Images/warning.png" id="warningEmail" /><img src="~/Images/checked.png" id="successEmail" /></span>
                            </div>
                            <input type="email" class="form-control" id="email" placeholder="ایمیل" title="پر کردن این بخش الزامی است">
                            <div class="invalid-feedback">
                                <small class="form-text text-danger text-right" id="emailDanger">ایمیل وارد شده اشتباه است</small>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="input-group">
                            <div class="input-group-prepend" id="input-group-prepend-Mobile">
                                <span class="input-group-text"><img src="~/Images/warning.png" id="warningMobile" /><img src="~/Images/checked.png" id="successMobile" /></span>
                            </div>
                            <input type="text" class="form-control" id="Mobile" placeholder="شماره موبایل" title="پر کردن این بخش الزامی است" maxlength="11">
                            <div class="invalid-feedback">
                                <small class="form-text text-danger text-right" id="MobileDanger">ایمیل وارد شده اشتباه است</small>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="input-group">
                            <input type="password" class="form-control" id="Pass" placeholder="رمز عبور" title="رمز عبور خوب بین 8 تا 12 کاراکتر است">
                            <div class="invalid-feedback" id="invalid-feedback-password">
                                <small class="form-text text-danger text-right" id="passDanger">حداقل رمز عبور 8 کاراکتر است</small>
                                <div class="progress">
                                    <div id="progressBarPass" class="progress-bar progress-bar-striped progress-bar-animated" role="progressbar" aria-valuenow="75" aria-valuemin="0" aria-valuemax="100" style="width:0"></div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="form-group form-check">
                        <label class="form-check-label">
                            <input class="form-check-input" type="checkbox"> مرا به خاطر بسپار
                        </label>
                    </div>
                    <div id="Message">

                    </div>
                </form>
            </div>
            <div id="loading"><img src="~/Images/ajax-loader.gif" alt="loader" /></div>
            <div class="card-footer"><button class="btn btn-dark" type="submit" title="کلیک کنید" id="btn-Send">ثبت</button></div>
        </div>
    </div>


    <script src="~/Scripts/jquery-3.3.1.min.js"></script>
    <script src="~/Scripts/popper.min.js"></script>
    <script src="~/Scripts/bootstrap.min.js"></script>

I added a script in the end of my Tags.

This is My Jquery Ajax Code :

$("#btn-Send").click(function () {
                $("div#loading").show();
                $("button#btn-Send").hide();
                var csUser = { UserEmail: txt_email, UserMobile: txt_mobile, UserPassword: txt_Pass };
                    $.ajax({
                        type: "POST",
                        dataType: "json",
                        data: csUser,
                        url: "@Url.Action("InsertAjax","Test")",
                        error: function (response) {
                            $("div#Message").html("Error on sending or receiving data!");
                        },
                        success: function (response) {
                            $("input#email").val("");
                            $("input#Mobile").val("");
                            $("input#Pass").val("");
                            $("div#message").html(response.Message);
                        },
                        complete: function (response) {
                            $("div#loading").hide();
                            $("button#btn-Send").show();
                        },
                    });
            });

In this script I have been asked to get the values and send it to the controller

But these values do not even enter the controller.

This is my controller Code :

[HttpPost]
    public JsonResult InsertAjax(string UserEmail,string UserMobile,string UserPassword)
    {
        int Ret = MyUser.InsertAjax(UserEmail,UserMobile,UserPassword);
        return Json("", JsonRequestBehavior.AllowGet);
    }

When the controller receives the value, it sends it to a method called Insertajax in the csUser class.

This is My Model's Code:

public class csUser
{
    public int UserID { get; set; }
    public string UserEmail { get; set; }
    public string UserMobile { get; set; }
    public string UserPassword { get; set; }

    public int InsertAjax(string UE,string UM,string UP)
    {
        DbCommand MyCommand = GozargahCRUD.CreateCommandProcedure();

        try
        {
            MyCommand.CommandText = "sp_UserInsertRegisterAjax";
            DbParameter Param;

            Param = MyCommand.CreateParameter();
            Param.DbType = DbType.Int32;
            Param.ParameterName = "@Ret";
            Param.Direction = ParameterDirection.ReturnValue;
            MyCommand.Parameters.Add(Param);


            Param = MyCommand.CreateParameter();
            Param.DbType = DbType.String;
            Param.ParameterName = "@UserEmail";
            Param.Value = UE;
            Param.Direction = ParameterDirection.Input;
            MyCommand.Parameters.Add(Param);

            Param = MyCommand.CreateParameter();
            Param.DbType = DbType.String;
            Param.ParameterName = "@UserMobile";
            Param.Value = UM;
            Param.Direction = ParameterDirection.Input;
            MyCommand.Parameters.Add(Param);

            Param = MyCommand.CreateParameter();
            Param.DbType = DbType.String;
            Param.ParameterName = "@UserPassword";
            Param.Value = UP;
            Param.Direction = ParameterDirection.Input;
            MyCommand.Parameters.Add(Param);

            int Ret = GozargahCRUD.ExecuteNonQuery(MyCommand);
            return Ret;
        }
        catch (Exception err)
        {

            throw;
        }



    }
}

What do you think is wrong with my work?

If it's a mistake, can you give me a complete example for ADO.NET?

Thanks Guys!!!

Mojtaba
  • 13
  • 6

1 Answers1

0

You should not give me credit, rather give Rory credit.

But here I prove it works:

cshtml:

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Tut103</title>
    @*Mojtaba , I put these in the head; I changed the version on the jquery*@
    @*just want to solve your problem*@
    @*<script src="~/Scripts/jquery-3.3.1.min.js"></script>*@
    <script src="~/Scripts/jquery-1.12.4.min.js"></script>
    @*Took out the next two , will only put back if needed
        <script src="~/Scripts/popper.min.js"></script>
        <script src="~/Scripts/bootstrap.min.js"></script>*@
</head>
<body>
    <div class="container">
        <div class="card marginAuto" id="draggable">
            <div class="card-header">فرم ثبت اطلاعات</div>
            <div class="card-body">

                <form>
                    <div class="form-group">
                        <div class="input-group">
                            <div class="input-group-prepend" id="input-group-prepend-Email">
                                <span class="input-group-text" id="inputGroupPrepend3"><img src="~/Images/warning.png" id="warningEmail" /><img src="~/Images/checked.png" id="successEmail" /></span>
                            </div>
                            <input type="email" class="form-control" id="email" placeholder="ایمیل" title="پر کردن این بخش الزامی است">
                            <div class="invalid-feedback">
                                <small class="form-text text-danger text-right" id="emailDanger">ایمیل وارد شده اشتباه است</small>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="input-group">
                            <div class="input-group-prepend" id="input-group-prepend-Mobile">
                                <span class="input-group-text"><img src="~/Images/warning.png" id="warningMobile" /><img src="~/Images/checked.png" id="successMobile" /></span>
                            </div>
                            <input type="text" class="form-control" id="Mobile" placeholder="شماره موبایل" title="پر کردن این بخش الزامی است" maxlength="11">
                            <div class="invalid-feedback">
                                <small class="form-text text-danger text-right" id="MobileDanger">ایمیل وارد شده اشتباه است</small>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="input-group">
                            <input type="password" class="form-control" id="Pass" placeholder="رمز عبور" title="رمز عبور خوب بین 8 تا 12 کاراکتر است">
                            <div class="invalid-feedback" id="invalid-feedback-password">
                                <small class="form-text text-danger text-right" id="passDanger">حداقل رمز عبور 8 کاراکتر است</small>
                                <div class="progress">
                                    <div id="progressBarPass" class="progress-bar progress-bar-striped progress-bar-animated" role="progressbar" aria-valuenow="75" aria-valuemin="0" aria-valuemax="100" style="width:0"></div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="form-group form-check">
                        <label class="form-check-label">
                            <input class="form-check-input" type="checkbox"> مرا به خاطر بسپار
                        </label>
                    </div>
                    <div id="Message">
                    </div>
                </form>
            </div>
            @*Yes the comment is correct that this made right makes a world of difference
                I added these three needed fields, you can add using a helper and model data*@
            <input type="text" id="txt_email" value="emailValue@a.a">
            <input type="text" id="txt_mobile" value="mobileText">
            <input type="text" id="txt_Pass" value="PassText">
            <div id="loading"><img src="~/Images/ajax-loader.gif" alt="loader" /></div>
            <div class="card-footer"><button class="btn btn-dark" type="submit" title="کلیک کنید" id="btn-Send">ثبت</button></div>
        </div>
    </div>

    <script type="text/javascript">
        //adding the wrap with wait for DOM to load; although might not need since this script is at the end of page
        $(function () {
            $("#btn-Send").click(function () {
                $("div#loading").show();
                $("button#btn-Send").hide();
                //Yes the comment is correct that this made right makes a world of difference
                //Also, put proper jquery identifiers
                var csUser = { UserEmail: $("#txt_email").val(), UserMobile: $("#txt_mobile").val(), UserPassword: $("#txt_Pass").val() };
                alert("ap");
                $.ajax({
                    type: "POST",
                    dataType: "json",
                    data: csUser,
                    //you use Test controller, I am using Home
                    url: "@Url.Action("InsertAjax","Home")",
                    error: function (response) {
                        //$("div#Message").html("Error on sending or receiving data!");
                        alert(response);
                    },
                    success: function (response) {
                        //$("input#email").val("");
                        //$("input#Mobile").val("");
                        //$("input#Pass").val("");
                        //$("div#message").html(response.Message);
                    },
                    complete: function (response) {
                        $("div#loading").hide();
                        $("button#btn-Send").show();
                    },
                });
            });
        })
    </script>

</body>
</html>

Controller:

namespace Testy20161006.Controllers
{
    public class csUser
    {
        public string UserEmail { get; set; }
        public string UserMobile { get; set; }
        public string UserPassword { get; set; }
    }

    public class HomeController : Controller
    {
        [HttpPost]
        public ActionResult InsertAjax(csUser csUser)
        {
            //!! Put a breakpoint here and see the values passed in for csUser
            return Json(new
            {
                ap = "dh"
            }
                , @"application/json");
        }
kblau
  • 2,094
  • 1
  • 8
  • 20