我有以下型号:
public class RegisterUseraccount { [required] [DataType(DataType.EmailAddress)] [Display(Name = "E-Mail-Adresse")] public string Email { get; set; } [required] [Display(Name = "Vorname")] public string FirstName { get; set; } [required] [Display(Name = "Nachname")] public string LastName { get; set; } [required] [DataType(DataType.Password)] [MinLength(5)] [Display(Name = "Passwort")] public string Password { get; set; } ... }
以下观点:
@using (Html.BeginForm("Register","Useraccount",FormMethod.Post,new { id = "registerUseraccountForm",@class = "ym-form" })) { @Html.ValidationSummary(true) <div class="ym-grid"> <div class="ym-g50 ym-gl"> <div class="ym-fBox-text"> @Html.LabelForrequired(model => model.RegisterUseraccount.FirstName,null) @Html.EditorFor(model => model.RegisterUseraccount.FirstName,new { required = "required",name = "firstName" }) @Html.ValidationMessageFor(model => model.RegisterUseraccount.FirstName) </div> </div> ...
和我的JavaScript
function sendForm(target) { alert(data); $.ajax({ url: target,type: "POST",contentType: 'application/json',data: $("#registerUseraccountForm").serialize(),success: ajaxOnSuccess,error: function (jqXHR,exception) { alert('Error message.'); } });
这是序列化的结果:
RegisterUseraccount.FirstName=Peter&RegisterUseraccount.LastName=Miller&RegisterUseraccount.Email=miller%40gmail.com&RegisterUseraccount.Password=admin
这是我试图POST到的控制器方法:
[HttpPost] public ActionResult Register(RegisterUseraccount registerUseraccount) { ... }
…但是数据没有到达方法,我收到错误404.我认为模型绑定器无法工作.
什么有效是以名称firstName = Peter发送的数据,但实际发送的是RegisterUseraccount.FirstName = Peter.
我该如何处理这个问题?