MVC中的验证无效

时间:2013-08-29 08:10:25

标签: asp.net-mvc-3

enter code here我使用客户端表单验证,但它无效。

我的模特:

[Key]
public int Id { get; set; }

[Required(ErrorMessage = "Error")]
[RegularExpression(@"^[0-9\.]*$",ErrorMessage="Error")]
public string Scode { get; set; }

我的观点:

@Html.ValidationSummary(true)
@using(Html.BeginForm("CreateSharshomari", "VahedInfo", FormMethod.Post))
{
    @Html.TextBox("Scode")
    @Html.ValidationMessage("Scode")

    <input id="BtnErsal" type="submit" value="ارسال" />
}

可能出现什么问题?

它是我的控制器。我想也许我有一个问题。请看一下并说出你的意见。

    public PartialViewResult CreateSharshomari()
    {
        IEnumerable<Lcity> Lcitys = Dbcon.Lcitys;
        IEnumerable<RFaaliat> RFaaliats = Dbcon.RFaaliats;

        var query = Lcitys.Select(x => new SelectListItem
            {
                Value = x.Citycode.ToString(),
                Text = x.CityName,
                Selected = x.Citycode == 1
            });
        var query1 = RFaaliats.Select(x => new SelectListItem
            {
                Value = x.IDReshteh.ToString(),
                Text = x.ReshteName,
                Selected = x.IDReshteh == 1
            });

        var model = new CityFaaliatViewModel
        {
            Lcitylist = query.AsEnumerable(),
            RFaaliatList = query1.AsEnumerable()
        };
        return PartialView(model);
    }
    [HttpPost]
    public ActionResult CreateSharshomari(TaavoniInfo Info,CityFaaliatViewModel selcode)
    {
        try
        {
            TaavoniInfo idcity = Info;
            idcity.idCitycode = Convert.ToInt32(selcode.SelectedCitycode);
            idcity.idIDReshteh = Convert.ToInt32(selcode.SelectedIDReshteh);
           Dbcon.TaavoniInfos.Add(idcity);
            Dbcon.SaveChanges();
            ViewBag.SuccessMsg = "success";
            return View();
        }
        catch
        {
            ViewBag.SuccessMsg = "error";
            return View();
        }
   }

请帮帮我。

1 个答案:

答案 0 :(得分:1)

您是否导入了如下所需的jQuery文件?如果没有,那么我会邀请你这样做。

<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>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>

鉴于您的模型:

public class MyViewModel
{
  [Key]
  public int Id { get; set; }

  [Required(ErrorMessage = "Error")]
  [RegularExpression(@"^[0-9\.]*$",ErrorMessage="Error")]
  public string Scode { get; set; }
}

<强>控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new MyViewModel());
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        return View(model);
    }
}

查看:

@model SomeAppName.Models.MyViewModel

<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("CreateSharshomari", "VahedInfo", FormMethod.Post))
{
    @Html.ValidationSummary(true)
    @Html.TextBox("Scode")
    @Html.ValidationMessage("Scode")

    <input id="BtnErsal" type="submit" value="ارسال" />
}

请注意,您必须在Web.config文件中包含以下密钥,以便在客户端上运行验证:

<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />

或在App_Start中

protected void Application_Start() 
{
   //Enable or Disable Client Side Validation at Application Level
   HtmlHelper.ClientValidationEnabled = true;
   HtmlHelper.UnobtrusiveJavaScriptEnabled = true;
}