0

I have an input and a button. input's value supposed to be passed to @url.Action like description in code below:

<input class="in-class" id="textbox" type="text" runat="server" />
<button class="btn-class" id="press" type="button" onclick="location.href='@Url.Action("Index", "Home", new {id = /*Value Of textbox*/ })'" >click here</button>

As I mentioned in code, /*Value Of textbox*/ should be input's current value.

3
  • 2
    use a form for this. Commented Jul 25, 2018 at 18:39
  • you can't. you need to write javascript for this Commented Jul 25, 2018 at 18:49
  • Can you post the code for the controller action? Commented Jul 25, 2018 at 19:44

3 Answers 3

3

I use this form

<input type="text" id="txtValue" /> <input type="button" value="Detail" onclick="location.href='@Url.Action("Action", "Home")?Value=' + $('#txtValue').val()" />

or you cant write this in jquery function.

Sign up to request clarification or add additional context in comments.

1 Comment

this should work but i personally would recommend breaking out into a function as well.
2

Change the href value with jQuery or JavaScript like this:

     <button class="btn-class" id="press" type="button" onclick="changeHref()" >click here</button>

   function changeHref(){
         var url = '@Url.Action("Index", "Home")';
         var txtVal = $('#textbox').val();
         window.location.href = url + '/?txtVal=' + txtVal;
   }

7 Comments

Forgot the query string. You'll have to add ?txtVal= after the '/'. I updated the code
Is it hitting the Index method inside the Home controller? Put a breakpoint inside the method to see if you even go back to the server. If not, then it's the url that's being set.
they both wont hit breakpoint, in the first place ('/') it opens just txtVal as a link, but when I Use '/?txtVal=' nothing happens.
Try, window.location.href = url + '?id=' + txtVal;
use post or get instead of window.location then: $.post(url, {txtVal: txtVal});
|
1

I preferred using jQuery click handler since you have button ID and following standard event registration model to separate HTML & JS:

HTML

<input class="in-class" id="textbox" type="text" />
<button class="btn-class" id="press" type="button">click here</button>

JS

$('#press').click(function () {
   var url = '@Url.Action("Index", "Home")';
   var textValue = $('#textbox').val();

   window.location.href = url + '?id=' + textValue;
});

PS: No need to use runat="server" attribute in MVC since Razor doesn't require it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.