我有以下代码行,我需要该字段是强制性的,我已经把#34;必需",但是在运行代码时,它不会使字段成为必填字段而只是允许该字段是空的,
@Html.AutocompleteFor(model => model.AgenteNaviero_Codigo, model => model.AgenteNaviero_Nombre, false, Url.Action("AutocompleteAgenteNavieroSearch", "AgenteNaviero", new { area = "Maestros", required = "required" }), "width: 80%")
我感谢你能帮助我或说我如何能让你把这个领域作为强制性的。
答案 0 :(得分:3)
您是否尝试在模型上应用DataAnnotation?我认为你可以对你的模型属性做这样的事情
[Required]
AgenteNaviero_Codigo
然后在你的行动中,
public ActionResult AutocompleteAgenteNavieroSearch(YourModel model)
{
if (ModelState.IsValid)
{
...
}
return View(model);
}
例如,
public class Movie
{
public int ID { get; set; }
[Required(ErrorMessage = "Title is required")]
public string Title { get; set; }
public DateTime ReleaseDate { get; set; }
[Required(ErrorMessage = "Genre must be specified")]
public string Genre { get; set; }
[Range(1, 100, ErrorMessage = "Price must be between $1 and $100")]
public decimal Price { get; set; }
[StringLength(5)]
public string Rating { get; set; }
}
<强>控制器强>
[HttpPost]
public ActionResult Create(Movie movie)
{
if (ModelState.IsValid)
{
db.Movies.Add(movie);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(movie);
}
有关详细信息,请查看here