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

本文关键字:编辑 模型 net asp | 更新日期: 2024-10-26 05:41:45

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

我已经制作了创建方法,但是在编辑方法期间,从表单中检索数据后,所有属性的接缝要么为空,要么为 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="answerswer" class="col-md-2 control-label"></label>
            <div class="col-md-10">
                <input asp-for="answerswer" class="form-control" />
                <span asp-validation-for="answerswer" 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>

命名空间 日托.模型{ 公开课问题 {

    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 和一些字符串属性。

请告诉我,为什么会出现此错误?

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

您的输入没有名称,并且您没有使用 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>
}

这似乎是一个错误。创建一个视图模型并使用它来增强模型。

抱歉晚了