0

I have a form

<script src="@Url.Content("~/Scripts/jquery-1.8.2.min.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>

@using (Html.BeginForm("Index", "Company",IsPost))
{
    <div class="input-block-level">@Html.TextBoxFor(m => m.SearchString) 
                                   @Ajax.ActionLink("Submit", "Index", new{@class="btn"},new AjaxOptions {
                                                                                                HttpMethod = "Post",
                                                                                                UpdateTargetId = "partialDiv"
                                                                                             }) 
    </div>


}

<div id="partialDiv"></div>

When the link that is created via Ajax.Actionlink in browser is clicked, I don't get the value of the textbox in controllers post method. Why is that? Also, should I use Ajax.BeginForm? I haven't use Ajax.BeginForm yet, any tips on how could this Html.BeginForm be transformed to Ajax.BeginForm?

Cybercop
  • 8,475
  • 21
  • 75
  • 135

2 Answers2

0

Instead of Html.BeginForm you should use Ajax.BeginForm, and change the submit to the normal submit input: <input type="submit" ....

I suggest to take a look at this question: Using Ajax.BeginForm with ASP.NET MVC 3 Razor

Community
  • 1
  • 1
Marthijn
  • 3,292
  • 2
  • 31
  • 48
0

Have you tried making the request in jQuery?

<div class="container"> 
    @Html.TextBoxFor(m => m.Input, new { @id = "myInput"})
    <input type="submit" value="Submit" class="submit"/>
</div>

@section scripts{
 <script type="text/javascript">
    $('.submit').on('click', function() {
        var searchValue = $('#myInput').val();
        $.ajax({
            type: "POST",
            dataType: 'json',
            url: "/Home/MyMethod",
            data: {data: searchValue},
            success: function(data) {
                console.log();
            }
        });
    });
</script>
}

//Controller

[HttpPost]
public ActionResult MyMethod(string data)
{
   //do something
}
CinnamonBun
  • 1,150
  • 14
  • 28