Passing html textbox value as a parameter to controllers method in asp.net MVC

前端 未结 3 1730
再見小時候
再見小時候 2021-01-26 12:12

I want to pass the Html.Textbox value to a controller from anchor tag, so that I can search the value passed to a controller. Please tell me how can I achieve this.

相关标签:
3条回答
  • 2021-01-26 12:51
    @using (Html.BeginForm("Index", "Home", null, FormMethod.Post, new {@id = "my-form"}))
    {
        @Html.TextBox("String")
    }
    
    <a href="javascript:document.getElementById('my-form').submit();>@p</a>
    

    You can use FormMethod.Post or FormMethod.Get. The latter will append ?String= to the url.

    0 讨论(0)
  • 2021-01-26 12:57

    You don't have to use jQuery. If you're doing a HttpPost, you just need the "name" of the textbox.

    On your page:

    @using (Html.BeginForm("Index", FormMethod.Post)) {
        @Html.TextBox(string.Empty, new { name = "textbox" })
    
        <input type="submit">Submit</input>
    }
    

    Then in your controller:

    [HttpPost]
    public ActionResult Index(string textbox) {
        // The name of the string parameter must match the name given to the TextBox element on the page.
    }
    
    0 讨论(0)
  • 2021-01-26 13:02

    use jquery

    @Html.TextBox("String", null, new { @class="txtString" })
    <a  href="@Url.Action("Index", "Home", new {  })" class="linkAction">@p</a>
    

    then in your script

    $('.txtString').on('blur', function(){
        $('.linkAction').attr('src', '@Url.Action("Index", "Home", new { text = "----" })'.replace("----", $('.txtString').val()));
    });
    
    0 讨论(0)
提交回复
热议问题