System.Nullreferenceexception sending Form Asp.Net MVC

Asked

Viewed 91 times

-1

inserir a descrição da imagem aqui

I have an edit controller, which receives a value from the bank and plays for view, and then I want with this view to play the values on the screen (basic test)...

However, even with my controller that takes to edit view by passing an object Subsidiary, it tells me which branch is void. Follows below my controller and my view:

public ActionResult Edita(string nome_filial)
    {
        Conexao conexao = new Conexao();
        Filial filial = new Filial();
        string comando = "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")
}

Filial Class:

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

Why is getting null my affiliate object, being that I am passing it to the view?

Note that the data is taken to the view: inserir a descrição da imagem aqui

  • Can anyone help me? I have done everything, I have done the same as other projects of mine and other projects are working, only this one that will not go at all. And it makes no sense!

  • I don’t know if it can be, it’s an achism, but his property is called filical and the filial object, maybe he’s getting lost in it

  • Add the class Filial to the question

  • @Barbetta, you won’t believe it, but I reenomed the branch name in the class, and it worked! Pqp (facepalm)

2 answers

3


As I mentioned in the comment the problem was that you had a property called filial and his action received an object with name filial, this caused it to get lost. one option is to rename the object, another is to rename the property, at your discretion.

Note that in C# the conversion is the properties start with the first uppercase letter, you can read more on Capitalization Conventions

2

According to the Barbetta commented, I changed the AFFILIATE property in the AFFILIATE class to NOME_FILIAL, this time working

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.