部分视图呈现为验证失败的完整视图

时间:2016-12-28 07:41:45

标签: asp.net-mvc razor

我的控制器

public ActionResult Create()
    {
        return PartialView();
    }

    //
    // POST: /User/Create

    [HttpPost]
    public ActionResult Create(User user)
    {
        if (ModelState.IsValid)
        {
            db.User.Add(user);
            db.SaveChanges();
            TempData["Message"] = "Data has been saved successfully!";
            return RedirectToAction("Index");
        }

        return View(user);
    }

我的观点

   <div class="row">



                     @Html.ActionLink("Create New", "Create", null, new { @class = "modal-with-form btn btn-default", href = "#modalForm" })

                    <!-- Modal Form -->


                    <div id="modalForm" class="modal-block modal-block-primary mfp-hide">


                            @Html.Partial("Create", new jQuery_CRUD.DAL.User())

                    </div>
     </div>        

我的部分观点

   @using (Ajax.BeginForm("Create", "User", null, new AjaxOptions { UpdateTargetId = "modalForm", InsertionMode = InsertionMode.Replace }))
  {


    <section class="panel">
    <header class="panel-heading">
        <h2 class="panel-title">Create</h2>
    </header>
    <div class="panel-body">


        <div class="form-group mt-lg">
            @Html.LabelFor(model => model.Name, new { @class = "col-sm-3 control-label" })

            <div class="col-sm-9">
                @Html.TextBoxFor(model => model.Name, new {  name = "name", @class = "form-control", placeholder = "Type your name..."})
                @Html.ValidationMessageFor(model => model.Name)

            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.Address, new { @class = "col-sm-3 control-label" })

            <div class="col-sm-9">
                @Html.TextBoxFor(model => model.Address, new { name = "address", @class = "form-control", placeholder = "Type your Address..." })
                @Html.ValidationMessageFor(model => model.Address)

            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.ContactNo, new { @class = "col-sm-3 control-label" })

            <div class="col-sm-9">
                @Html.TextBoxFor(model => model.ContactNo, new { name = "contactno", @class = "form-control", placeholder = "Type your Contact No..." })
                @Html.ValidationMessageFor(model => model.ContactNo)

            </div>
        </div>

    </div>
    <footer class="panel-footer">
        <div class="row">
            <div class="col-md-12 text-right">


                <input type="submit" value="Create" class="btn btn-primary"  />
                <button class="btn btn-default modal-dismiss" id="btnCancel">Cancel</button>
            </div>
        </div>
    </footer>

</section>   


}

当我单击按钮时,弹出模式,在后期操作验证失败时,视图返回到完整视图。哪里可能是问题???? ..

我是mvc的新手帮助我...

1 个答案:

答案 0 :(得分:0)

在局部视图的开头添加

{
   Layout = null;
}

并在Validation Error not View

中返回partialView
[HttpPost]
public ActionResult Create(User user)
{
    if (ModelState.IsValid)
    {
        db.User.Add(user);
        db.SaveChanges();
        TempData["Message"] = "Data has been saved successfully!";
        return RedirectToAction("Index");
    }

    return PartialView(user);
}