为什么我的MVC应用程序中的ModelState.IsValid属性等于false,当一切看起来都很好

时间:2017-05-10 21:47:11

标签: c# asp.net-mvc view model

我可能在某个地方犯了错误,因为当我打电话给ModelState.IsValid [HttpPost]时,ActionResult属性总是等于假。

这是我的[HttpPost]方法:

[HttpPost]
public ActionResult EditArticle(ArticleEditViewModel model)
{
   //This ModelState.IsValid is allways false
   if (ModelState.IsValid)
   {
      //Write something to database 
   }
}

这是相同但[HttpGet]方法:

public ActionResult EditArticle(Guid Id)
{
    Article article = Controllers.ArticleController.GetById(Id);



            if (article != null)
            {
               ArticleEditViewModel model = new ArticleEditViewModel()
                {
                    articleID = article.articleID,
                    ValidFrom = article.ValidFrom,
                    ValidTo = (DateTime)article.ValidTo,
                    Code = article.Code,
                };

                return View(model);
            }
            return View("NotFound");

        }

以下是我的表单的外观(正如您所看到的,Code不为null且Valid to和valid from也有一些值):

enter image description here

但在我的视觉工作室,当我正在调试时,我正在获得下一个值:

enter image description here

我无法理解为什么这个Propertys为null并且ValidFrom的一些奇怪值为“1.1.0001”,当它们在视图中显示时,它们在文本框中有一些值。

最后,这是我的观点:

@model MyProject.Web.Models.BaseData.ArticleEditViewModel


@using MyProject.DataAccess.Model
@using MyProject.Web.Helpers
@{
    ViewBag.Title = "Edit articles";
}

<div class="">
 @using (Html.BeginForm("EditArticle", "Articles", FormMethod.Post, new { @class = "form-horizontal form-label-left", id = "demo-form2" }))
 {
    @Html.AntiForgeryToken()
    @Html.HiddenFor(model => model.ArticleID)

                            <div class="form-group">
                                @Html.LabelFor(model => model.Code)
                                @Html.TextBoxFor(model => model.Code, new { @class = "form-control", @disabled = "disabled" })
                                @Html.ValidationMessageFor(model => model.Code)
                                <span class="fa fa-barcode form-control-feedback right" aria-hidden="true"></span>
                            </div>

                            //Some other divs which and textboxes which are not important for us right now

                            <div class="form-group">
                                @Html.LabelFor(model => model.ValidFrom)

                                    @Html.TextBoxFor(model => model.ValidFrom, new { @class = "form-control", @disabled = "disabled" })
                                    @Html.ValidationMessageFor(model => model.ValidFrom)

                            </div>

                            <div class="form-group">
                                @Html.LabelFor(model => model.ValidTo)
                                @Html.TextBoxFor(model => model.ValidTo, new { @class = "form-control", @disabled = "disabled" })
                                @Html.ValidationMessageFor(model => model.ValidTo)

                            </div>
                        </div>

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

1 个答案:

答案 0 :(得分:2)

如果字段被禁用,则无法发布。

你有几个选择:

  

将只读字段呈现为标签

如果您无法计算只读字段的值,则可以使用隐藏字段发布值:

@Html.HiddenFor(x => x.DateTo)
  

渲染已禁用的输入

如果您知道可以计算控制器中的值,则可以 现在渲染字段,但在条件之前的操作中,计算您知道未发布的值。

public IActionResult Edit(Model model)
{
    // Calculate the values here
    If (ModelState.IsValid)
    {
        // Do your stuff here...
    }
}

希望这有帮助!