默认活页夹的问题(可能很简单)
我正在构建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您尚未显示您的模型,但请确保它包含具有 getter 和 setter 以及默认无参数构造函数的公共属性。示例:
最后尝试将操作参数重命名为
user
以外的其他名称(仅用于测试):You haven't shown your model but make sure that it contains public properties with getters and setters and default parameterless constructors. Example:
Finally try renaming the action argument to something else other than
user
(just to test):