1

当我的页面以“编辑”模式加载时,我的文本字段会正确呈现,但我的数字字段会呈现错误验证文本,即使字段中的值是有效的:

带有错误文本的数字字段加载

我的问题出在一个更复杂的项目中,但我能够在一个开箱即用的 MVC 3 应用程序中重现它,我只是在其中添加了这些位。为什么页面加载时数字字段显示错误文本但文本字段正常?

这里发生了什么?

我的模型、控制器和视图具有以下内容:

模型:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace MvcIssues.Models
{
    public enum Operations
    {
        View = 0,
        Edit = 1
    }

    public class ShowsModel
    {
        public Operations Operation { get; set; }

        [Display(Name = "Name")]
        [DataType(DataType.Text)]
        [StringLength(10)]
        public string Name { get; set; }

        [Display(Name = "Number")]
        [Required]
        [Range(typeof(int), "1", "999")]
        public int Number { get; set; }
    }
}

控制器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcIssues.Models;
using MvcIssues.Data;

namespace MvcIssues.Controllers
{
    public class TestController : Controller
    {
        // GET: /Test/Shows
        [AcceptVerbs(HttpVerbs.Get)]
        [ActionName("Shows")]
        [Authorize]
        public ActionResult SelectedShows()
        {
            ShowsData shows = MvcApplication.Shows;

            ShowsModel model = new ShowsModel();

            model.Operation = Operations.View;
            model.Name = shows.Name;
            model.Number = shows.Number;

            return View(model);
        }


        // POST: /Test/Shows
        [AcceptVerbs(HttpVerbs.Post)]
        [ActionName("Shows")]
        [ValidateInput(false)]
        [Authorize]
        public ActionResult ShowsSubmit(ShowsModel data)
        {
            string name = data.Name;
            int number = data.Number;

            ShowsModel model = new ShowsModel();

            if (Request.Form.AllKeys.Contains("btnEdit"))
            {
                ShowsData shows = MvcApplication.Shows;
                model.Name = shows.Name;
                model.Number = shows.Number;

                model.Operation = Operations.Edit;
            }
            else if (Request.Form.AllKeys.Contains("btnCancel"))
            {
                ShowsData shows = MvcApplication.Shows;

                model.Name = shows.Name;
                model.Number = shows.Number;

                model.Operation = Operations.View;
            }
            else if (Request.Form.AllKeys.Contains("btnSaveEdit"))
            {
                ShowsData shows = MvcApplication.Shows;

                shows.Name = name;
                shows.Number = number;

                model.Name = shows.Name;
                model.Number = shows.Number;

                model.Operation = Operations.View;
            }

            return View("Shows", model);
        }
    }
}

看法:

@model MvcIssues.Models.ShowsModel

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

<h2>Test Page</h2>

<div>
    Show = @this.ViewData.Model.Name
    <br />
    Number = @this.ViewData.Model.Number.ToString()
</div>

<hr />

<div>
    @Html.ValidationSummary(true, "oops!")

    @using (Html.BeginForm())
    {
        <div>

            Name: @(this.ViewData.Model.Operation == MvcIssues.Models.Operations.View ?
                Html.TextBoxFor(m => m.Name, new { disabled = "disabled", maxLength = "20" })
            :
                Html.TextBoxFor(m => m.Name))
            @Html.ValidationMessageFor(m => m.Name)

            <br />

            Number: @(this.ViewData.Model.Operation == MvcIssues.Models.Operations.View ?
                Html.TextBoxFor(m => m.Number, new { disabled = "disabled" })
            :
                Html.TextBoxFor(m => m.Number))
            @Html.ValidationMessageFor(m => m.Number)

        </div>

        <div>
            @switch (this.ViewData.Model.Operation)
            {
                case MvcIssues.Models.Operations.Edit:
                    <input type="submit" name="btnSaveEdit" value="Save" />
                    <input type="submit" name="btnCancel" value="Cancel" />
                    break;
                case MvcIssues.Models.Operations.View:
                default:
                    <input type="submit" name="btnEdit" value="Edit" />
                    break;
            }
        </div>        
    }

</div>

如果有人可以帮助我,将不胜感激。

4

3 回答 3

1

我也有同样的问题。这是造成它的原因,以及我如何修复它:

原因:

[Authorize]
[HttpGet]
public ActionResult BidToolv2(BidToolv2ViewModel model)
{
    ...

修复:

[Authorize]
[HttpGet]
public ActionResult BidToolv2()
{
    BidToolv2ViewModel model = new BidToolv2ViewModel();

本质上问题是当用户第一次访问页面时,控制器采用了一个空模型,当页面加载时,它假设它已经通过了模型(也许?)。不完全确定,但为了修复它,我删除了模型作为参数,而是在控制器动作本身中创建了一个模型

希望这可以帮助!

于 2014-05-07T21:24:16.257 回答
0

您需要必填以确保输入了某些内容。

您需要 Range 来确保输入的值符合您的要求

    Example:



    [Required(ErrorMessage="Weekly Rental value is required")]
    [Range(1, 9999, ErrorMessage = "Value must be between 1 - 9,999")]
    public string WeeklyRental { get; set; }
于 2012-08-05T17:28:54.597 回答
0

好的,将其发布asp.net 论坛。可能是同一个问题的措辞更简洁的版本。

解决方案有点破解,但似乎运行良好 - 为有问题的视图模型类创建了空构造函数,并在空构造函数中将属性初始化为有效值。成功了。

于 2012-07-12T19:45:38.963 回答