locked
pass input data from view to controller RRS feed

  • Question

  • User-1634604574 posted

    how can i pass input data from view to controller and then use that value in another view

    Sunday, November 11, 2018 4:16 AM

Answers

  • User-369506445 posted

    hi

    you can pass your inputs data with using a form element below like :

    create a controller called Home

    then create an action called Index

    after that create a view called Index and put below like

    @using (Html.BeginForm("MyAction","Home"))
    {
        <input type="text"  name="myText"/>
        <input type="submit" value="send" />
    }


     next in your controller put below code

    public ActionResult Index()
            {
                return View();
            }
            
            public ActionResult MyAction(string myText)
            {
                ViewBag.MyText = myText;
                return View("Details");
            }

    now create a View called Details and put below

    <p>@ViewBag.MyText</p>
    
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, November 11, 2018 5:26 AM
  • User-369506445 posted

    Of course, you can, please try below code instead

    <input type="text" name="myText" id="myText" />
    <input type="submit" value="send" id="btn" />
    
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $('#btn').click(function (evt) {
                $.ajax({
                    type: 'GET',
                    url:'@Url.Action("MyAction", "Home")',
                    cache: false,
                    data: { myText: $("#myText").val() },
                    success: function (data) {
                    },
                    error: function (req) {
    
                    }
                });
    
                // we make sure to cancel the default action of the link
                // because we will be sending an AJAX call
                return false;
            });
        });
    </script>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, November 11, 2018 6:25 AM
  • User-369506445 posted

    as far as I understood, you want to send a parameter from view to your action and then in the action redirect to another action or controller

    if yes, you can not do it with ajax,but you can  go to another controller after get ajax result in success or error function

    <script type="text/javascript">
        $(function () {
            $('#btn').click(function (evt) {
                $.ajax({
                    type: 'GET',
                    url:'@Url.Action("MyAction", "Home")',
                    cache: false,
                    data: { myText: $("#myText").val() },
                    success: function (data) {
                        location.href = '@Url.Action("Index","Home2")' + "?myText=" + $("#myText").val()
                    },
                    error: function (req) {
    
                    }
                });
    
                // we make sure to cancel the default action of the link
                // because we will be sending an AJAX call
                return false;
            });
        });
    </script>

    and create another controller called Home2Controller

    public class Home2Controller : Controller
        {
            // GET: Home2
            public ActionResult Index(string myText)
            {
                ViewBag.myText = myText;
                return View();
            }
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, November 11, 2018 7:02 AM
  • User-369506445 posted

    also, you can use jquery below like

    in you index view

    <input type="text" name="myText" id="myText" />
    <a id="btn" href="@Url.Action("MyAction","Home")">Go to Home Index</a>
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script>
        $(document).ready(function () {
            $("#btn").click(function (event) {
                event.preventDefault();
                window.location.href = "@Url.Action("MyAction", "Home")" + "?myText=" + $("#myText").val();
            });
        });
    </script>

    in controller

    public ActionResult Index()
            {
                return View();
            }
            
            public ActionResult MyAction(string myText)
            {
                ViewBag.MyText = myText;
                return View("Details");
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, November 11, 2018 7:08 AM
  • User-369506445 posted

     in this sample, I created 3 input

    <input type="text" name="myText" id="myText1" />
    <input type="text" name="myText" id="myText2" />
    <input type="text" name="myText" id="myText3" />
    
    
    <a id="btn" href="@Url.Action("MyAction","Home")">Go to Home Index</a>
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script>
        $(document).ready(function () {
            $("#btn").click(function (event) {
                event.preventDefault();
                window.location.href = "@Url.Action("MyAction", "Home")" +
                    "?myText1=" + $("#myText1").val() +
                    "&myText2=" + $("#myText2").val() +
                    "&myText3=" + $("#myText3").val();
            });
        });
    </script>
    
    

    in Controller

    public ActionResult Index()
            {
                return View();
            }
            
            public ActionResult MyAction(string myText1, string myText2, string myText3)
            {
                string[] arr = new string[] { myText1, myText2, myText3 };
    
                ViewBag.MyText = arr;
                return View("Details");
            }

    and in Details View

    @foreach (var item in @ViewBag.MyText)
    {
        <p>@item</p>
    }
    
    
    or
    <br /> param 1 :<span>@ViewBag.MyText[0]</span> <br /> param 2 :<span>@ViewBag.MyText[1]</span> <br />

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, November 11, 2018 7:09 AM

All replies

  • User-369506445 posted

    hi

    you can pass your inputs data with using a form element below like :

    create a controller called Home

    then create an action called Index

    after that create a view called Index and put below like

    @using (Html.BeginForm("MyAction","Home"))
    {
        <input type="text"  name="myText"/>
        <input type="submit" value="send" />
    }


     next in your controller put below code

    public ActionResult Index()
            {
                return View();
            }
            
            public ActionResult MyAction(string myText)
            {
                ViewBag.MyText = myText;
                return View("Details");
            }

    now create a View called Details and put below

    <p>@ViewBag.MyText</p>
    
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, November 11, 2018 5:26 AM
  • User-1634604574 posted

    whithout 

    Html.BeginForm is there another way to send tha value to controller? for example by ajax can i send value from view to controler?
    Sunday, November 11, 2018 6:13 AM
  • User-369506445 posted

    Of course, you can, please try below code instead

    <input type="text" name="myText" id="myText" />
    <input type="submit" value="send" id="btn" />
    
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $('#btn').click(function (evt) {
                $.ajax({
                    type: 'GET',
                    url:'@Url.Action("MyAction", "Home")',
                    cache: false,
                    data: { myText: $("#myText").val() },
                    success: function (data) {
                    },
                    error: function (req) {
    
                    }
                });
    
                // we make sure to cancel the default action of the link
                // because we will be sending an AJAX call
                return false;
            });
        });
    </script>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, November 11, 2018 6:25 AM
  • User-1634604574 posted

    this is correct for this case

    i want to send value from view1 controller1  to view2 controller2 is this working for that?

    i mean i want when clicked on that button which is in view1 controller1 open view2 controller2 to get that input data value from view1

    Sunday, November 11, 2018 6:30 AM
  • User-369506445 posted

    as far as I understood, you want to send a parameter from view to your action and then in the action redirect to another action or controller

    if yes, you can not do it with ajax,but you can  go to another controller after get ajax result in success or error function

    <script type="text/javascript">
        $(function () {
            $('#btn').click(function (evt) {
                $.ajax({
                    type: 'GET',
                    url:'@Url.Action("MyAction", "Home")',
                    cache: false,
                    data: { myText: $("#myText").val() },
                    success: function (data) {
                        location.href = '@Url.Action("Index","Home2")' + "?myText=" + $("#myText").val()
                    },
                    error: function (req) {
    
                    }
                });
    
                // we make sure to cancel the default action of the link
                // because we will be sending an AJAX call
                return false;
            });
        });
    </script>

    and create another controller called Home2Controller

    public class Home2Controller : Controller
        {
            // GET: Home2
            public ActionResult Index(string myText)
            {
                ViewBag.myText = myText;
                return View();
            }
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, November 11, 2018 7:02 AM
  • User-1634604574 posted

    if i want to send two input value to view2 how can i do it?then how can i get those two values in view2

    Sunday, November 11, 2018 7:07 AM
  • User-369506445 posted

    also, you can use jquery below like

    in you index view

    <input type="text" name="myText" id="myText" />
    <a id="btn" href="@Url.Action("MyAction","Home")">Go to Home Index</a>
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script>
        $(document).ready(function () {
            $("#btn").click(function (event) {
                event.preventDefault();
                window.location.href = "@Url.Action("MyAction", "Home")" + "?myText=" + $("#myText").val();
            });
        });
    </script>

    in controller

    public ActionResult Index()
            {
                return View();
            }
            
            public ActionResult MyAction(string myText)
            {
                ViewBag.MyText = myText;
                return View("Details");
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, November 11, 2018 7:08 AM
  • User-369506445 posted

     in this sample, I created 3 input

    <input type="text" name="myText" id="myText1" />
    <input type="text" name="myText" id="myText2" />
    <input type="text" name="myText" id="myText3" />
    
    
    <a id="btn" href="@Url.Action("MyAction","Home")">Go to Home Index</a>
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script>
        $(document).ready(function () {
            $("#btn").click(function (event) {
                event.preventDefault();
                window.location.href = "@Url.Action("MyAction", "Home")" +
                    "?myText1=" + $("#myText1").val() +
                    "&myText2=" + $("#myText2").val() +
                    "&myText3=" + $("#myText3").val();
            });
        });
    </script>
    
    

    in Controller

    public ActionResult Index()
            {
                return View();
            }
            
            public ActionResult MyAction(string myText1, string myText2, string myText3)
            {
                string[] arr = new string[] { myText1, myText2, myText3 };
    
                ViewBag.MyText = arr;
                return View("Details");
            }

    and in Details View

    @foreach (var item in @ViewBag.MyText)
    {
        <p>@item</p>
    }
    
    
    or
    <br /> param 1 :<span>@ViewBag.MyText[0]</span> <br /> param 2 :<span>@ViewBag.MyText[1]</span> <br />

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, November 11, 2018 7:09 AM