【问题标题】:How to retain formcollection values on postback in MVC如何在 MVC 中的回发中保留 formcollection 值
【发布时间】:2018-09-20 13:44:59
【问题描述】:

这里我使用的是简单的应用程序,点击发送按钮我将执行索引 httppost 操作。但是由于某种原因,如果我的验证码不正确,那么我正在尝试加载相同的视图。我正在传递表单集合,所以我的名字和姓氏不会被抹去。请建议我如何坚持我的价值观。

[HttpPost]
        public ActionResult Index(FormCollection dataColl)
        {
            ColComments datgrp = new ColComments();
            datgrp.fname = dataColl[0].ToString();
            datgrp.lname = dataColl[1].ToString();

            if (!this.IsCaptchaValid(""))
            {

                ViewBag.Classname = "alert alert-warning";
                ViewBag.ErrorMessage = "Incorrect captcha answer.";
            }
            else
            {
                ViewBag.ErrorMessage = "OKAY";
                return RedirectToAction("Landing", "Account");
            }

            return View(dataColl);
        }

索引视图。

@using CaptchaMvc.HtmlHelpers
@model CaptchaTestApp.Models.ColComments
@{
    ViewBag.Title = "Home Page";
}

<div class="jumbotron">
    <h1>ASP.NET</h1>
    <p class="lead">ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS and JavaScript.</p>
    <p><a href="http://asp.net" class="btn btn-primary btn-lg">Learn more &raquo;</a></p>

</div>
<div>
    @using (Html.BeginForm())
    {
        <div> @Html.ValidationSummary(true)</div>
            <fieldset>
                <legend>ColComments</legend>

                <div class="editor-label">
                    @Html.LabelFor(model => model.fname)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.fname)
                    @Html.ValidationMessageFor(model => model.fname)
                </div>

                <div class="editor-label">
                    @Html.LabelFor(model => model.lname)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.lname)
                    @Html.ValidationMessageFor(model => model.lname)
                </div>
            </fieldset>
                <table>
                    <tr>
                        <td colspan="3">
                            @Html.Captcha(8, "_captchaCnt")
                        </td>
                    </tr>
                </table>
                <p>

                    <input type="submit" value="Send" />
                </p>
    }
</div>

【问题讨论】:

    标签: asp.net-mvc model-view-controller


    【解决方案1】:

    您需要返回您在 HttpGet 方法中发送的具有更新值的相同视图。您正在发送表单集合对象以查看。

    [HttpPost]
            public ActionResult Index(FormCollection dataColl)
            {
                ColComments datgrp = new ColComments();
                datgrp.fname = dataColl[0].ToString();
                datgrp.lname = dataColl[1].ToString();
    
                if (!this.IsCaptchaValid(""))
                {
    
                    ViewBag.Classname = "alert alert-warning";
                    ViewBag.ErrorMessage = "Incorrect captcha answer.";
                }
                else
                {
                    ViewBag.ErrorMessage = "OKAY";
                    return RedirectToAction("Index", "Home");
                }
    
                return View(datgrp);
            }
    

    【讨论】:

    • 我正在使用 captchaMVC 并且在 IsCaptchaValid("") 上我收到了从客户端错误中检测到潜在危险的 Request.Form 值。那是因为 lname 字段有 html 内容lname="&lt;p&gt;Test&lt;/p&gt;" 我该如何解决这个错误
    • 您有两种选择来解决这个问题 1. 在模型属性 lname 上添加 [AllowHtml] 属性。 2.控制器POST方法添加[ValidateInput(false)]
    • 我做到了[ValidateInput(false)],但我仍然收到A potentially dangerous Request.Form value was detected from the client errorIsCaptchaValid("")
    • 是进入 POST 方法还是在此之前抛出错误?
    • 它进入了 post 方法,但是在这一行抛出了错误if (!this.IsCaptchaValid(""))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-11
    • 2017-02-11
    • 2014-05-16
    • 1970-01-01
    • 1970-01-01
    • 2010-09-18
    • 2012-03-18
    相关资源
    最近更新 更多