System.NullReferenceException when sending Form Asp.Net MVC

-1

Ihaveaneditcontroller,whichreceivesavaluefromthedatabaseandplaysitforview,andthenIwantwiththatviewtoplaythevaluesonthescreen(basictest)...

However,evenwithmycontrollerleadingtotheeditviewpassingaBranchobject,ittellsmethatbranchisnull.Hereismycontrollerandmyview:

publicActionResultEdita(stringnome_filial){Conexaoconexao=newConexao();Filialfilial=newFilial();stringcomando="SELECT A.FILIAL, B.VALOR_PROPRIEDADE FROM FILIAIS A " +
                         "LEFT JOIN PROP_FILIAIS B ON A.FILIAL = B.FILIAL AND B.PROPRIEDADE = '00814'" +
                         "WHERE A.FILIAL = @FILIAL";

        var parametros = new Dictionary<string, object>
        {
            {"FILIAL", nome_filial}
        };
        var rows = conexao.ExecutaComandoComRetorno(comando, parametros);
        foreach (var row in rows)
        {
            filial.filial = row["FILIAL"];
            filial.estoque_max = row["VALOR_PROPRIEDADE"];
        }

        return View(filial);
    }

    [HttpPost]
    public ActionResult Edita(Filial filial) {

        return Content(filial.estoque_max.ToString() + filial.filial.ToString());
    }

VIEW:

@model MaxEstoqueFiliais.Models.Filial


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Filial</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.filial, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.filial, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.filial, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.estoque_max, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.estoque_max, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.estoque_max, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

Subsidiary Class:

public class Filial
{
    [DisplayName("Filial")]
    public string filial { get; set; }
    [DisplayName("Estoque Máximo")]
    public string estoque_max { get; set; }
}

Why is my branch object being null, and am I passing it to the view?

Notice that the data is taken to the view:

    
asked by anonymous 12.09.2018 / 21:22

2 answers

3

As I mentioned in the comment, the problem was that you had a property called filial and your action was receiving an object named filial , which caused it to get lost. one option is to change the name of the object, another is to change the name of the property, it is at your discretion.

If you notice that in C # conversion is the properties starting with the first letter of capital, you can read more in Capitalization Conventions

    
12.09.2018 / 22:07
2

As the Barbetta commented, I changed the FILIAL property in the FILIAL class to FULLNAME, Turning right

public class Filial
{
 [DisplayName("Filial")]
 public string nome_filial { get; set; }
 [DisplayName("Estoque Máximo")]
 public string estoque_max { get; set; }
}
    
12.09.2018 / 22:05