表单域模型类的不验证 [英] Validation of Form Field not in Model Class

查看:109
本文介绍了表单域模型类的不验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图增加对表单域需要验证不是我的数据库模型。目前在我的数据库模型中,我有2个值。

型号:

 公共类值{
    [需要]
    公共字符串值1 {搞定;组; }
    [需要]
    公共字符串值2 {搞定;组; }
}

我想在我的观点第三个值组成,可以处理验证,但它不存在于数据库中,所以不应该在我的数据库模型存在,但我需要它的控制器使用。

查看:

 < D​​IV CLASS =主编场>
        @ Html.EditorFor(型号=> model.Value1)
        @ Html.ValidationMessageFor(型号=> model.Value1)
    < / DIV>    < D​​IV CLASS =主编场>
        @ Html.EditorFor(型号=> model.Value2)
        @ Html.ValidationMessageFor(型号=> model.Value2)
    < / DIV>

我如何添加第三场与同样简单的验证,但是,这并不在我的数据库存在吗?如果没有,我怎么能实现类似的验证?

例如:

 < D​​IV CLASS =主编场>
        @ Html.EditorFor(型号=> model.Value3)
        @ Html.ValidationMessageFor(型号=> model.Value3)
      < / DIV>

感谢您!


解决方案

  我

如何添加第三个字段使用相同的简单验证,但
  在我的数据库中不存在?


这就是视图模型就派上用场了。所以,你可以接受,这并不一定(或特异性)映射回你的模型或数据库或多或少的投入。所以,你可以有这样的事情:

 公共类Values​​ViewModel {
    [需要]
    公共字符串值1 {搞定;组; }
    [需要]
    公共字符串值2 {搞定;组; }
    [需要]
    公共字符串值3 {搞定;组; }
}

您查询,然后从数据库的模型,并将其映射到视图模型。事情是这样的:

 公众的ActionResult的GetValues​​(){
    变种值= getFromDb();
    返回查看(新Values​​ViewModel {
        值1 = values​​.Value1;
        值2 = values​​.Value2;
        值3 = some_other_value;
    });}

您就可以收到回用,在你的数据库做,你认为合适的东西字段一起。

  [HttpPost]
公众的ActionResult PostValues​​(Values​​ViewModel输入){
    变种值= getFromDb();
    //映射值回从视图模型的模型
    values​​.Value1 = input.Value1;
    values​​.Value2 = input.Value2;
    //应用于值3的一些逻辑
    //等等等等
    //你的模型保存到数据库中
}

I am trying to add a required validation for a form field not in my database model. Currently in my database model, I have 2 values.

Model:

public class Values{
    [Required]
    public string Value1{ get; set; }
    [Required]
    public string Value2 { get; set; }
}

I want a third value in my Views form that can handle validation, but it does not exist in the database, so should not exist in my Database Model, but I need it for use in the Controller.

View:

    <div class="editor-field">
        @Html.EditorFor(model => model.Value1)
        @Html.ValidationMessageFor(model => model.Value1)
    </div>

    <div class="editor-field">
        @Html.EditorFor(model => model.Value2)
        @Html.ValidationMessageFor(model => model.Value2)
    </div>

How can I add a third field with the same simple validation, but that does not exist in my database? If not, how can I achieve similar validation?

Example:

      <div class="editor-field">
        @Html.EditorFor(model => model.Value3)
        @Html.ValidationMessageFor(model => model.Value3)
      </div>

Thank You!

解决方案

How can I add a third field with the same simple validation, but that does not exist in my database?

This is where viewmodel comes in handy. So you can accept more or less input that does not necessarily (or specifically) mapped back to your model or database. So you can have something like this:

public class ValuesViewModel {
    [Required]
    public string Value1{ get; set; }
    [Required]
    public string Value2 { get; set; }
    [Required]
    public string Value3 { get; set; }
}

You then query the model from your database and map it to that viewmodel. Something like this:

public ActionResult GetValues() {
    var values = getFromDb();
    return View(new ValuesViewModel {
        Value1 = values.Value1;
        Value2 = values.Value2;
        Value3 = some_other_value;
    });

}

You can then receive it back along with the fields that are in your database and do things with as you see fit.

[HttpPost]
public ActionResult PostValues(ValuesViewModel input) {
    var values = getFromDb();
    // map values back to the model from the viewmodel
    values.Value1 = input.Value1;
    values.Value2 = input.Value2;
    // some logic applied to Value3
    // etc, etc
    // save your model to the database
}

这篇关于表单域模型类的不验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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