部分视图在 URL 中显示其操作,而不是容器视图操作

发布于 2024-10-05 01:26:35 字数 2846 浏览 0 评论 0原文

我有一个包含表单的部分视图,并且该部分视图存在于包含其他一些表单和 html 的视图中。
当我按下提交并且验证失败时,它会在 URL 中显示此部分视图表单操作,而不是原始 URL。

家长查看“用户帐户”: - 登录部分视图 - 注册部分视图

打开页面时原始URL为:/users/account
注册验证失败时的 URL 变为: /users/register

这是我的部分视图:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<PicGhost.Website.ViewModels.RegisterViewModel>" %>

    <% using (Html.BeginForm("Register", "Users", FormMethod.Post)) {%>
        <%: Html.ValidationSummary(true) %>

        <fieldset>
            <legend>Fields</legend>

            <div class="editor-label">
                <%: Html.LabelFor(model => model.UserName) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.UserName) %>
                <%: Html.ValidationMessageFor(model => model.UserName) %>
            </div>

            <div class="editor-label">
                <%: Html.LabelFor(model => model.Email) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.Email) %>
                <%: Html.ValidationMessageFor(model => model.Email) %>
            </div>

            <div class="editor-label">
                <%: Html.LabelFor(model => model.Password) %>
            </div>
            <div class="editor-field">
                <%: Html.PasswordFor(model => model.Password)%>
                <%: Html.ValidationMessageFor(model => model.Password) %>
            </div>

            <div class="editor-label">
                <%: Html.LabelFor(model => model.ConfirmPassword) %>
            </div>
            <div class="editor-field">
                <%: Html.PasswordFor(model => model.ConfirmPassword) %>
                <%: Html.ValidationMessageFor(model => model.ConfirmPassword) %>
            </div>

            <p>
                <input type="submit" value="Register" />
            </p>
        </fieldset>

    <% } %>

和注册操作:

        [HttpPost]
        public ActionResult Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                IUser user = _factory.CreateUser(model.UserName, model.Email, model.Password);
                UserRepository.Add(user);
                return RedirectToAction("Index");
            }
            return View(model);
        }

如何避免显示此错误的 URl 并保留原始 URL?

原文网址:
alt text

验证后 URL:
替代文本

I have a partial view which contains a form, and this partial view exists in a view which contain some other forms and html.
When I press submit and the validation fails, it show this partial view form action in the URL instead of the original URL.

Parent View "User Account":
- Login partial view
- Register partial view

Original URL when the page open is: / users/account
URL when register validation fail become: /users/register

Here is my partial view:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<PicGhost.Website.ViewModels.RegisterViewModel>" %>

    <% using (Html.BeginForm("Register", "Users", FormMethod.Post)) {%>
        <%: Html.ValidationSummary(true) %>

        <fieldset>
            <legend>Fields</legend>

            <div class="editor-label">
                <%: Html.LabelFor(model => model.UserName) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.UserName) %>
                <%: Html.ValidationMessageFor(model => model.UserName) %>
            </div>

            <div class="editor-label">
                <%: Html.LabelFor(model => model.Email) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.Email) %>
                <%: Html.ValidationMessageFor(model => model.Email) %>
            </div>

            <div class="editor-label">
                <%: Html.LabelFor(model => model.Password) %>
            </div>
            <div class="editor-field">
                <%: Html.PasswordFor(model => model.Password)%>
                <%: Html.ValidationMessageFor(model => model.Password) %>
            </div>

            <div class="editor-label">
                <%: Html.LabelFor(model => model.ConfirmPassword) %>
            </div>
            <div class="editor-field">
                <%: Html.PasswordFor(model => model.ConfirmPassword) %>
                <%: Html.ValidationMessageFor(model => model.ConfirmPassword) %>
            </div>

            <p>
                <input type="submit" value="Register" />
            </p>
        </fieldset>

    <% } %>

And register action:

        [HttpPost]
        public ActionResult Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                IUser user = _factory.CreateUser(model.UserName, model.Email, model.Password);
                UserRepository.Add(user);
                return RedirectToAction("Index");
            }
            return View(model);
        }

How to avoid showing this wrong URl and keep the original URL?

Original URL:
alt text

After validation URL:
alt text

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

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

发布评论

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

评论(2

零度° 2024-10-12 01:26:35

您发布到/users/register,这就是返回响应的内容。

为了避免这种情况:

  1. 发布到创建的操作
    页面 - 也许不可能,如果你
    有多种形式
  2. 或者而不是
    返回视图返回a
    redirecttoaction 到原始操作

如果您重定向到某个操作,但您需要处理模型数据(可能来自多个子操作) - 您可以将数据传递回 TempData 集合中以进行重定向。

You post to /users/register and that is what is returning the response.

To avoid this either:

  1. post to the action that created the
    page - perhaps not possible if you
    have several forms
  2. or rather than
    returning the view return a
    redirecttoaction to the original action

If you redirect to an action though you will need to handle the model data, perhaps from several child actions - you can pass the data back in the TempData collection for the redirect.

春庭雪 2024-10-12 01:26:35

您需要返回包含视图。发生的情况是,由于 modelstate 无效,它仅返回部分视图。这意味着,您必须以某种方式将部分视图的模型状态恢复到部分视图中。我通过让我的父视图拥有一个包含子模型的模型来完成此操作。

像这样

public class ParentViewModel
{
    public RegisterViewModel RegisterModel { get; set; }
}

然后在帐户视图中

<% Html.RenderPartial("Register", Model.RegisterModel); %>

然后您可以构建 ParentViewModel 并转储到 RegisterViewModel 中,然后在 ModelState 无效时返回帐户视图。在 Register 操作中,您会看到类似这样的内容。

if (ModelState.IsValid)
{
    ...
}

var parentModel = new ParentViewModel()
{
    RegisterModel = model;
};

return View("Account", parentModel);

只需确保始终初始化 RegisterModel,否则会出现错误。

You need to return the containing view. What is happening is that because modelstate is invalid, it is returning only the partial view. This means that somehow, you will have to get the modelstate of your partial view back into the partial view. I did this once by having my parent view have a model that contained the child models.

Something like this

public class ParentViewModel
{
    public RegisterViewModel RegisterModel { get; set; }
}

Then in the Account view

<% Html.RenderPartial("Register", Model.RegisterModel); %>

Then you can build the ParentViewModel and dump in the RegisterViewModel and then return the Account view when ModelState is invalid. Inside the Register action you'd have something like this.

if (ModelState.IsValid)
{
    ...
}

var parentModel = new ParentViewModel()
{
    RegisterModel = model;
};

return View("Account", parentModel);

Just make sure you always init RegisterModel or you'll get an error.

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