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

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

本文介绍了部分视图呈现为验证失败的完整视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我的控制器

public ActionResult Create(){返回 PartialView();}////POST:/用户/创建[HttpPost]公共 ActionResult 创建(用户用户){如果(模型状态.IsValid){db.User.Add(user);db.SaveChanges();TempData["Message"] = "数据保存成功!";return RedirectToAction("索引");}返回视图(用户);}

我的观点

 

@Html.ActionLink("新建", "创建", null, new { @class = "modal-with-form btn btn-default", href = "#modalForm" })<!-- 模态形式--><div id="modalForm" class="modal-block modal-block-primary mfp-hide">@Html.Partial("创建", new jQuery_CRUD.DAL.User())

我的部分观点

 @using (Ajax.BeginForm("Create", "User", null, new AjaxOptions { UpdateTargetId = "modalForm", InsertionMode = InsertionMode.Replace })){<section class="panel"><header class="panel-heading"><h2 class="panel-title">创建</h2></标题><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 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 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)

<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">取消</button>

</页脚></节>}

当我单击按钮时,弹出模态,在发布操作验证失败时,视图返回到完整视图.问题出在哪里????...

我是 mvc 的新手,请帮我解决这个问题..

解决方案

在局部视图的开头添加

{布局 = 空;}

并在验证错误中返回partialView而不是View

[HttpPost]公共 ActionResult 创建(用户用户){如果(模型状态.IsValid){db.User.Add(user);db.SaveChanges();TempData["Message"] = "数据保存成功!";return RedirectToAction("索引");}返回 PartialView(user);}

my controller

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

my view

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

my partial view

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


}

when i click the button , modal pops up,upon validation fail on post action, the view returns to full view. where could be the problem????..

i'm new to mvc help me with this..

解决方案

Add in the beginning of your partial view

{
   Layout = null;
}

And return partialView in Validation Error not View

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

这篇关于部分视图呈现为验证失败的完整视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 18:01