Pass value from one View to another with QueryString using ActionLink in ASP.Net MVC

Last Reply 3 months ago By dharmendr

Posted 3 months ago

hello,

i have this view in mvc, on action click even i want to page @html.txt values to query string parameter and redirect user on Details View page please advice

@{
    ViewBag.Title = "Insert";
}

<h2>Insert</h2>
<h6>@Html.ActionLink("Go Back", "Index", "QueryString")</h6>
<h6>Insert New Records Here:</h6> 

@using (@Html.BeginForm("InsertNew", "QueryString", FormMethod.Get))
{
    <table>
        <tr>
            <td>Enter Your Name</td>
            <td>:</td>
            <td>@Html.TextBox("TxtName")</td>
        </tr>
        <tr>
            <td>Enter Your Mobile No</td>
            <td>:</td>
            <td>@Html.TextBox("TxtMobile")</td>
        </tr>
        <tr>
            <td>Enter Your EmailID</td>
            <td>:</td>
            <td>@Html.TextBox("TxtEmailID")</td>
        </tr>
        <tr>
            <td>Enter Your Address</td>
            <td>:</td>
            <td>@Html.TextBox("TxtAddress")</td>
        </tr>
        <tr>
            <td>Save Your Details</td>
            <td>:</td>
            @*<td><input type="submit" value="Submit" /></td>*@
        </tr>
    </table>
    
    
    @Html.ActionLink("Call Index Action", "Details", new {  Name = txtnamevalue, Mobile = moblietextboxvalue  })
}  

 

Posted 3 months ago

Hi nauna,

Check this example. Now please take its reference and correct your code.

Controller

Home

public class HomeController : Controller
{
    // GET: /Home/
    public ActionResult Index()
    {
        return View();
    }
}

Details

public class DetailsController : Controller
{
    // GET: /Details/
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Details(string name, string mobile)
    {
        TempData["Name"] = name;
        TempData["Mobile"] = mobile;
        return View();
    }
}

View

Home

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Index</title>
    <script type="text/javascript" src="https://code.jquery.com/jquery-2.2.3.js"></script>
    <script type="text/javascript">
        $(function () {
            $('.Redirect').on("click", function () {
                var name = $('#TxtName').val();
                var mobile = $('#TxtMobile').val();
                var url = $(this).attr('href') + '?Name=' + name + '&Mobile=' + mobile;
                location.href = url;
                return false;
            });
        });
    </script>
</head>
<body>
    <div>
        <%using (Html.BeginForm()) {%>
        <table>
            <tr>
                <td>Enter Your Name</td>
                <td>:</td>
                <td><%:Html.TextBox("TxtName")%></td>
            </tr>
            <tr>
                <td>Enter Your Mobile No</td>
                <td>:</td>
                <td><%:Html.TextBox("TxtMobile")%></td>
            </tr>
            <tr>
                <td>Enter Your EmailID</td>
                <td>:</td>
                <td><%:Html.TextBox("TxtEmailID")%></td>
            </tr>
            <tr>
                <td>Enter Your Address</td>
                <td>:</td>
                <td><%:Html.TextBox("TxtAddress")%></td>
            </tr>
        </table>
        <%:Html.ActionLink("Create", "Details", "Details", new { }, new { @class = "Redirect" })%>
        <%} %>
    </div>
</body>
</html>

Details

<body>
    Name : <%=TempData["Name"]%><br />
    Mobile : <%=TempData["Mobile"]%>
</body>

Screenshot