提交重定向到同一页面后

时间:2015-05-06 12:41:52

标签: c# asp.net-mvc-4 razorengine

我正在使用smoothscroll导航,如下所示:

 <li><a href="#home" class="smoothScroll">HOME</a></li>

我有一个联系表格,如下:

<section id="contact" class="text-center">




    @using (Html.BeginForm("contact","Home", FormMethod.Post, new { @enctype = "multipart/form-data" }))
    {
        @Html.AntiForgeryToken()

        <section id="contact" class="text-center">
            <div class="container">
                <div class="row">
                    <div class="col-md-12 wow fadeInDown" data-wow-delay="2000">
                        <h3>CONTACT Microrout.nl</h3>
                    </div>
                    <div class="col-md-2"></div>
                    <div class="col-md-8">

                        Microrout.nl
                        <br />frederik hendriklaan 253-b
                        <br />Den Haag
                        <br />0641697144


                    </div>
                    <div class="col-md-2"></div>
                    <hr />
                    <div class="form-group">
                        @Html.LabelFor(m => m.FromName, new { @class = "col-md-3" })
                        <div class="col-md-10">
                            @Html.TextBoxFor(m => m.FromName, new { @class = "form-control" })
                            @Html.ValidationMessageFor(m => m.FromName)
                        </div>
                    </div>
                    <div class="form-group">
                        @Html.LabelFor(m => m.FromEmail, new { @class = "col-md-3 control-label" })
                        <div class="col-md-10">
                            @Html.TextBoxFor(m => m.FromEmail, new { @class = "form-control" })
                            @Html.ValidationMessageFor(m => m.FromEmail)
                        </div>
                    </div>
                    <div class="form-group">
                        @Html.LabelFor(m => m.Message, new { @class = "col-md-3 control-label" })
                        <div class="col-md-10">
                            @Html.TextAreaFor(m => m.Message, new { @class = "form-control" })
                            @Html.ValidationMessageFor(m => m.Message)
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-md-3">
                            <input type="submit" value="SHOOT MESSAGE" class="form-control">
                        </div>
                    </div>
                </div>
            </div>
            @*<div class="google_map">
                    <div id="map-canvas"></div>
                </div>*@


        </section>
    }






</section>

这是联系方式:

public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Contact(EmailFormModel model)
        {
            string message2 = "There are a few errors";
            if (ModelState.IsValid)
            {
                try
                {


                    message2 = "Thanks! We'll get back to you soon.";
                    var body = "<p>Email From: {0} ({1})</p><p>Message:</p><p>{2}</p>";
                    var message = new MailMessage();
                    message.To.Add(new MailAddress("nengelen@online.nl")); //replace with valid value
                    message.Subject = "Your email subject";
                    message.Body = string.Format(body, model.FromName, model.FromEmail, model.Message);
                    message.IsBodyHtml = true;

                    using (var smtp = new SmtpClient())
                    {
                        await smtp.SendMailAsync(message);

                    }



                }
                catch (Exception error)
                {
                    Response.Write("Error sending email: " + error.Message + "<br /> StackTrace: " + error.StackTrace);
                }
                ViewBag.Message = "Thank you for contact us";
                //return new RedirectToActionAnchor("Contact", "", "#contact");

            }

            //if (Request.IsAjaxRequest())
            //{
            //    return new JsonResult { Data = new { success = true, message = message2 } };
            //}

            //TempData["Message"] = message2;


            return View(model);
        }

但是如何在提交后返回联系表单,因为提交后的url更改为:

http://localhost:53534/Home/contact

谢谢

我现在就这样:

[HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Contact(EmailFormModel model)
        {
            string message2 = "There are a few errors";
            if (ModelState.IsValid)
            {
                try
                {


                    message2 = "Thanks! We'll get back to you soon.";
                    var body = "<p>Email From: {0} ({1})</p><p>Message:</p><p>{2}</p>";
                    var message = new MailMessage();
                    message.To.Add(new MailAddress("nengelen@online.nl")); //replace with valid value
                    message.Subject = "Your email subject";
                    message.Body = string.Format(body, model.FromName, model.FromEmail, model.Message);
                    message.IsBodyHtml = true;

                    using (var smtp = new SmtpClient())
                    {
                        await smtp.SendMailAsync(message);

                    }



                }
                catch (Exception error)
                {
                    Response.Write("Error sending email: " + error.Message + "<br /> StackTrace: " + error.StackTrace);
                }
                ViewBag.Message = "Thank you for contact us";
                //return new RedirectToActionAnchor("Contact", "", "#contact");

            }

            //if (Request.IsAjaxRequest())
            //{
            //    return new JsonResult { Data = new { success = true, message = message2 } };
            //}

            //TempData["Message"] = message2;


            return View("Contact",model);
        }

但问题是它会转到:http://localhost:53534/Home/contact

但是,联系人网址是:http://localhost:53534/#contact

这是我的导航:

<div class="collapse navbar-collapse">
                        <ul class="nav navbar-nav navbar-right">
                            <li><a href="#home" class="smoothScroll">HOME</a></li>
                            <li><a href="#about" class="smoothScroll">STUDIO</a></li>
                            <li><a href="#team" class="smoothScroll">TEAM</a></li>
                            <li><a href="#service" class="smoothScroll">SERVICES</a></li>
                            <li><a href="#work" class="smoothScroll">WORK</a></li>
                            <li><a href="#pricing" class="smoothScroll">PRICING</a></li>
                            <li><a href="#contact" class="smoothScroll">CONTACT</a></li>
                            @*<li>@Html.ActionLink("Contact", "Contact", "Home")
                            </li>*@
                        </ul>
                    </div>

这是我的家庭控制器:

public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }



        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Contact(EmailFormModel model)
        {
            string message2 = "There are a few errors";
            if (ModelState.IsValid)
            {
                try
                {


                    message2 = "Thanks! We'll get back to you soon.";
                    var body = "<p>Email From: {0} ({1})</p><p>Message:</p><p>{2}</p>";
                    var message = new MailMessage();
                    message.To.Add(new MailAddress("nengelen@online.nl")); //replace with valid value
                    message.Subject = "Your email subject";
                    message.Body = string.Format(body, model.FromName, model.FromEmail, model.Message);
                    message.IsBodyHtml = true;

                    using (var smtp = new SmtpClient())
                    {
                        await smtp.SendMailAsync(message);

                    }



                }
                catch (Exception error)
                {
                    Response.Write("Error sending email: " + error.Message + "<br /> StackTrace: " + error.StackTrace);
                }
                ViewBag.Message = "Thank you for contact us";
                //return new RedirectToActionAnchor("Contact", "", "#contact");

            }

            //if (Request.IsAjaxRequest())
            //{
            //    return new JsonResult { Data = new { success = true, message = message2 } };
            //}

            //TempData["Message"] = message2;


            return View("Home",model);
        }
    }

似乎如果我使用它:return Redirect(Url.RouteUrl(&#34; Home&#34;)+&#34; #contact&#34;);锚(#)将被跳过

1 个答案:

答案 0 :(得分:1)

不是返回视图,而是返回重定向:

return RedirectToAction("Index");

由于您还希望跳转到页面上的锚点,您可以使用以下内容:

return Redirect(Url.RouteUrl("Index") + "#contact");

这是Post-Redirect-Get模式,可确保您无法双重提交操作。但是,在这种情况下,您将丢失ViewBag.Message

中的信息