I have a form for deleting an object from a table. I would like to put a textfield which would validate the input before pressing the Delete button.
The entity model of the objects looks like this (it has many more attributes, but I just left the important one):
public partial class card
{
public string reason { get; set; }
}
The controller method of the POST (delete) request looks like this:
// POST: /card/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
card temp_card = db.cardss.Find(id);
temp_card.deleted = true;
db.SaveChanges();
if (ModelState.IsValid)
return RedirectToAction("Index");
return View(temp_card);
}
I've read, I have to create another class and use MetaDataAnnotations for this to work, since I'm using entity models. So I wrote this:
[MetadataType(typeof(CardMetaData))]
public partial class card
{
public string reason { get; set; }
}
public class CardMetaData
{
[Required(ErrorMessage = "Write a reason for deletion.")]
public string reason { get; set; }
}
And in my Delete.aspx are the following lines:
<%= Html.ValidationSummary("Delete was unsuccessful.") %>
<div class="display-field">
<%: Html.TextBoxFor(model => model.reason) %>
<%: Html.ValidationMessageFor(model => model.reason) %>
</div>
This doesn't display a message if I press the delete button and the textfield is empty. What am I missing?