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

时间:2015-10-14 12:15:23

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

我正在使用MVC进行应用程序。在我看来,我有一个名为EmailId的文本框和一个Submit按钮。如果我输入电子邮件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>

我的控制器代码是

 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();
        }

任何人都可以帮助我吗? 在此先感谢。

2 个答案:

答案 0 :(得分:3)

每当我们想要更新网页中的值而不刷新时,我们就必须使用Ajax。

我们必须做以下事情才能使您的网页有效。

  1. 从视图中删除BeginForm块,因为当我们使用BeginForm时,它会向控制器发送请求并刷新页面。
  2. 使用Ajax将信息传递给控制器​​并更新页面而不刷新它。
  3. 由于您在控制器中有两个POST操作,因此请将两个div保留为“rnd”和“email”

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

      $('#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");
        }
      });
    });
    

答案 1 :(得分:0)

首先,您需要使用Ajax.BeginForm

Using Ajax.BeginForm with ASP.NET MVC 3 Razor

success函数上,您可以编写以下代码用于明文EmailId和一个Submit按钮。

$("#EmailId").val("");
$("#btn").val("Verify");

如果您打算执行上述操作,则不需要两种表格。