默认活页夹的问题(可能很简单)

发布于 2024-11-04 06:48:21 字数 1543 浏览 5 评论 0原文

我正在构建一个 ASP.NET MVC 2 应用程序,并有一个包含以下操作的控制器:

public ActionResult Edit()
{
  ...
}

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

为此,我得到了一个强类型视图,如下所示:

<%@ 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>

当点击提交按钮(代码中未显示)时,公共 ActionResult Edit(EditUser user) 操作会被命中,但用户对象不会包含任何数据?

这就是 html 的一部分的样子:

<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">

这应该意味着输入指向正确的属性。

值得一提的是,我使用数据注释来验证发送到操作的对象,但即使有几个必需字段,模型也始终有效。

知道为什么会发生这种情况吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱情眠于流年 2024-11-11 06:48:21

您尚未显示您的模型,但请确保它包含具有 getter 和 setter 以及默认无参数构造函数的公共属性。示例:

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

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

最后尝试将操作参数重命名为 user 以外的其他名称(仅用于测试):

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

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)
{
    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文