asp.net .NET C# -如何清理ViewModel中所需的错误消息?

6mzjoqzu  于 5个月前  发布在  .NET
关注(0)|答案(1)|浏览(45)

我有一个关于我创建的表单中错误信息的处理问题。每当用户不填写信息时,信息将存储在这里ModelState.Root.Children
为了遍历每个item/errorMessage,我首先检查是否有错误的总和,然后继续访问对应于字段的每个字符串。
这是我的页面视图模型的公共类。到目前为止,我所实现的是将每个错误消息传递给List,以便之后我可以在前端的弹出窗口中打印出来。

public class LandingViewModel
{
    [Required]
    [Display(Name = "Introduce un nombre.")]
    public string ContactFormName { get; set; }

    [Required]
    [Display(Name = "Introduce un correo.")]
    public string ContactFormEmail { get; set; }

    [Required]
    [Display(Name = "Introduce un teléfono de contacto.")]
    public string ContactFormTel { get; set; }

    [Required]
    [Display(Name = "Introduce un teléfono de contacto.")]
    public string Reason { get; set; }

    [Required]
    [Display(Name = "Confirma la política de privacidad para enviar la solicitud.")]
    public bool ContactoPolitica { get; set; }

    public string ContactFormText { get; set; }
}

字符串
这是我的公共类,用于创建基本响应,它将包含一个状态和一条消息发送到前端,它可能包含每个错误消息,如果有一个。

public class BaseResponse
{
 public string status { get; set; }
 public string message { get; set; }
}


最后,这是我的HttpPost,它是通过我页面上的AJAX请求触发的。

[HttpPost]
[AllowAnonymous]
public async Task<BaseResponse> Landing(LandingViewModel model)
{
    string result = await registerService.SendEmailForContactLanding(model);
    var response = new BaseResponse();
    try
    {
        if (!string.IsNullOrEmpty(result) && ModelState.ErrorCount == 0)
        {
            ModelState.AddModelError(string.Empty, result);
            response.status = "KO";
            response.message = "No se ha podido enviar tu solicitud. Por favor, intentelo más tarde.";
        }

        string[] errors;

        if (ModelState.ErrorCount > 0)
        {
            List<string> errorMessages = new List<string>();
            foreach (var item in ModelState.Root.Children)
            {

                var errorMessage = item.Errors[0].ErrorMessage;
                errorMessages.Add(errorMessage);

            }
            // in here I will transform the List<string> to a response.message for my front-end
        }

        if (ModelState.IsValid)
        {
            response.status = "OK";
            response.message = "Tu petición se ha enviado correctamente.";
        }
    }
    catch (Exception ex)
    {
        response = new BaseResponse();
        response.status = "KO";
        response.message = "Ha habido un error con tu petición " + ex.Message;
    }

    return response;
}


我想摆脱的是:每当用户没有填写某些字段时,当我通过断点时,错误消息不会像在我的public class LandingViewModel上写的那样出现。
例如,[Display(Name = "Introduce un nombre.")]必须显示为**“Introduce un nombre.",但它不是。它显示为:“The Introduce un teléfono de contacto. field is required."**。

该字段是必填的由于某种原因,在每个错误消息字符串中都会抛出。如何清理此类消息?

jk9hmnmh

jk9hmnmh1#

通过将我的ViewModel更改为这个,它工作了。我得到的错误消息正是我想要的。

public class LandingViewModel
{

    [Required(ErrorMessage = "Introduce un nombre.")]
    public string ContactFormName { get; set; }

    [Required(ErrorMessage = "Introduce un correo.")]
    public string ContactFormEmail { get; set; }

    [Required(ErrorMessage = "Introduce un teléfono de contacto.")]
    public string ContactFormTel { get; set; }

    [Required(ErrorMessage = "Selecciona la razón de contacto.")]
    public string Reason { get; set; }

    [Required(ErrorMessage = "Confirma la política de privacidad para enviar la solicitud.")]
    public bool ContactoPolitica { get; set; }

    public string ContactFormText { get; set; }

}

字符串

相关问题