ASP .NET MVC。模型和表格的问题
我正在尝试发布模型的一部分,以便在另一个控制器中查看操作。我尝试这样做:
<% using (Html.BeginForm("Register", "User", new {createDto = Model.UserCreateDto}, FormMethod.Post))
{%>
Form code here...
<%}%>
UserCreateDto 是我的模型的一部分,并且正在填写此表单。我试图将其传递给 UsersController 中的“Register”操作:
[HttpPost]
public ActionResult Register(UserCreateDto createDto)
问题是表单提交后,Register 操作中的路由值为 null (createDto)。也许发生这种情况是因为在表单提交事件之前(以及在填充模型字段之前)路由值被附加到 url。
有什么办法可以做我想做的事吗?
更新这是实际的 HTML 代码:
<form action="/User/Register" id="form1" method="post">
<input id="UserCreateDto_Username" name="UserCreateDto.Username" type="hidden" value="" />
<input id="UserCreateDto_Password" name="UserCreateDto.Password" type="hidden" value="" />
<input id="UserCreateDto_Email" name="UserCreateDto.Email" type="hidden" value="" />
<input id="UserCreateDto_Active" name="UserCreateDto.Active" type="hidden" value="" />
<span class="field-validation-valid" id="UserCreateDto_Username_validationMessage"></span><br /><label for="UserCreateDto_Username">Имя пользователя</label><br /><input id="UserCreateDto_Username" name="UserCreateDto.Username" type="text" value="" /><br />
<span class="field-validation-valid" id="UserCreateDto_Password_validationMessage"></span><br /><label for="UserCreateDto_Password">Пароль</label><br /><input id="UserCreateDto_Password" name="UserCreateDto.Password" type="text" value="" /><br />
<span class="field-validation-valid" id="UserCreateDto_Email_validationMessage"></span><br /><label for="UserCreateDto_Email">E-mail</label><br /><input id="UserCreateDto_Email" name="UserCreateDto.Email" type="text" value="" /><br />
<input type="submit" value="Register"/>
</form>
这是更新的表单代码:
<% using (Html.BeginForm("Register", "User", FormMethod.Post))
{%>
<%: Html.HiddenFor(userCreateDto => Model.UserCreateDto.Username) %>
<%: Html.HiddenFor(userCreateDto => Model.UserCreateDto.Password) %>
<%: Html.HiddenFor(userCreateDto => Model.UserCreateDto.Email) %>
<%: Html.HiddenFor(userCreateDto => Model.UserCreateDto.Active) %>
<%= Html.LabeledValidatedTextBoxFor(username => Model.UserCreateDto.Username, true) %>
<%= Html.LabeledValidatedTextBoxFor(password => Model.UserCreateDto.Password, true)%>
<%= Html.LabeledValidatedTextBoxFor(email => Model.UserCreateDto.Email, true)%>
<input type="submit" value="Register"/>
<%}%>
Html.LabeledValidatedTextBoxFor
只是我的自定义 HTML 助手。
这是更新的操作签名:
[HttpPost]
public ActionResult Register(UserCreateDto userCreateDto)
此操作中的 userСreateDto 仍然为空。顺便问一下,有什么办法不使用隐藏字段吗?它们不够安全,无法传递用户注册信息。
I am trying to post a part of my model for a view to action in another controller. I've tried to do it this way:
<% using (Html.BeginForm("Register", "User", new {createDto = Model.UserCreateDto}, FormMethod.Post))
{%>
Form code here...
<%}%>
UserCreateDto is a part of my model and it's being filled in this form. I'm trying to pass it to "Register" action in UsersController:
[HttpPost]
public ActionResult Register(UserCreateDto createDto)
The problem is that route value in Register action is null (createDto) after form submit. Maybe it is happening because route values are being attached to url before form submit event (and before model fields are filled).
Is there any way to do what I want?
UPDATE Here is the actual HTML code:
<form action="/User/Register" id="form1" method="post">
<input id="UserCreateDto_Username" name="UserCreateDto.Username" type="hidden" value="" />
<input id="UserCreateDto_Password" name="UserCreateDto.Password" type="hidden" value="" />
<input id="UserCreateDto_Email" name="UserCreateDto.Email" type="hidden" value="" />
<input id="UserCreateDto_Active" name="UserCreateDto.Active" type="hidden" value="" />
<span class="field-validation-valid" id="UserCreateDto_Username_validationMessage"></span><br /><label for="UserCreateDto_Username">Имя пользователя</label><br /><input id="UserCreateDto_Username" name="UserCreateDto.Username" type="text" value="" /><br />
<span class="field-validation-valid" id="UserCreateDto_Password_validationMessage"></span><br /><label for="UserCreateDto_Password">Пароль</label><br /><input id="UserCreateDto_Password" name="UserCreateDto.Password" type="text" value="" /><br />
<span class="field-validation-valid" id="UserCreateDto_Email_validationMessage"></span><br /><label for="UserCreateDto_Email">E-mail</label><br /><input id="UserCreateDto_Email" name="UserCreateDto.Email" type="text" value="" /><br />
<input type="submit" value="Register"/>
</form>
And here is the updated form code:
<% using (Html.BeginForm("Register", "User", FormMethod.Post))
{%>
<%: Html.HiddenFor(userCreateDto => Model.UserCreateDto.Username) %>
<%: Html.HiddenFor(userCreateDto => Model.UserCreateDto.Password) %>
<%: Html.HiddenFor(userCreateDto => Model.UserCreateDto.Email) %>
<%: Html.HiddenFor(userCreateDto => Model.UserCreateDto.Active) %>
<%= Html.LabeledValidatedTextBoxFor(username => Model.UserCreateDto.Username, true) %>
<%= Html.LabeledValidatedTextBoxFor(password => Model.UserCreateDto.Password, true)%>
<%= Html.LabeledValidatedTextBoxFor(email => Model.UserCreateDto.Email, true)%>
<input type="submit" value="Register"/>
<%}%>
Html.LabeledValidatedTextBoxFor
is just my custon HTML helper.
Here is updated action signature:
[HttpPost]
public ActionResult Register(UserCreateDto userCreateDto)
userСreateDto in this action is still null. By the way, is there any way not to use hidden fields? They are not secure enough to pass through them user registration information.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设 UserCreateDto 是一个类,这将不起作用,因为它将序列化为表单操作 url 中的字符串。您想要做的可能是使用隐藏字段来填充表单本身中的 UserCreateDto 字段。这将允许在表单回发时在表单参数中设置模型的属性,从而允许模型绑定器完成其工作并在操作方法参数中重构对象。
在服务器端,它应该如下所示:
[编辑]
或者,您可以将 UserCreateDto 存储在用户的会话中并在发布时检索它,因为您不信任将数据放入隐藏字段中。
Assuming that UserCreateDto is a class, this won't work as it will serialize it to a string in the form action url. What you want to do, probably, is use hidden fields to populate the UserCreateDto fields in the form itself. This will allow the properties of the model to set in the form parameters when the form is posted back, allowing the model binder to do its work and reconstitute the object in the action method parameters.
On the server side it should look like this:
[Edit]
Alternatively, you could just store the UserCreateDto in the user's session and retrieve it on post since you don't trust putting the data in a hidden field.
我决定使用分部视图,以便所有用户注册逻辑现在都封装在用户控制器中。我还开始使用 Ajax 表单(在 JQuery 的帮助下)。
I decided to use partial views so that all user registration logic now is incapsulated in Users controller. Also I've started to use Ajax forms (with the help of JQuery).