使用视图包显示消息而不刷新视图

时间:2013-04-08 06:22:00

标签: ajax asp.net-mvc

我有一个视图A来自ControllerA,它有两个按钮1.创建2.更新

单击更新或创建其打开新的部分视图B作为另一个控制器B的弹出窗口。

我想要获得的是如果在b中成功创建了一条记录,我现在正在关闭弹出窗口。除了关闭弹出窗口,我想在视图A中显示一条消息。

我这样想: 控制器B

      public ActionResult Create(FormCollection args)
    {
        var obj = new ProjectManagernew();

        var res = new ProjectViewModelNew();
        try
        {
            UpdateModel(res);
            if (obj.AddUpdateOrderField(res))
            {
                ViewBag.RecordAdded = true;
                ViewBag.Message = "Project Added Successfully";

            }
            return View(res);
        }
        catch (Exception)
        {
            //ModelState.AddRuleViolations(res.GetRuleViolations());
            return View(res);
        }
    }

在视图中A:

@if(ViewBag.Message!=null)
{
@ViewBag.Message
}

查看B:

@model DreamTrade.Web.BL.ViewModels.ProjectViewModelNew

@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout_Popup.cshtml";
}
@if (ViewBag.RecordAdded != null && (bool)ViewBag.RecordAdded)
{
<script type="text/javascript">
    parent.$.nmTop().close();
 $('#jqgprojectnew').text("Record added successfully");//jqgprojectnew is the name of grid in view A
</script>
 }
   <h2>Create</h2>

 @using (Html.BeginForm()) {
@Html.ValidationSummary(true)

<fieldset>
    <legend>Project</legend>
      @Html.HiddenFor(model => model.ProjectID)

    <div class="editor-label">
        @Html.LabelFor(model => model.ProjectDetail)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.ProjectDetail)
        @Html.ValidationMessageFor(model => model.ProjectDetail)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.ProjectRef)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.ProjectRef)
        @Html.ValidationMessageFor(model => model.ProjectRef)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.ProjectName)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.ProjectName)
        @Html.ValidationMessageFor(model => model.ProjectName)
    </div>

   <div class="editor-label">
        @Html.LabelFor(model => model.StatusList)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model => model.ProjectStatusId,new SelectList(Model.StatusList,"SourceStatusId","Description"),"Please Select")
        @Html.ValidationMessageFor(model => model.ProjectStatusId)
    </div>

     <div class="editor-label">
        @Html.LabelFor(model => model.CustomerList)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model => model.Id,new SelectList(Model.CustomerList,"Id","Name"),"Please Select")
        @Html.ValidationMessageFor(model => model.Id)
    </div>
    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>
 <div>
 <a href="javascript:parent.$.nmTop().close()">Back to list</a>
 </div>

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

请让我知道我在哪里做错了

1 个答案:

答案 0 :(得分:1)

在您要显示消息的位置添加标签字段。

<label id=mylabel></label>// Add this before jqgrid

将代码修改为:

@if (ViewBag.RecordAdded != null && (bool)ViewBag.RecordAdded)
{
<script type="text/javascript">
    parent.$.nmTop().close();
    $('#mylabel').text("Record added successfully");
</script>
 }