2012-08-30 182 views
0

jQuery對話框中的數據究竟應該如何傳遞給Controller?我不確定要放什麼東西的數據(見Index.cshtml代碼)將數據從jquery對話框傳遞給asp.net mvc控制器

控制器

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

我應該要展現形式的對話框中創建一個用戶,如果這樣,只需要創建這個<div id="dialog" title="Create User" style="overflow: hidden;"></div>元素作爲內部表單正常,只要確保你使用javascript驗證,否則頁面將被刷新,如果用戶提交invalidad信息,對話框將會丟失。

+0

能否詳細說明'在此元素內部創建表單' – theStig

+0

對不起,我粘貼了一些代碼,但由於某種原因它沒有顯示出來,我現在正在編輯 – ryudice

+0

會不會有點多餘?我已經定義了我的模型,並且正在調用$(this).load('@ Url.Action(「CreateUser」)'); – theStig

相關問題