c# - 在 mVC4 中更改按钮文本而不刷新页面

标签 c# asp.net-mvc asp.net-mvc-4 razor ajaxform

我正在用 MVC 做我的应用程序。在我看来,我有一个名为 EmailId 的文本框和一个提交按钮。如果我输入电子邮件 ID 并提交按钮,按钮的标签要更改为验证并且应清除文本框而不刷新页面。

我的查看页面是

    <div class="sign" id="email">
        @using (Html.BeginForm("Randa", "BU", FormMethod.Post))
        {
            <div class="sign1">

                <div class="sign2" style="height:267px;width:562px;margin-left:214px" id="cdiv">
                                     @Html.TextBox("EmailId","", new {@placeholder ="Enter the Email id",id="txtemail "})<br /><br />

                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" name="submit" value="Sign Up" id="btn" onclick="addbutton()" class="addbutton"/>


                </div>
            </div>

        }
    </div>
    <div class="drnd" id="rnd" style="display:none">
        @using (Html.BeginForm("Ra_verify", "BU", FormMethod.Post))
        {
            <div class="sign1">

                <div class="sign2" style="height:267px;width:562px;margin-left:214px" id="cdiv">
                    @Html.TextBox("Getran", "", new { @placeholder = "Enter the Randam", id = "txtrnd" })<br /><br />

                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" name="submit" value="Verify" id="btnrnd" class="addbutton" />


                </div>
            </div>

        }
    </div>
    }
<script type="text/javascript">
        var btxt = "Verified";
        document.getElementById("#btn").innerHTML = btxt;
    </script>
    <script type="text/javascript">
        function addbutton()
        {

             ($("#email").hide())

                $("#rnd").show();


        }
    </script>

我的 Controller 代码是

 public ActionResult Randa()
        {
            return View();
        }
        [HttpPost]

        // send the randam No to the Entered mail id . Store the mail id and randam no into tbl_bussiness table;
        public ActionResult Randa(string EmailId, string submit) 
        {
            string userId = System.Configuration.ConfigurationManager.AppSettings["UserTypeId"];
            int typeid = Convert.ToInt32(userId);
            if (ModelState.IsValid)
            {
                   if (submit != null && EmailId != null)
                    {
                        EmailManager.SendConfirmationEmail(EmailId);
                        tbl_BusinessUser b = new tbl_BusinessUser();
                        b.EmailId = EmailId;
                        b.RandomNumber = (int)Session["rnd"];
                        b.UserTypeId = typeid;
                        b.CreateDTTM = System.DateTime.Now;
                        db.tbl_BusinessUser.Add(b);
                        db.SaveChanges();


                        ViewBag.message = "Please check ur Mail for randam no.Enter random in textbox ";
                    }
                    else
                    {
                        ModelState.AddModelError("", "Error");

                }
            }


                return View();

            }
        public ActionResult Ra_verify()
        {
            return View();
        }
        [HttpPost]
        // check the random no with table random no ,if match redirect to registration create page
        public ActionResult Ra_verify(int EmailId, string submit)
        {
            if (submit != null)
            {
               // int c = Convert.ToInt32(EmailId);
                tbl_BusinessUser b = new tbl_BusinessUser();
                var tbra = db.tbl_BusinessUser.Where(x => x.RandomNumber == EmailId).FirstOrDefault();
                //var tbram = Convert.ToInt32(tbra);


                    return RedirectToAction("Create", "BU");

            }
            return View();
        }

谁能帮帮我? 提前致谢。

最佳答案

每当我们想在不刷新的情况下更新网页中的值时,我们必须使用 Ajax。

我们必须执行以下操作才能使您的页面正常运行。

  1. 从您的 View 中删除 BeginForm block ,因为当我们使用 BeginForm 时,它会向 Controller 发送请求并刷新页面。
  2. 使用 Ajax 将信息传递给 Controller ​​并在不刷新页面的情况下更新页面。

因为你在 Controller 中有两个 POST 操作,所以保留两个 div“rnd”和“email”

这是带有 Ajax 选项的示例脚本 block ,可根据您的请求更新页面,

  $('#btn').click(function () {

  var urlinfo = '/Home/Randa';
  var textboxValue = $('#txtemail').val();

  $.ajax({
    type: "POST",
    data: { value: textboxValue },
    url: urlinfo,
    success: function (result) {          
      $('#email').hide();
      $('#rnd').show();
    },
    error: function () {
      alert("failed");
    }
  });
});

关于c# - 在 mVC4 中更改按钮文本而不刷新页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33125033/

相关文章:

c# - ASP.NET 中的动态表单生成

asp.net-mvc-4 - 同一台计算机上的两个 Chrome session - 一个将连接到我们的 Azure 网站,另一个 "unable to connect to SQL Server database"

.net - MVC 4 模拟 HttpContext - 如何模拟 DisplayModeProvider

c# - EF core 2.1 中具有 lambda 表达式的最大 n 个组

c# - 无法显示链接字段值表+ReportViewer

c# - 如何使用 LINQ 从 sql 查询编写 linq 查询?

asp.net-mvc - 没有 App_Data,Asp.net mvc 无法工作

c# - OWIN 启动类丢失

c# - 在 WPF 应用程序中使用服务定位器模式时的 View 模型范围

asp.net-mvc - "The semaphore timeout period has expired"SQL Azure