سؤال

I am building a ASP.NET MVC 2 application and have a controller that contains the following actions :

public ActionResult Edit()
{
  ...
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(EditUser user)
{
  ...
}

To this I got a stronge typed view that looks like this :

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/MasterPages/DefaultMasterPage.Master" Inherits="System.Web.Mvc.ViewPage<MyApp.Views.ViewClasses.EditUser>" %>
<%@ Import Namespace="MyApp.Views.Helpers" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
 <% Html.EnableClientValidation(); %>
  <% using (Html.BeginForm("Edit", "Account", FormMethod.Post, new { enctype = "multipart/form-data", @id = "edit_account" }))
   {%>
     <%: Html.LabelFor(model => model.User.UserEmail, false) %>
     <%: Html.TextBoxFor(model => model.User.UserEmail, new { @class = "tb1" })%>
     ...
<% } %>
</asp:Content>

When hitting the submit button(not shown in code) the public ActionResult Edit(EditUser user) action will be hit but the user object will not contain any data?

This is how part of the html look like :

<div class="controlTitleContainer"><div class="text"><label for="User_UserEmail">Mailadress</label></div></div>
<input type="text" value="" name="User.UserEmail" id="User_UserEmail" class="tb1">

This should mean that the input is pointed to the correct property.

Worth mentioning is that I use Data annotation to validate the object sent to the action but the model is always valid even when having a couple of requre fields.

Any idé why this is happening?

هل كانت مفيدة؟

المحلول

You haven't shown your model but make sure that it contains public properties with getters and setters and default parameterless constructors. Example:

public class EditUser
{
    public UserModel User { get; set; }
}

public class UserModel
{
    [Required]
    public string UserEmail { get; set; }
}

Finally try renaming the action argument to something else other than user (just to test):

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(EditUser u)
{
    ...
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top