asp.net-mvc MVC Razor - 如何向自身提交表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9888066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:49:55  来源:igfitidea点击:

MVC Razor - How to submit form to itself

asp.net-mvcasp.net-mvc-3razor

提问by higgsy

I'm using MVC and Razor for the first time and have what is hopefully a really easy question. I have two pages on the site. The first page (Page A) has a very small form on it which has an input for an email address. when the user enters their email address and clicks submit they are sent to page B using HttpPost, like so:

我是第一次使用 MVC 和 Razor,希望能提出一个非常简单的问题。我在网站上有两个页面。第一页(页面 A)上有一个非常小的表单,其中包含电子邮件地址的输入。当用户输入他们的电子邮件地址并点击提交时,他们将使用 HttpPost 发送到页面 B,如下所示:

@using (Html.BeginForm("NewsletterSignup","Common", FormMethod.Post))
{
    <p>
        <input type="text" class="text" value="Enter your email address" id="email" name="email" />
        <input type="submit" class="submit" value="Sign Up" />
    </p>
}

On Page B there is the main form which allows the user to also enter their name and mobile number. The controller for the main form looks like so:

在页面 B 上有一个主表单,允许用户输入他们的姓名和手机号码。主窗体的控制器如下所示:

//newsletter signup page
public ActionResult NewsletterSignup()
{
    var model = new NewsletterSignupModel();

    return View(model);
}

[HttpPost, ActionName("NewsletterSignup")]
public ActionResult NewsletterSignupSend(NewsletterSignupModel model)
{
    if (ModelState.IsValid)
    {
        //register the user here
    }

    return View(model);
}

On the main form I have a Validationsummary and validation for each of the fields. The problem is that my contoller states that the NewsletterSignupSend action can only be executed using HttpPost. Because the form on Page A uses HttpPost when the user arrives on Page B the validation has already been run - i.e. before the user has submitted the form on Page b.

在主表单上,我对每个字段都有一个 Validationsummary 和验证。问题是我的控制器声明 NewsletterSignupSend 操作只能使用 HttpPost 执行。因为当用户到达页面 B 时,页面 A 上的表单使用 HttpPost 验证已经运行 - 即在用户提交页面 b 上的表单之前。

I know I'm missing a basic here - can someone steer me in the right direction?

我知道我在这里缺少一个基本知识 - 有人可以引导我走向正确的方向吗?

Thanks in advance Al

提前致谢

UPDATE: To resolve this question I have done the following.

更新:为了解决这个问题,我做了以下工作。

Form A is rendered using:

表单 A 使用以下方式呈现:

@Html.Action("MiniNewsletterSignup")

Form A has a controller method:

Form A 有一个控制器方法:

//mini newsletter view
        public ActionResult MiniNewsletterSignup()
        {
            var model = new MiniNewsletterSignupModel();

            return View(model);
        }

And the content of the view is:

视图的内容是:

@model Nop.Web.Models.Common.MiniNewsletterSignupModel
@using (Html.BeginForm("NewsletterSignup", "Common"))
{
    <p>
        <input type="text" class="text" value="Enter your email address" id="email" name="email" />
        <input type="submit" class="submit" value="Sign Up" />
    </p>
}

This submits the form using HttpPost off to Page B.

这使用 HttpPost off 将表单提交到页面 B。

Page B has 2 controller methods:

页面 B 有 2 个控制器方法:

//newsletter signup page
public ActionResult NewsletterSignup()
{
    var model = new NewsletterSignupModel();

    if (Request["email"] != null)
        model.Email = Request["email"];

    return View(model);
}

And:

和:

[HttpPost, **WhenRequestContainsKey("FullName")**]
public ActionResult NewsletterSignup(NewsletterSignupModel model)
{
    if (ModelState.IsValid)
    {
        //process here
    }

    return View(model);
}

You will notice I have added the Selector WhenRequestContainsKey which I found at http://softwaredevelopmentsolutions.blogspot.co.uk/2011/06/aspnet-mvc-3-partial-form-validation-on.html. This means this code is only called when there is a field in the Request called FullName, which on our site only exists on Page B.

您会注意到我添加了 Selector WhenRequestContainsKey,我在http://softwaredevelopmentsolutions.blogspot.co.uk/2011/06/aspnet-mvc-3-partial-form-validation-on.html找到了它。这意味着只有当请求中有一个名为 FullName 的字段时才会调用此代码,该字段在我们的站点上仅存在于页面 B 上。

This seems to work as I wanted it, but Im not sure why - why for example does this stop the validation taking place until the form is posted back on Page B - there is nothing in the method that calls a validate method???

这似乎像我想要的那样工作,但我不知道为什么 - 例如为什么这会停止验证,直到表单被发回页面 B - 调用验证方法的方法中没有任何内容???

Is there anything wrong with the way Ive implemented this?

我实现这个的方式有什么问题吗?

Thanks Al

谢谢艾尔

回答by Paddy

I think you are missing a post action - if you try to follow the pattern GET-POST-Redirect you should avoid these issues (i.e. every post should redirect to a GET action):

我认为您缺少一个 post 操作 - 如果您尝试遵循 GET-POST-Redirect 模式,您应该避免这些问题(即每个帖子都应该重定向到一个 GET 操作):

FirstAction()
{
    return View();
}

[HttpPost]
FirstAction()
{
    //Save email address
    return Redirect("NewsletterSignup","Common");
}

And your first form becomes:

你的第一个表格变成:

@using (Html.BeginForm())
{
    <p>
        <input type="text" class="text" value="Enter your email address" id="email" name="email" />
        <input type="submit" class="submit" value="Sign Up" />
    </p>
}

回答by Aliostad

Put a [HttpGet]on the first one:

将 a[HttpGet]放在第一个上:

[HttpGet]
public ActionResult NewsletterSignup()
{  
    var model = new NewsletterSignupModel();
    return View(model);
}