我的get方法中出现堆栈溢出异常

时间:2019-01-16 11:44:47

标签: c# asp.net-mvc

在重构代码时,当我的视图尝试访问类的成员时,我的 get 方法抛出了System.StackOverflowException。 这是我第一次使用? (空推销运算符)在c#中。

那是我的模型

using System;
using System.ComponentModel.DataAnnotations;

namespace Extranet45.Models
{
    public class Complaint
    {
        public string Nome
        {
            get => Nome ?? "Nome não informado.";
            private set => Nome = value ?? "Nome não informado.";
        }
        public string Email
        {
            get => Email ?? "Email não informado.";
            private set => Email = value ?? "Email não informado.";
        }
        [Required(ErrorMessage = "A denuncia não possui o texto obrigatório do seu conteúdo. (Corpo da Mensagem)")]
        public string Denuncia
        {
            get => Denuncia ?? "O texto da denuncia não foi encontrado.";
            private set => Denuncia = value ?? throw new ArgumentNullException("O campo denúncia é obrigatório.");
        }
        public Complaint() { }

        public Complaint(string nome, string email, string denuncia)
        {
            Nome = nome;
            Email = email;
            Denuncia = denuncia;
        }
    }
}

那是我的控制器/动作

using System;
using System.Web.Mvc;
using Extranet45.Models;
using Utils;

namespace Extranet45.Controllers
{
    public class ComplaintController : Controller
    {
        public ActionResult Index()
        {
            return RedirectToAction("Send");
        }

        // GET: Complaint/Send
        public ActionResult Send()
        {

            return View(new Complaint());
        }
    }
}

这是我的视图部分,其中引发了异常。

@model Extranet45.Models.Complaint

@{

    Layout = "~/Views/Shared/_Layout.cshtml";
    ViewBag.Title = "Denúncia";
    ViewBag.ShowHeader = false;
    ViewBag.BodyClass = "profile-page"; //Faz com que o header fique com a altura menor, porém ñão é uma solução elegante, refatorar
    ViewBag.HeaderImageUrl = "https://github.com/creativetimofficial/material-kit/blob/master/assets/img/city-profile.jpg?raw=true";

}

@using (Html.BeginForm("Send", "Complaint", FormMethod.Post, new { @Class = "contact-form"}))
{
    <div class="section section-contacts">
        <div class="col-md-8 ml-auto mr-auto">
            @ViewBag.ErrorMessage
            <h2 id="main-title" class="text-center title">Denuncie</h2>
            <h4 class="text-center description">Caso você tenha alguma reclamação ou precise comunicar algo incorreto ao Ceape, nos envie uma mensagem abaixo ou um email para <a href="mailto:denuncias@ceapema.org.br">denuncias@@ceapema.org.br</a></h4>

            <div class="row justify-content-center">
                <div class="col-md-8">
                    <div class=" form-group bmd-form-group">
                        <label for="textbox-nome" class="bmd-label-static">Nome - Opcional</label>
                        @Html.TextBoxFor(m => m.Nome, new { @id = "textbox-nome", @Type = "Text", @Class = "form-control", name = "nome" })
                    </div>
                </div>
            </div>

更确切地说,当视图尝试访问成员 m.Nome

时,
@Html.TextBoxFor(m => m.Nome, new { @id = "textbox-nome", @Type = "Text", @Class = "form-control", name = "nome" })

我不知道为什么我使用null合并运算符会导致堆栈溢出。

1 个答案:

答案 0 :(得分:0)

这是因为您的Nome,Email和Denucia的安装者称呼自己的过程是无限的。您可以通过为每个属性使用一个后备字段来解决它。

   private string _nome ;

   public string Nome
    {
        get => _nome ?? "Nome não informado.";
        private set => _nome = value ?? "Nome não informado.";
    }

    private string _email; 

    public string Email
    {
        get => _email?? "Email não informado.";
        private set => _email= value ?? "Email não informado.";
    }

    private string _denucia;

    [Required(ErrorMessage = "A denuncia não possui o texto obrigatório do seu conteúdo. (Corpo da Mensagem)")]
    public string Denuncia
    {
        get => _denucia?? "O texto da denuncia não foi encontrado.";
        private set => _denucia= value ?? throw new ArgumentNullException("O campo denúncia é obrigatório.");
    }