1

I have a form in my view page.it contains 5 text boxes,one search button.while the user enters values in textbox(Entering all fields are not mandatory)and click on the search button,the values I have to store it in an array and pass it to the controller and depending upon the search results i have to display the results of those searched records.

I am able to store the searched values in an array,now i want how to pass this array to the controller and how to access these values in the controller.

2
  • Point the form action into one action of your controller. Commented Aug 3, 2012 at 10:54
  • Thanks for the reply,how to pass these array of values from view to controller Commented Aug 3, 2012 at 10:56

2 Answers 2

1

as Jose referred , your request may look like this :

 $("#submit").click(function () {
        var searchData = new Array();
        $(".search-input").each(function () {
            searchData.push($(this).attr('value'));
        });

        $.ajax({
            type: "POST",
            url: "/Home/Index",
            data: {"searchData"  : searchData},
            success: function (data) {
                // do something on success
            },
            traditional: true,
            dataType: "json"
        });
        return false;
    });

and your controller action could be :

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Index([Bind(Prefix="searchData")] List<string> searchData)
    {
        return Index();
    }

and your form have to have markup like this:

<form id="myform">
       <input type="text" class='search-input' />
       <input type="text" class='search-input' />
       <input type="text" class='search-input' />
       <input type="text" class='search-input' />
       <input type="submit" id="submit" />
</form>
Sign up to request clarification or add additional context in comments.

1 Comment

I have implemented in this way ,I used foreach loop in my controller for entered value is null or not.when i enter one value in the text box and click on the search button ,at the foreach button it is throwing NullReference Exception.
0

Use ajax. If jQuery is an option you could write something like this

$(form).submit(function()
{
    var ;
    $.ajax({
        type: "POST",
        url: "/Controller/Action",
        data: JSON.stringify(_yourArrayObject),
        success: function(data){
            alert(data.Result);
        },
        dataType: "json"
    });
})

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.