modello che ho il seguente modello:vincolante con jquery ajax serialize non funziona
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; }
...
}
E la seguente vista:
@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>
...
e la mia 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.');
}
});
questo è il risultato della serializzazione:
RegisterUseraccount.FirstName=Peter&RegisterUseraccount.LastName=Miller&RegisterUseraccount.Email=miller%40gmail.com&RegisterUseraccount.Password=admin
Questo è il mio metodo di controllo che sto cercando di posta a:
[HttpPost]
public ActionResult Register(RegisterUseraccount registerUseraccount)
{
...
}
... ma i dati non arriva al metodo, ottengo un errore 404. Penso che la modelbinder puo' lavoro.
Ciò che funziona sono i dati che vengono inviati con il nome firstName = Peter, ma ciò che viene effettivamente inviato è RegisterUseraccount.FirstName = Peter.
Come posso gestire questo problema?
Sei sicuro la 'url' è corretta? – Zabavsky
Dove stai chiamando il metodo 'sendForm'? – Shyju
remove 'contentType: 'application/json',' –