mishap
mishap

Reputation: 8525

Calling controller from view, passing input text fields

I have two text inputs, content of which needs to be passed to a certain action as parameters when a button is clicked. I'm using MVC3 View:

<input name="input2" type="text"  class="inputfield" id="datepicker_1" /></td>
<input name="input2" type="text"  class="inputfield" id="datepicker_2" /></td>

@Html.Action("Search", ...)

Controller: public ActionResult Search(...) I suppose the object routeValues or RouteValueDictionary should be used in the @Html.Action for this. These object are confusing for me a bit. Could anyone clarify this for me please. Thank you!

Upvotes: 1

Views: 2711

Answers (2)

Christian Horsdal
Christian Horsdal

Reputation: 4932

It seems like you need have a submit on your view, and then implement a post action on your controller, to handle the submit. I.e. something along the lines of

 <input type="submit" value="Search..." />

in the view, and

[HttpPost]
public ActionResult Search(FormCollection collection)

the parameters depend on whether your view is strongly typed. The above assumes its not.

Upvotes: 0

AJC
AJC

Reputation: 1893

The Html.Action will probably generate the link html before you provide the inputs. You need to either place your inputs inside a form to be submited to your action, or use ajax, with jquery perhaps, to call the action, like so:

@using (Html.BeginForm("Search", "Controller", FormMethod.Post, new { id = "frmAction" }))
{
    <input name="datepicker_1" type="text"  class="inputfield" id="datepicker_1" /></td>
    <input name="datepicker_2" type="text"  class="inputfield" id="datepicker_2" /></td>
}


[HttpPost]
public ActionResult Search(Datetime datepicker_1, Datetime datepicker_2) {...}

For an Ajax example, check this question:

jquery ajax forms for ASP.NET MVC 3

Hope this helps...

Upvotes: 2

Related Questions