Reputation: 13
enter code here
I use client side form validation, but it's not working.
My Model:
[Key]
public int Id { get; set; }
[Required(ErrorMessage = "Error")]
[RegularExpression(@"^[0-9\.]*$",ErrorMessage="Error")]
public string Scode { get; set; }
My View:
@Html.ValidationSummary(true)
@using(Html.BeginForm("CreateSharshomari", "VahedInfo", FormMethod.Post))
{
@Html.TextBox("Scode")
@Html.ValidationMessage("Scode")
<input id="BtnErsal" type="submit" value="ارسال" />
}
What could be wrong?
its my controller.i think perhaps i have a problem about it . please look at it and say your opinion .
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();
}
}
please help me.
Upvotes: 0
Views: 306
Reputation: 9448
Have you imported required jQuery files as below? If not , then I would invite you to do so.
<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>
Given your model:
public class MyViewModel
{
[Key]
public int Id { get; set; }
[Required(ErrorMessage = "Error")]
[RegularExpression(@"^[0-9\.]*$",ErrorMessage="Error")]
public string Scode { get; set; }
}
Controller:
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new MyViewModel());
}
[HttpPost]
public ActionResult Index(MyViewModel model)
{
return View(model);
}
}
View:
@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="ارسال" />
}
Note that you must have following keys in the Web.config
file, for validtion to work on client side:
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
Or in the App_Start
protected void Application_Start()
{
//Enable or Disable Client Side Validation at Application Level
HtmlHelper.ClientValidationEnabled = true;
HtmlHelper.UnobtrusiveJavaScriptEnabled = true;
}
Upvotes: 1