ASP .NET MVC。模型和表格的问题

发布于 2024-11-04 06:59:21 字数 3145 浏览 0 评论 0原文

我正在尝试发布模型的一部分,以便在另一个控制器中查看操作。我尝试这样做:

    <% 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 技术交流群。

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

发布评论

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

评论(2

草莓酥 2024-11-11 06:59:21

假设 UserCreateDto 是一个类,这将不起作用,因为它将序列化为表单操作 url 中的字符串。您想要做的可能是使用隐藏字段来填充表单本身中的 UserCreateDto 字段。这将允许在表单回发时在表单参数中设置模型的属性,从而允许模型绑定器完成其工作并在操作方法参数中重构对象。

<% using (Html.BeginForm("Register", "User"))
{%>
      <%: Html.HiddenFor( model => model.UserCreateDto.UserID ) %>
      <%: Html.HiddenFor( model => model.UserCreateDto.Username ) %>
      ...
<%}%>

在服务器端,它应该如下所示:

[HttpPost]
public ActionResult Register(UserCreateDto 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.

<% using (Html.BeginForm("Register", "User"))
{%>
      <%: Html.HiddenFor( model => model.UserCreateDto.UserID ) %>
      <%: Html.HiddenFor( model => model.UserCreateDto.Username ) %>
      ...
<%}%>

On the server side it should look like this:

[HttpPost]
public ActionResult Register(UserCreateDto userCreateDto)

[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.

一影成城 2024-11-11 06:59:21

我决定使用分部视图,以便所有用户注册逻辑现在都封装在用户控制器中。我还开始使用 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).

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