验证消息未显示为模型在ViewModel中的属性

时间:2019-09-18 14:45:42

标签: c# asp.net-mvc

我一直在asp.net中苦苦挣扎模型验证消息

我有一个ViewModel使用的模型。 如果用户未填写必填字段,我希望视图显示验证错误。

当未填写必填字段(预期行为)但我看不到时,我的ModelState.IsValid为false 任何错误消息

我的模型班级:

public class Model
{
        [Required(ErrorMessage = "Name is required.")]
        public string Name { get; set; }

        [Required(ErrorMessage = "Adress is required.")]
        public string Adress { get; set; }
}

我的ViewModel类:

public class ViewModel
{
        [Required]
        public Model SelectedModel { get; set; }

        public string Title { get; set;}
}

我的控制器:

        [HttpPost]
        public ActionResult Create(ViewModel vm)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    bool result = *DatabaseStuff*
                    if(result == true)
                    {
                        return RedirectToAction("Index");
                    }
                    else
                    {
                        return View();
                    }
                }
                return RedirectToAction("Index",vm);
            }
            catch
            {
                return View();
            }
        }

我的观点

@model ViewModel

@using (Html.BeginForm("Create", "MyController", FormMethod.Post))
{
    @Html.AntiForgeryToken()

    <div class="box box-primary">
        <div class="box-header with-border">
            <h4 class="box-title">ViewModel Form</h4>
        </div>
        <div class="box-body">
        <div class="row">
            <div class="col-md-12">
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <div class="form-group">
            @Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label" })
                @Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.SelectedModel.Name, htmlAttributes: new { @class = "control-label" })
                @Html.EditorFor(model => model.SelectedModel.Name, null, "SelectedModel_Name",new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.SelectedModel.Name, "", new { @class = "text-danger", @data_valmsg_for = "SelectedModel_Name" })
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.SelectedModel.Adress, htmlAttributes: new { @class = "control-label" })
                @Html.EditorFor(model => model.SelectedModel.Adress, null, "SelectedModel_Adress",new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.SelectedModel.Adress, "", new { @class = "text-danger", @data_valmsg_for = "SelectedModel_Adress" })
        </div>

    </div>
    </div>
    </div>
        <div class="form-group">
            <div class="box-footer">
                <input type="submit" value="Create" class="btn btn-success pull-right" />
            </div>
        </div>

    </div>

}

谢谢。

1 个答案:

答案 0 :(得分:1)

我相信当ModelState无效时,您应该返回View。

  • 返回视图不发出新请求,它仅呈现视图 而不更改浏览器地址栏中的URL。

  • Return RedirectToAction发出新请求,并在浏览器的URL中显示URL
    MVC用生成的URL更新地址栏。

        try
        {
            if (ModelState.IsValid)
            {
                bool result = *DatabaseStuff*
                if(result == true)
                {
                    return RedirectToAction("Index");
                }
                else
                {
                    return View();
                }
            }
            return View();
        }