Greenmachine
Greenmachine

Reputation: 292

Null after asp.net post? Editting models

I'm new to asp.net, but I'm having a ridiculous amount of trouble creating a simple Edit method. I've googled alot, but it was of no help.Though I've seen this issue accure no answers were relevent to my case.

I've made the create methods, but during edit method, after retrieving data from the form, all of the attributes seam to be either empty or 0.

I've spent two days on this and have no clue what to do. Please please advise.

These are the two methods of the controller are

 public IActionResult Edit(int? id)
        {
            if (id == null)
            {
                return HttpNotFound();
            }

            Question question = _context.questions.Single(m => m.id == id);
            if (question == null)
            {
                return HttpNotFound();
            }
            return View(question);
        }


        [HttpPost]
        public  IActionResult Edit( Question question)
        {
            if(question.id == 0)
                return HttpNotFound();

            //      _context.questions.Attach(question);
            //         _context.Entry(question).State = EntityState.Modified;
            //         _context.SaveChangesAsync();

            return RedirectToAction("Index");

        }

The view's form

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Edit</title>
</head>
<body>

<form asp-action="Edit" method="post" >
    <div class="form-horizontal">
        <h4>Question</h4>
        <hr />
        <div asp-validation-summary="ValidationSummary.ModelOnly" class="text-danger"></div>
        <div class="form-group">
            <input asp-for="id" class="form-control" />
        </div>
        <div class="form-group">
            <label asp-for="question" class="col-md-2 control-label"></label>
            <div class="col-md-10">
                <input asp-for="question" class="form-control" />
                <span asp-validation-for="question" class="text-danger" />
            </div>
        </div>
        <div class="form-group">
            <label asp-for="title" class="col-md-2 control-label"></label>
            <div class="col-md-10">
                <input asp-for="title" class="form-control" />
                <span asp-validation-for="title" class="text-danger" />
            </div>
        </div>
        <div class="form-group">
            <label asp-for="answer" class="col-md-2 control-label"></label>
            <div class="col-md-10">
                <input asp-for="answer" class="form-control" />
                <span asp-validation-for="answer" 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>
</form>

<div>
    <a asp-action="Index">Back to List</a>
</div>

@section Scripts {
    <script src="~/lib/jquery/dist/jquery.min.js"></script>
    <script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script>
    <script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
}
</body>
</html>

namespace Daycare.Models { public class Question {

    public Question() {

    }

    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int id { get; set; }
    public string title { get;set;}

    public string question { get; set; }

    public string answer { get; set; }
    public DateTime date { get; set; }
}

}

The model is just a very simple thing with an Int Id and some string attributes.

Please tell me, why is this error accuring?

Upvotes: 3

Views: 113

Answers (2)

Greenmachine
Greenmachine

Reputation: 292

It seemed to be a bug. Creating a viewmodel and using it aroung the model helped.

Sorry for late post

Upvotes: 0

Vitor Rigoni
Vitor Rigoni

Reputation: 573

Your inputs have no names, and you are not using Razor's helpers for creating them, that's why your model is empty in your HttpPost Edit method.

Here's an example of an edit view:

@model ControleDeProducaoWeb.Models.EquipamentosViewModel

<h2>@ViewBag.Title</h2>
<hr />
@using (Html.BeginForm("Save", ViewContext.RouteData.Values["controller"].ToString(), FormMethod.Post, new { @class = "form-horizontal" }))
{ 
    <div class="row">
        <div class="col-sm-6">
            <div class="form-group">
                <label class="col-sm-3 control-label">Id</label>
                <div class="col-sm-4">
                    @Html.TextBoxFor(x => x.Id, new { @class = "form-control", @disabled = "disabled" })
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label">Coligada</label>
                <div class="col-sm-4">
                    @Html.DropDownListFor(x => x.ColigadaId, ViewData.Model.Coligadas, new { @class = "form-control", @id = "lista-coligadas" })
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label">Filial</label>
                <div class="col-sm-4">
                    @Html.DropDownListFor(x => x.FilialId, ViewData.Model.Filiais, new { @class = "form-control", @id = "lista-filiais" })
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label">Cala Mínima</label>
                <div class="col-sm-4">
                    @Html.TextBoxFor(x => x.CalaMin, new { @class = "form-control" })
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label">Cala Máxima</label>
                <div class="col-sm-4">
                    @Html.TextBoxFor(x => x.CalaMax, new { @class = "form-control" })
                </div>        
            </div>
        </div>
        <div class="col-sm-6">
            <div class="form-group">
                <label class="col-sm-3 control-label">Comprimento Máximo</label>
                <div class="col-sm-4">
                    @Html.TextBoxFor(x => x.ComprimentoMax, new { @class = "form-control" })
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label">Altura Máxima</label>
                <div class="col-sm-4">
                    @Html.TextBoxFor(x => x.AlturaMax, new { @class = "form-control" })
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label">Largura Máxima</label>
                <div class="col-sm-4">
                    @Html.TextBoxFor(x => x.LarguraMax, new { @class = "form-control" })
                </div>
            </div>

            <div class="form-group">
                <div class="col-sm-4 col-sm-offset-3">
                    <a href="@Request.UrlReferrer.AbsoluteUri" class="btn btn-danger pull-right">Cancelar</a>
                    <button class="btn btn-success" type="submit">Salvar</button>
                </div>
            </div>

        </div>
    </div>
}

Upvotes: 2

Related Questions