【发布时间】:2015-04-20 02:54:34
【问题描述】:
我的Manufacturers 实体有以下Create() - POST 控制器:
// POST: INV_Manufacturers/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create([Bind(Include = "Id,manufacturer_description,created_date,created_by,modified_date,modified_by")] INV_Manufacturers iNV_Manufacturers)
{
iNV_Manufacturers.created_date = DateTime.Now;
iNV_Manufacturers.created_by = System.Environment.UserName;
if (ModelState.IsValid == false && iNV_Manufacturers.Id == 0)
{
ModelState.Clear();
}
if (ModelState.IsValid)
{
db.INV_Manufacturers.Add(iNV_Manufacturers);
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(iNV_Manufacturers);
}
Model 是这样定义的:
public class INV_Manufacturers
{
public int Id { get; set; }
[Required(ErrorMessage = "Please enter a Manufacturer.")]
public string manufacturer_description { get; set; }
[Required]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime created_date { get; set; }
[Required]
public string created_by { get; set; }
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime? modified_date { get; set; }
public string modified_by { get; set; }
}
在我的控制器中使用LINQ 的语法是什么,我可以在其中搜索INV_Manufacturers 表并检查表中是否已经存在具有相同description 的Manufacturer保存在我的Create() 方法中?
伪码:
if (manufacturer.description == ANY.manufacturer.description in Table)
{
alert("This value already exists in table!");
} else {
Save(new manufacturer);
}
【问题讨论】:
-
除了 Travis 对服务器端检查的回答之外,如果您还需要客户端验证,还可以使用
[Remote]属性。 How to: Implement Remote Validation in ASP.NET MVC
标签: c# asp.net-mvc linq asp.net-mvc-5