Thursday 23 January 2014

How to redirect to another page from MVC controller in mvc4



Description:-

            In Previous Example We Explain that How to Export Gridview or Webgrid Data to MS Word or MS Excel sheet in MVC4.but now in this example we explain that how to redirect one page to another page in mvc or redirect from Controller to view page in mvc.

This is simple just you can pass the url in controller method and in which we create a code to redirect to the specified URL argument like

<form action="/Home/Goto" method="post">
    <label for="gotourl">url:</label>
    <input type="text" name="gotourl" />
    <input type="submit" />
    </form>

In above code you enter the url of the page that you want to redirect and when click on submit button then it will call the Goto method of the Homecontroller like
public ActionResult Goto(string gotourl)
        {
         

            return Redirect(gotourl);
        }

Look at this method simple redirect the page with  gotourl argument that you can enter in the Textbox.
The main thing keep in mind is that  <input type="text" name="gotourl" /> name of textbox and argument of method both are same like name of textbox gotourl and argument of the method like public ActionResult Goto(string gotourl) must be same that you are define in controller method.


Implement Remember Me functionality using CheckBox ASP.Net 

set WaterMark Text in PDF using itextsharp in C#

How to set Default Button in MVC Web Form Application 

How to Bind XML File data to Treeview in asp.net

JQuery datepicker calender with Dropdown month and year in asp.net. 




Index.cshtml:-

@{
  
}
@section featured {
    <section class="featured">
        <div class="content-wrapper">
            <hgroup class="title">
                <h1>@ViewBag.Title.</h1>
                <h2>@ViewBag.Message</h2>
            </hgroup>
            <p>
                To learn more about ASP.NET MVC visit
                <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
                The page features <mark>videos, tutorials, and samples</mark> to help you get the most from ASP.NET MVC.
                If you have any questions about ASP.NET MVC visit
                <a href="http://forums.asp.net/1146.aspx/1?MVC" title="ASP.NET MVC Forum">our forums</a>.
            </p>
        </div>
    </section>
}
<h3>We suggest the following:</h3>
<ol class="round">
    <li class="one">
        <h5>Getting Started</h5>
        ASP.NET MVC gives you a powerful, patterns-based way to build dynamic websites that
        enables a clean separation of concerns and that gives you full control over markup
        for enjoyable, agile development. ASP.NET MVC includes many features that enable
        fast, TDD-friendly development for creating sophisticated applications that use
        the latest web standards.
        <a href="http://go.microsoft.com/fwlink/?LinkId=245151">Learn more…</a>
    </li>

    <li class="two">
        <h5>Add NuGet packages and jump-start your coding</h5>
        NuGet makes it easy to install and update free libraries and tools.
        <a href="http://go.microsoft.com/fwlink/?LinkId=245153">Learn more…</a>
    </li>

    <li class="three">
        <h5>Find Web Hosting</h5>
        You can easily find a web hosting company that offers the right mix of features
        and price for your applications.
        <a href="http://go.microsoft.com/fwlink/?LinkId=245157">Learn more…</a>
    </li>
</ol>
<form action="/Home/Goto" method="post">
    <label for="gotourl">url:</label>
    <input type="text" name="gotourl" />
    <input type="submit" />
    </form>


 HomeController:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace mvcredirectpage.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message ="Hi Everyone";
            ViewBag.Title = "kirit";

            return View();
        }

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

            return View();
        }

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

            return View();
        }
        public ActionResult Goto(string gotourl)
        {
         

            return Redirect(gotourl);
        }
    }
}

This entry was posted in :

3 comments: