asp.net帖子后空?编辑模型

时间:2016-04-20 05:44:14

标签: c# asp.net post asp.net-core-mvc

我是asp.net的新手,但我在创建一个简单的Edit方法时遇到了很多麻烦。我已经搜索了很多,但它没有任何帮助。虽然我已经看到这个问题,但没有答案与我的情况有关。

我已经制作了创建方法,但在编辑方法中,从表单中检索数据后,所有属性都会为空或者为0。

我已经花了两天时间,并且不知道该怎么做。请告知。

这些是控制器的两种方法

 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");

        }

视图的形式

<!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>

命名空间Daycare.Models {     公共课问题     {

    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; }
}

}

模型只是一个非常简单的东西,有一个Int Id和一些字符串属性。

请告诉我,为什么这个错误会产生影响?

2 个答案:

答案 0 :(得分:2)

您的输入没有名称,并且您没有使用Razor的帮助程序来创建它们,这就是您的模型在HttpPost Edit方法中为空的原因。

以下是编辑视图的示例:

@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>
}

答案 1 :(得分:0)

这似乎是一个错误。 创建一个viewmodel并使用它来帮助模型。

对于迟到的帖子抱歉