Validation in MVC not working - asp.net-mvc-3

enter code hereI 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.

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

Related

Remote not firing in ASP Core 1.0 RC2

The Remote attribute is not firing to the controller on validation. Does anyone have a clue why not?
I have the following class:
public class XUser{
[Required]
[Range(0, 100, ErrorMessage = "This is an incorrect ID")] //Just a test
public int ID { get; set; }
[Required]
[Remote("CheckUsername", "Home")]
public string Name { get; set; }
[Required]
[RegularExpression(#"[A-Za-z0-9._%+-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}", ErrorMessage ="This is not an email adres!")]
public string Email { get; set; }
}
The following Controller:
[HttpGet]
public IActionResult Edit(Int64 id) {
return View(users.Where(u => u.ID == id).SingleOrDefault());
}
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Edit(XUser user) {
if (ModelState.IsValid) {
return RedirectToAction("Index");
} else {
return View(user);
}
}
[HttpGet]
public JsonResult CheckUsername(string Name) {
return Json(false);
}
And last but not least, the following view:
#model XUser
#using (Html.BeginForm("Edit", "Home", FormMethod.Post)) {
#Html.AntiForgeryToken()
#Html.ValidationSummary(true)
#Html.EditorFor(Model => Model.ID) #Html.ValidationMessageFor(Model => Model.ID)
#Html.EditorFor(Model => Model.Name) #Html.ValidationMessageFor(Model => Model.Name)
#Html.EditorFor(Model => Model.Email) #Html.ValidationMessageFor(Model => Model.Email)
<input type="submit" value="Save" />
}
In my _Layout I have the following scripts included:
<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>
I also tried the following:
//Class
[Required]
[Remote("CheckUsername", "Home", HttpMethod = "POST")]
public string Name { get; set; }
//Controller
[HttpPost]
[AllowAnonymous]
public JsonResult CheckUsername(string Name) {
return Json(false);
}
I will answer my own question for further reference, if anyone else will come along the same problem. I could've seen it in the solution explorer, but some things you just overlook and can save a lot of time!
With Fiddler I saw that the JQuery files were not found. The paths did not match anymore with MVC 4.0 / 5.0 paths.
They have been moved from:
~/Scripts/jquery.validate.min.js
~/Scripts/jquery.validate.unobtrusive.min.js
To:
~/lib/jquery-validation/dist/jquery.validate.js
~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js
Further; AllowAnonymous is not obligated to have as an attribute with Remote[] (For neither HttpPost or HttpGet).

How do you send data to controller with ajax.beginform?

This is my method on controller "sale"
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Models.account account)
{
Models.sale creaventa = new Models.sale();
//creaventa.account = cliente;
creaventa.createdon = DateTime.Now;
creaventa.idaccount = account.id;
creaventa.modifiedon = DateTime.Now;
creaventa.status = 0;
context.sales.Add(creaventa);
context.SaveChanges();
// return "venta creada";
return View();
}
and this is the partial view
#model List<modal3.Models.account>
#{
ViewBag.Title = "Create";
}
<select class="form-control" id="control1">
#{
foreach (var cliente in Model)
{
<option value="#cliente.id"> #cliente.name</option>
}
}
</select>
#*#using (Html.BeginForm("create", "sale", FormMethod.Post, new {id="my-form" }))
{
#Html.AntiForgeryToken()
<button type="submit" class="btn btn-default" value="Create" id="btncrear">
Iniciar Venta
</button>
}*#
#using (
Ajax.BeginForm("create","sale",new AjaxOptions()
{
HttpMethod ="Post",
InsertionMode = InsertionMode.Replace,
})
)
{
#Html.AntiForgeryToken()
<button type="submit" class="btn btn-default" value="Create" id="btncrear">
Iniciar Venta
</button>
}
This does enter to method with this does not send the model.
Then:
How to send a model?
How to send a lot of objects?
this is my model
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace modal3.Models
{
using System;
using System.Collections.Generic;
public partial class sale
{
public sale()
{
this.saledetails = new HashSet<saledetail>();
}
public int id { get; set; }
public Nullable<System.DateTime> createdon { get; set; }
public Nullable<System.DateTime> modifiedon { get; set; }
public Nullable<int> status { get; set; }
public Nullable<int> idaccount { get; set; }
public virtual account account { get; set; }
public virtual ICollection<saledetail> saledetails { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
namespace modal3.Models
{
[MetadataType (typeof (sale_validation ))]
public partial class sale
{
}
public class sale_validation
{
//2015-06-17 22:07:26.353 2015-06-17 22:07:26.353 1 1
[Display (Name="")]
[HiddenInput (DisplayValue =false )]
public Nullable<System.DateTime> createdon { get; set; }
[Display(Name = "")]
[HiddenInput(DisplayValue = false)]
public Nullable<System.DateTime> modifiedon { get; set; }
[Display(Name = "")]
[HiddenInput(DisplayValue = false)]
public Nullable<int> status { get; set; }
public Nullable<int> idaccount { get; set; }
}
}
To make you understand how AJAX FORM works, I created below code -
Lets say our model -
public class Sale
{
public string SaleOwner { get; set; }
public virtual Account Account { get; set; }
}
public class Account
{
public string Name { get; set; }
}
I created two controller actions -
public ActionResult adatas()
{
return View();
}
[HttpPost]
public JsonResult Create(Sale s)
{
return Json("true");
}
The first controller action return following view -
#model WebApplication1.Controllers.Sale
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
#using (Ajax.BeginForm("Create", "Sale", new AjaxOptions()
{
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "done"
}))
{
#Html.TextBoxFor(m => m.SaleOwner)
#Html.TextBoxFor(m => m.Account.Name)
<input type="submit" value="click" />
}
<div id="done">
</div>
View Renders as follows -
Once we click on button, with breakpoints in the code -
Once AJAX POST happens, output would be -

Remote Validation in MVC3

I am performing Remote Validation in ASP.NET MVC3,but its not working means it not calling the action as I am seeing in Chrome java script Console.Here is my Code:
Model:
public class Genre
{
public int GenreId { get; set; }
[Remote("GenreNameExist","Genre",ErrorMessage="Genre name already Exists!!!")]
public string Name { get; set; }
public String Description { get; set; }
public List<Album> Albums { get; set; }
}
Controller:
public ActionResult GenreNameExist(Genre genre)
{
var gen = db.Genres.Where(x=>x.Name==genre.Name).FirstOrDefault();
if (gen == null)
{
return Json(true, JsonRequestBehavior.AllowGet);
}
else {
return Json(string.Format("{0} is not available.", Name), JsonRequestBehavior.AllowGet);
}
}
and View:
#Html.ValidationSummary(true);
<div class="editor-label">
#Html.LabelFor(x => x.Name)
</div>
<div class="editor-field">
#Html.TextBoxFor(x => x.Name)
#Html.ValidationMessageFor(x=>x.Name)
</div>
Thanks in Advance!!!
You need to add:
var gen = db.Genres.Where(x=>x.Name==Name).FirstOrDefault();
so it will execute.
In the controller pass the class Genre so it maps to that class and then add if(ModelState.IsValid) so it'll perform validation against that class. Or add a ViewModel class.
Update:
Do you have the scripts in the right order?
<script src="/Scripts/jquery-1.7.2.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.validate.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.unobtrusive-ajax.min.js" type="text/javascript"></script>

No view updating after viewmodel been changed

can't find whats wrong and can't find answer anywhere. My problem is my view not updated after view model have bean changed
ViewModel:
public class OrderView
{
public Customer Customer { get; set; }
public Order Order { get; set; }
}
public class Order
{
public int OrderId { get; set; }
public int CustomerId { get; set; }
public List<string> DomenNames { get; set; }
}
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public string Telephone { get; set; }
public string Email { get; set; }
}
Controller:
private OrderView ov;
public ActionResult Index()
{
return View(ov);
}
[HttpPost]
public ActionResult Index(OrderView model, FormCollection collection) {
return View("done");
}
public ActionResult BlankEditorRow(OrderView model) {
ov = model;
ov.Order.DomenNames.Add("");
return View("Index",ov) ;
}
View:
#using (Html.BeginForm("Index","Order",FormMethod.Post, new {id = "createOrder"})) {
#Html.ValidationSummary(true)
<fieldset>
<legend>Order</legend>
<div class="editor-label">
#Html.LabelFor(model => model.Order.DomenNames)
</div>
#for(int i = 0; i < Model.Order.DomenNames.Count; i++) {
<div>
#Html.EditorFor(item => item.Order.DomenNames[i])
</div>
}
<button type="button" id="b1" onclick="setallert()" >Click me</button>
...
and script:
<script type="text/javascript">
function setallert() {
$.ajax({
url: "Order/BlankEditorRow",
data: $('#createOrder').serialize(),
cache: false,
success: function (data) {
...?
}
});
};
</script>
it goes fine to pass model to controller and while debugging through view I can see that model is changed, but in some case it nothing happen in view. It looks like old model is in place.
I have solved my problem. I post my solution in case some one need it:
Controller:
public ActionResult BlankEditorRow(OrderView model) {
model.Order.DomenNames.Add("");
return PartialView("Index", model) ;
}
View:
<div class="editor-label">
#Html.LabelFor(model => model.Order.DomenNames)
</div>
<div id="tt">
#{Html.RenderPartial("OrderDN", this.Model);}
</div>
<button type="button" id="addBtn">Click me</button>
Script:
$("#addBtn").click(function () {
$.get('#Url.Action("BlankEditorRow", "Order")', $('#createOrder').serialize(), function (res) {
document.getElementById("tt").innerHTML = res;
});
})
I hope it will be useful to someone

asp.net mvc 3 remote attribute passing 3 fields

I want to pass three field to my controller using RemoteAttribute. How can i do it?
public int ID1 { get; set; }
public int ID2 { get; set; }
[Remote("CheckTopicExists", "Groups", AdditionalFields = "ID1", ErrorMessage = " ")]
public string Topic { get; set; }
public ActionResult CheckTopicExists(string topic, int ID1,int ID2)
{
return Json(true, JsonRequestBehavior.AllowGet);
}
How can i pass three field to that function?
You could separate them by comma:
AdditionalFields = "ID1, ID2"
Full example:
Model:
public class MyViewModel
{
public int ID1 { get; set; }
public int ID2 { get; set; }
[Remote("CheckTopicExists", "Home", AdditionalFields = "ID1, ID2", ErrorMessage = " ")]
public string Topic { get; set; }
}
Controller:
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new MyViewModel
{
ID1 = 1,
ID2 = 2,
Topic = "sample topic"
});
}
[HttpPost]
public ActionResult Index(MyViewModel model)
{
return View(model);
}
public ActionResult CheckTopicExists(MyViewModel model)
{
return Json(false, JsonRequestBehavior.AllowGet);
}
}
View:
#model MyViewModel
<script src="#Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
#using (Html.BeginForm())
{
#Html.EditorFor(x => x.ID1)
#Html.EditorFor(x => x.ID2)
#Html.LabelFor(x => x.Topic)
#Html.EditorFor(x => x.Topic)
#Html.ValidationMessageFor(x => x.Topic)
<input type="submit" value="OK" />
}
Be aware of sending dates, sometimes controller receive date in a wrong format: was dd/mm/yyyy, receive mm/dd/yyyy
Instead of using
public ActionResult CheckTopicExists(MyViewModel model)
If you use
public ActionResult CheckTopicExists(FormCollection Collection)
then you can reuse the code for other classes as well

Resources