开发者

Passing Data from Usercontrol to controller

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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜