因为我的是弹出窗口 那个验证有问题就想用微软mvc创建工程自带的例子 的验证方式 但是我的为什么页面就不能验证呢?
大侠们帮我看下我的问题出在那里?
<link href="@Url.Content("~/Content/AddCustomer.css")" rel="stylesheet" type="text/css" /> <script src="@Url.Content("~/Scripts/validate/jquery-1.5.1.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/validate/jquery.validate.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script> </head> <body> @using (Html.BeginForm("AddCustomer_sp", "WC_AddCustomer", FormMethod.Post, new { id = "fromsp" })) { @Html.ValidationSummary(true) <table width="594" id="CustomerInfo_sp"> <tr> <td colspan="4"> Cutomer Type:        @Html.DropDownList("CustomerType", new List<SelectListItem> { (new SelectListItem(){Text="Personal",Value="0",Selected=true}), (new SelectListItem(){Text="Company",Value="1",Selected=false}) }) </td> </tr> <tr> <td width="128"> Cuntomer name: </td> <td width="171">@Html.TextBox("Cuntomername") @Html.ValidationMessageFor(model => model.A_Customer_Name)
就验证这个Cuntomer name
后台代码:
[Required(ErrorMessage = "*")] public string A_Customer_Name { get; set; }
if (ModelState.IsValid) { A_CustomerInfoModel model = new A_CustomerInfoModel(); model.A_Customer_Name = fc["Cuntomername"]; model.A_CustomerLevel = fc["CustomerType"]; model.A_Customer_Address = fc["Adderss"]; model.A_Contacter_email = fc["Email"]; model.A_city = fc["City"]; model.A_Contacter_Tel = fc["Phont"]; bool result = CustomerInfo.InsertModel(model); if (result) { ViewBag.Msg = "ok"; //Response.Write("<script language=\"javascript\">alert('添加成功!');window.close();</script>"); return View("AddCustomer"); } else { ViewBag.msg = "添加失败!"; return View("AddCustomer"); } } return View("AddCustomer");
我那里还要问题吧吗?我看工具自动生成的 也是这样啊
首先给model添加属性
然后view 添加@Html.ValidationMessageFor(model => model.BookName)
控制器判断下就是
我的理解错了吗 还是。。。
解决方案是:
<td width="171">@Html.TextBox("Cuntomername") @Html.ValidationMessageFor(model => model.A_Customer_Name)
name的名字要一样