添加验证用的DataAnnotations [英] Adding validation with DataAnnotations

查看:118
本文介绍了添加验证用的DataAnnotations的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从表中删除对象的一种形式。我想提出一个文本框这将验证输入$ P $之前pssing删除按钮。

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

在POST(删除)请求的控制方法是这样的:

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

我读过,我要创建另一个类,并使用MetaDataAnnotations这个工作,因为我使用的实体模型。所以我写了这一点:

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

和我Delete.aspx有下面几行:

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>

这不显示一条消息,如果我preSS删除按钮和文本框为空。我在想什么?

This doesn't display a message if I press the delete button and the textfield is empty. What am I missing?

推荐答案

您需要用 [必需] 属性的原因属性

public class DeleteConfirmViewModel
{
    [Required]
    public string Reason { get; set; }
}

然后返工你的 DeleteConfirmed 的行动。你目前的实现是不工作,因为你首先更新数据库,然后进行验证,如果您的输入模型是正确的。其次是,你不应该在发生非有效的模型重定向,因为你将失去的的ModelState 包含错误(消息)。正确的实施 DeleteConfirmed 将是这样的:

Then rework your DeleteConfirmed action. You're current implementation is just not working because you first update the database and then validate if your input model is correct. Next is that you should not redirect in case of a non valid model because you will loose the ModelState containing the error (messages). A correct implementation of DeleteConfirmed will look like this:

[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id, DeleteConfirmViewModel viewModel)
{
    if (ModelState.IsValid)
        return View(viewModel);

    card temp_card = db.cardss.Find(id);
    temp_card.deleted = true;
    temp_card.reason = viewModel.Reason;
    db.SaveChanges();

    return View(temp_card);
}

在你查看你将需要显示的情况下没有理由被赋予了验证消息

In you're view you will need to show a validation message in case no reason was given

@Html.ValidationMessageFor(m => m.Reason)

因此​​,一个工作输入字段设置为现场的原因在你看来可能看起来像这样

So a working input field setup for the reason field in your view could look like this

@Html.LabelFor(m => m.Reason)
@Html.EditorFor(m => m.Reason)
@Html.ValidationMessageFor(m => m.Reason)

修改

要呈现 DeleteConfirmed 视图,你需要创建一个视图模型,并把它传递给视图

To render the DeleteConfirmed view you'll need to create a view model and pass it to the view

[HttpGet, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
    return View(new DeleteConfirmViewModel());
}

这篇关于添加验证用的DataAnnotations的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆