views:

116

answers:

2

Hi,

I am new to MVC, and trying something and got stuck somewhere in between.

I have a user control there I have three textbox html type(ID, Lastname, firstname) and a submit buttom.

I set the button like

<input type="button" value="Search" 
                onclick="location.href='<%= Url.Action("action", "controller") %>'" /> 

I have called this usercontrol on some view through

<%= Html.Partial("ucName") %>

Now on pressing that button(on user control) I need to pass the data from these textboxes to controller again to some specific action(Http Post action). By using this data I want to do some database interaction and storing the result in a dataset and pass this data set to same view again to show up in some Grid.

I know the first part in conventional Asp.net can be done by raising the event through delegate but don't know how to do that in MVC.

A: 

In your actionresult you should have;

public ActionResult(MyModel model)
{
  //now do something with model
}

Get rid of the button and use a submit button instead and then use BeginForm so that it will submit to the same action and controller.

MyModel should contain the three fields and your model should inherit from MyModel as well so that MVC knows where to get the data from.

Have you gone through the NerdDinner sample yet because it sounds like you haven't. you need to do that first to get an appreciation of how MVC models and view work.

griegs
A: 

Can't you simply use a submit button that will post data to the desired controller action:

<% using (Html.BeginForm("action", "controller", FormMethod.Post)) { %>
    <!-- ...  your textboxes here ... -->
    <input type="submit" value="Search" />
<% } %>

You could also use FormMethod.Get if you want the parameters to be passed in the url.

Darin Dimitrov
I have put the same code in my usercontrol but on submit it is not calling the controller, may be because it is a user control but that I want. I want to put the textboxes and button in one usercontrol.
Nits