将数据从jquery对话框传递到asp.net mvc控制器

时间:2012-08-30 22:19:42

标签: jquery ajax asp.net-mvc

如何将来自jquery对话框的数据传递给Controller?我不确定要为数据添加什么(请参阅Index.cshtml代码)

控制器

public ActionResult CreateUser() {
        return PartialView("_Create", new RegisterModel());
    }

[HttpPost]
public ActionResult CreateUser(RegisterModel user) {
    //...
}

Index.cshtml

<script type="text/javascript">
$(document).ready(function () {
    $('#dialog').dialog({
        //...dialog information
        open: function(event, ui) {
            $(this).load('@Url.Action("CreateUser")');
        },
        buttons: {
            "Submit": function () {
                $.ajax({
                    url: 'Users/CreateUser',
                    type: 'POST',
                    data: /* What is passed here? */,
                });
            }
        }
    });

    $('#btnCreate').click(function (e) {
        e.preventDefault();
        $('#dialog').dialog('open');
    });

});
</script>

@Html.ActionLink("Create New User", "CreateUser", null, new { id= "btnCreate" })
<div id="dialog" title="Create User" style="overflow: hidden;"></div>

--- ---编辑 这是从开放功能中调用的模型

模型

public class RegisterModel {
    [Required]
    [Display(Name = "User name")]
    public string UserName { get; set; }

    [Required]
    [DataType(DataType.EmailAddress)]
    [Display(Name = "Email address")]
    public string Email { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }

    [Required]
    [Display(Name = "Roles")]
    public string RolesId { get; set; }
    public IEnumerable<SelectListItem> RolesItem {
        get { return new SelectList(Roles.GetAllRoles()); }
    }
}

部分视图

@model MvcApp.Models.RegisterModel

@{
    ViewBag.Title = "Register";
}

<h2>Create a New User</h2>
<p>
    Passwords are required to be a minimum of @Membership.MinRequiredPasswordLength characters in length.
</p>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

<div id="CreateModel"></div>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true, "Account creation was unsuccessful. Please correct the errors and try again.")
    <div>
        <fieldset>
            <table>
                <tr>
                    <td>@Html.LabelFor(m => m.UserName)</td>
                    <td>@Html.EditorFor(m => m.UserName)</td>
                    <td>@Html.ValidationMessageFor(m => m.UserName)</td>
                </tr>
                ... more fields... 

            </table>
        </fieldset>
    </div>
}

1 个答案:

答案 0 :(得分:1)

我想你要在对话框中显示用于创建用户的表单,如果是这样,只需在正常的<div id="dialog" title="Create User" style="overflow: hidden;"></div>元素内创建表单,只需确保使用javascript验证,否则页面将如果用户提交无效信息,则刷新并且对话框将丢失。