对于DropDownList的客户端验证失败上的int字段,IE浏览器 [英] Client-side Validation fails for DropDownList on int field, IE browser

查看:117
本文介绍了对于DropDownList的客户端验证失败上的int字段,IE浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Win 7的ULT,IE9。
下面的列表被传递到一个DropDownList在通过视图袋的看法。

 列表< INT> test1ddl =新的List< INT> {0,1,2,3,4,5,6,7,8,9};

这是工作的罚款几天后,突然开始没有客户端验证了这个领​​域的Test1必须是一个数字贴时(它从来没有达到POST操作)。当我通过一个列表或列表失败。如果我用EditorFor更换DropDownList的自检工作和提交更新。

我已经在电脑上所做的唯一的事情就是重新安装IIS,这是在调试使用IIS前preSS的发生。

当我恢复备份的问题就走了电脑。随后,我部署一个应用程序来使用这个DropDownList的设置互联网,它失败与XP IE 8的必须是一个数字错误。 Chrome和Firefox正常工作。它也工作正常IE9 Win7上。

我还发现,我甚至都不需要点击保存,只需点击浏览器窗口出来把它拿出来重点,选择的DropDownList后,带来了验证错误。这种情况与字符串和INT同时列出。这也会发生在IE8与XP部署的Web应用程序。

我恢复失败的Win7配置的备份,它然后在IIS防爆preSS使用IE9再次失败的调试。如果我在这个配置打开了IE9的部署的站点正常工作。 (我不能用IIS防爆preSS试用Chrome,因为失败的配置VS2010将不使用默认的浏览器,只有IE浏览器,这是一个独立的问题,如果任何人有这是为什么发生的事情的想法)。

在这一点上,我不认为这有什么不妥的实际code,但它似乎是一个浏览器的问题。由于XP和IE8仍然是一个非常常见的桌面设置,我需要得到这个工作。

我的问题是,为什么某些浏览器生成验证错误?

谢谢,乔

如果您创建一个新的MVC3 Web App和添加这些文件,你可以重新创建我的ddlTestDB应用程序。

 将此加入到_Layout.cshtml菜单。 <立GT; @ Html.ActionLink(测试,索引,测试)< /李>***************控制器TestController.cs **********
使用System.Collections.Generic;
使用System.Web.Mvc;
使用ddltest.Models;
使用ddltest.Infrastructure;命名空间ddltest.Controllers
{
  公共类的TestController:控制器
  {
    ddlTestDb _db =新ddlTestDb();    公众的ActionResult指数()
    {
      VAR模型= _db.Tests;
      返回查看(模型);
    }    公众的ActionResult编辑(INT ID)
    {
      变种测试= _db.Tests.FindById(ID);
      //列表<串GT; test1ddl =新的List<串GT; {0,1,2,3,4,5,6,7,8,9};
      清单< INT> test1ddl =新的List< INT> {0,1,2,3,4,5,6,7,8,9};
      ViewBag._Test1 = test1ddl;
      返回查看(测试);
    }    [HttpPost]
    公众的ActionResult编辑(INT ID,收集的FormCollection)
    {
      变种测试= _db.Tests.FindById(ID);      如果(TryUpdateModel(测试)){返回RedirectToAction(「指数」); }      返回查看(测试);
    }
  }
}**********************类test.cs中**************
命名空间ddltest.Models
{公共类的测试{公众诠释标识{搞定;组; }公众诠释的Test1 {搞定;组; }}}**********************静态数据ddlTestDb.cs *****************
使用System.Collections.Generic;命名空间ddltest.Models
{
  公共类ddlTestDb
  {
    静态ddlTestDb()
    {
        _tests =新的List<试验>();
        _tests.Add(新测试{ID = 1,测试1 = 0});
        _tests.Add(新测试{ID = 2,测试1 = 1});
        _tests.Add(新测试{ID = 3,测试1 = 2});
        _tests.Add(新测试{ID = 4,测试1 = 1});
    }    公众的IList<试验>测试{{返回_tests; }}    静态列表<试验> _tests;
  }
}********************* Extensions.cs ************
使用System.Collections.Generic;
使用System.Linq的;
使用ddltest.Models;命名空间ddltest.Infrastructure
{
  公共静态类TestExtensions
  {
    公共静态测试FindById(这IList的<试验>的测试中,INT ID)
    {
      返回tests.Single(T => t.Id == ID);
    }
  }
}******************视图\\测试\\ Edit.cshtml ****************
@model ddltest.Models.Test@ {
    ViewBag.Title =编辑;
}< H2>编辑< / H><脚本的src =@ Url.Content(〜/脚本/ jquery.validate.min.js)TYPE =文/ JavaScript的>< / SCRIPT>
<脚本的src =@ Url.Content(〜/脚本/ jquery.validate.unobtrusive.min.js)TYPE =文/ JavaScript的>< / SCRIPT>@using(Html.BeginForm()){
    @ Html.ValidationSummary(真)
    <&字段集GT;
        <传奇>测试与LT; /传说>        @ Html.HiddenFor(型号=> model.Id)        < D​​IV CLASS =编辑标记>
            @ Html.LabelFor(型号=> model.Test1)
        < / DIV>
        < D​​IV CLASS =主编场>
            @ Html.DropDownList(测试1,新的SelectList(ViewBag._Test1))
            @ * @ Html.EditorFor(型号=> model.Test1)* @
            @ Html.ValidationMessageFor(型号=> model.Test1)
        < / DIV>        &所述p为H.;
            <输入类型=提交值=保存/>
        &所述; / P>
    < /字段集>
}< D​​IV>
    @ Html.ActionLink(返回目录,索引)
< / DIV>***************视图\\测试\\ Index.cshtml ************
@model IEnumerable的< ddltest.Models.Test>@ {
    ViewBag.Title =指数;
}< H2>指数< / H>&所述p为H.;
    @ Html.ActionLink(新建,创建)
&所述; / P>
<表>
    &所述; TR>
         <第i个
            ID
        < /第i
        <第i个
            测试1
        < /第i
        百分位>< /第i
    < / TR>@foreach(以型号VAR项){
    &所述; TR>
        &所述; TD>
            @ Html.DisplayFor(modelItem => item.Id)
        < / TD>
        &所述; TD>
            @ Html.DisplayFor(modelItem => item.Test1)
        < / TD>
        &所述; TD>
            @ Html.ActionLink(编辑,编辑,新{ID = item.Id})|
            @ Html.ActionLink(详细信息,详细信息,新{ID = item.Id})|
            @ Html.ActionLink(删除,删除,新{ID = item.Id})
        < / TD>
    < / TR>
}
< /表>


解决方案

<选项> 元素没有值。看看EM:

 <选择ID =Test1的NAME =Test1的>
    <选项> 0℃; /选项>
    <选项> 1 LT; /选项>
    <选项> 2'; /选项>
    <选项>第3版; /选项>
    <选项> 4℃; /选项>
    <选项> 5℃/选项>
    <选项> 6≤/选项>
    <选项> 7 LT; /选项>
    <选项> 8示/选项>
    <选项> 9< /选项>
< /选择>

看到这个问题?他们谁也没有一个值的属性,所以没有被发布到服务器,当您尝试绑定没事你得到一个整数,你会得到什么:错误消息

所以:

 列表< INT> test1ddl =新的List< INT> {0,1,2,3,4,5,6,7,8,9};
ViewBag._Test1 = test1ddl.Select(X =>新建SelectListItem
{
    值= x.ToString(),
    文字= x.ToString()
});

和则:

  @ Html.DropDownList(测试1(IEnumerable的< SelectListItem>)ViewBag._Test1)

现在来看看:

 <选择ID =Test1的NAME =Test1的>
    <期权价值=0> 0℃; /选项>
    <期权价值=1> 1 LT; /选项>
    <期权价值=2> 2'; /选项>
    <期权价值=3>第3版; /选项>
    <期权价值=4> 4℃; /选项>
    <期权价值=5> 5℃/选项>
    <期权价值=6> 6≤/选项>
    <期权价值=7> 7 LT; /选项>
    <期权价值=8> 8示/选项>
    <期权价值=9> 9< /选项>
< /选择>

好多了。哦,请不要使用ViewBag。使用视图模型和助手的强类型的版本,如 Html.DropDownListFor

Win 7 Ult, IE9. The following list is passed to a DropDownList in the view through a view bag.

List<int> test1ddl = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

It was working fine for a few days and then suddenly started failing Client-side Validation with a "The field Test1 must be a number" when posted (it never reaches the POST Action). It fails when I pass a List or List . If I replace the DropDownList with an EditorFor the POST works and the filed is updated.

The only thing I had done on the computer was to re-install IIS, this is occurring in debug using IIS Express.

When I restored a backup to the computer the problem went away. I subsequently deployed an App to the internet using this DropDownList setup and it fails with the "must be a number" error with IE 8 on XP. Chrome and FireFox work fine. It also works fine with IE9 on Win7.

I also found that I don’t even have to click on Save, just clicking out of the browser window to take it out of focus, after selecting the DropDownList, brings up the validation error. This happens with both the string and int List. This also occurs with IE8 on XP with the deployed Web App.

I restored a backup of the failing Win7 configuration and it then failed again in debug using IE9 on IIS Express. If I open up the deployed site with IE9 in this configuration it works fine. (I can’t test Chrome with IIS Express because in the failing configuration VS2010 will not use the default browser, only IE. That is a separate question if anyone has an idea on why that is happening).

At this point I don’t think there is anything wrong with the actual code but it seems to be a browser problem. Since XP and IE8 is still a very common desktop setup I need to get this to work.

My question is why do some browsers generate the Validation error?

Thanks, Joe

If you create a new MVC3 Web App and add these files you can re-create my ddlTestDB app.

Add this to the _Layout.cshtml menu.         <li>@Html.ActionLink("Test", "Index", "Test")</li>

*************** Controller     TestController.cs    **********
using System.Collections.Generic;
using System.Web.Mvc;
using ddltest.Models;
using ddltest.Infrastructure;

namespace ddltest.Controllers
{
  public class TestController : Controller
  {
    ddlTestDb _db = new ddlTestDb();

    public ActionResult Index()
    {
      var model = _db.Tests;
      return View(model);
    }

    public ActionResult Edit(int id)
    {
      var test = _db.Tests.FindById(id);
      //List<string> test1ddl = new List<string> { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
      List<int> test1ddl = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
      ViewBag._Test1 = test1ddl;
      return View(test);
    }

    [HttpPost]
    public ActionResult Edit(int id, FormCollection collection)
    {
      var test = _db.Tests.FindById(id);

      if (TryUpdateModel(test)) { return RedirectToAction("Index"); }

      return View(test);
    }
  }
}

********************** Class          Test.cs **************
namespace ddltest.Models
{   public class Test   {   public int Id { get; set; }     public int Test1 { get; set; }  }  }

********************** Static data  ddlTestDb.cs *****************
using System.Collections.Generic;

namespace ddltest.Models
{
  public class ddlTestDb
  {
    static ddlTestDb()
    {
        _tests = new List<Test>();
        _tests.Add(new Test { Id = 1, Test1 = 0 });
        _tests.Add(new Test { Id = 2, Test1 = 1 });
        _tests.Add(new Test { Id = 3, Test1 = 2 });
        _tests.Add(new Test { Id = 4, Test1 = 1 });
    }

    public IList<Test> Tests { get { return _tests; } }

    static List<Test> _tests;
  }
}

*********************  Extensions.cs ************
using System.Collections.Generic;
using System.Linq;
using ddltest.Models;

namespace ddltest.Infrastructure
{
  public static class TestExtensions
  {
    public static Test FindById(this IList<Test> tests, int id)
    {
      return tests.Single(t => t.Id == id);
    }
  }
}

******************  Views\Test\Edit.cshtml ****************
@model ddltest.Models.Test

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>

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

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

        @Html.HiddenFor(model => model.Id)

        <div class="editor-label">
            @Html.LabelFor(model => model.Test1)
        </div>
        <div class="editor-field">
            @Html.DropDownList("Test1", new SelectList(ViewBag._Test1))
            @*@Html.EditorFor(model => model.Test1)*@
            @Html.ValidationMessageFor(model => model.Test1)
        </div>

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

***************Views\Test\ Index.cshtml ************
@model IEnumerable<ddltest.Models.Test>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
         <th>
            Id
        </th>
        <th>
            Test1
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Id)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Test1)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
            @Html.ActionLink("Details", "Details", new { id=item.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.Id })
        </td>
    </tr>
}
</table>

解决方案

Your <option> elements don't have values. Look at em:

<select id="Test1" name="Test1">
    <option>0</option>
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
    <option>6</option>
    <option>7</option>
    <option>8</option>
    <option>9</option>
</select>

See the problem? None of them have a value attribute, so nothing gets posted to the server and when you try to bind nothing to an integer you get what you get: an error message.

So:

List<int> test1ddl = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
ViewBag._Test1 = test1ddl.Select(x => new SelectListItem
{
    Value = x.ToString(),
    Text = x.ToString()
});

and then:

@Html.DropDownList("Test1", (IEnumerable<SelectListItem>)ViewBag._Test1)

Look now:

<select id="Test1" name="Test1">
    <option value="0">0</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
</select>

Much better. Oh and please don't use ViewBag. Use view models and the strongly typed versions of the helpers such as Html.DropDownListFor.

这篇关于对于DropDownList的客户端验证失败上的int字段,IE浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆