0
votes

I have created a texbox. When user give some input in the textbox and click the actionlink below, the value of the textbox will get pass to the actionResult(FWMenu) in the controller. I can not use html.begin form and submit button in the view. And i can not even use [httppost] in my controller. Is it possible in that way? If yes then please help me how. I have not used any class in model.

Below is my Controller.

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

        public ActionResult FWMenu(string username)
        {
            return View();
        }

    }

This is my View.

<div>
    @Html.TextBox("txtUserName")
    @Html.ActionLink("Login", "FWMenu", new { username = @Html.TextBox("txtUserName") })
</div>
1
Yes, you can send it through AJAX, but as you are sending sensitive data like username, think before using GET method.BabyDuck
No its not. You would need to use javascript/jquery to update the url of the link (append the value of the textbox). But why can't you use a form and post the value?user3559349
@StephenMuecke, This is just a prototype. In my project i can not use form and submit button every time in every page. So trying to implement it this way, if it gets successful then it will be a huge help for me.Amit
What make you think you cannot use a form and submit button?user3559349
@StephenMuecke, It is because, suppose i have ten buttons in a page. And on every button click i have to call an action. Then i have to make ten submit buttons. bUT I want to avoid that. Another thing suppose i am using an image as a button, then will it be possible to make that image as a submit button?Amit

1 Answers

0
votes

You need to use javascript/jquery to build the url and redirect. From your comments you mentioned you wanted to use a image rather than a button or link, and that you will have multiple items, so assuming you html is

<div>
  <input type="text" name="username">
  <img class="submit scr=....>
<div>

<script>
  var urlBase='@Url.Action("FWMenu");
  $('.submit').click(function() {
    var userName = $(this).prev('input').val();
    location.href = urlBase + '/' + userName;
  }
</script>

Side note: No real point using @Html.TextBox("txtUserName") and if you have multiple instance of this it would generate invalid html (duplicate id attributes) and in any case the name of the parameter is username so it would have needed to be @Html.TextBox("username")`