First we build a Controller that has an action to output our view and an action to accept posts
1: [HandleError]
2: public class HomeController : Controller
3: {
4: public ActionResult Index()
5: {
6: UserModel userModel = new UserModel(){UserId = 1,Type = UserTypes.User};
7:
8: return View(userModel);
9: }
10:
11: [AcceptVerbs(WebRequestMethods.Http.Post)]
12: public ActionResult UpdateUserType(UpdateUser input)
13: {
14: var result = new {Succss = true, Message = "Updated"};
15: return Json(result);
16: }
17: }
Here is a quick over view of the model objects we are going to be using
1: public class UserModel
2: {
3: public int UserId { get; set; }
4: public UserTypes Type { get; set; }
5: }
6:
7: public enum UserTypes
8: {
9: User = 1,
10: PowerUser = 2,
11: Admin = 3
12: }
13:
14: public class UpdateUser
15: {
16: public int UserId { get; set; }
17: public UserTypes Type { get; set; }
18: }
The view is a very simple and strongly typed for the UserModel, first build the select control
1: <select name="SelectUserType" onchange="UpdateUserType(this,<%=Model.UserId%>)">
2: <%foreach (UserTypes item in Enum.GetValues(typeof(UserTypes)).Cast<UserTypes>())
3: {
4: if(item == Model.Type)
5: {
6: %><option selected="selected" value="<%=((int)item).ToString() %>"><%=item.ToString() %></option><%
7: }
8: else
9: {
10: %><option value="<%=((int)item).ToString() %>"><%=item.ToString() %></option><%
11: }
12: } %>
13: </select>
Next we add the javascript function(fyi this requires Jquery)
1: <script type="text/javascript">
2: function UpdateUserType(sender, userId) {
3: var model = { UserId: userId, Type: sender.value };
4: $.post("/home/UpdateUserType",
5: model,
6: function (data) {
7:
8: alert("Was Successful: " + data.Succss);
9: alert("Message: " + data.Message);
10: });
11: }
12: </script>
A quick and easy way to add Ajax to any control and as always here is a link to the demo project http://bob-the-janitor-sample-code.googlecode.com/svn/trunk/ajaxMadeEasy/
No comments:
Post a Comment