简体   繁体   中英

Pass a value of textbox from view to controller through hyperlink in MVC ASP.NET

In View-

<input type="text" hidden="hidden" id= "nameString" name="nameString" value="xyz" />
@Html.ActionLink("Save", "HomePage", "ControllerName", new { nameString = "/* Value from above input here.*/" })

In this case I want to pass "xyz" through this hyperlink.

Can't use Get or POST methods to pass this value.

This HomePage view is not the current ActionLink view either, so can't grab the value in Controller using Request.Form["nameString"];

I tried JQuery like following but its not working-

var nameVar = document.getElementById('nameString').value;
$.ajax({
                type: 'GET',
                url: "@Url.Action("HomePage", "ControllerName")",
                data: { nameString : nameVar }                   
            });

I checked in debugger and saw that Controller is actually getting value and processing it but nothing is coming on browser. I am not sure how ajax works.

since the redirect will happen in jquery you don't need a helper

<input type="button" class="btnRedirect" value="Click Here" />

then in your script

$('.btnRedirect').on('click', function(){
    var url = '@Url.Action("HomePage", "ControllerName", new { textValue = "----" })'.replace("----", $('#nameString').val());
    window.location = url;
});

This is how it would be done with ajax although this will not handle returning a page.

var name = $("#nameString").value;
$.get("/ControllerName/HomePage",{ nameString : nameVar })
           .done(function(){
                 window.location.assign("/ControllerName/HomePage");
                 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM