user2189168
user2189168

Reputation: 199

Display message using view bag without refreshing view

I have a view A From ControllerA Which has two buttons 1. Create 2. Update

Upon clicking update or Create its opening a new partial viewB As popup from another controller B.

What iam trying to get is If a record is created successfully in b I am now closing the popup. Apart from closing the popup I want to display a message in view A.

I am trying like this: Controller 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);
        }
    }

And in the view A:

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

View 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")
}

please let me know where iam doing wrong

Upvotes: 0

Views: 2539

Answers (1)

Sharun
Sharun

Reputation: 3082

Include a label field where you want to show the message.

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

Modify the code to:

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

Upvotes: 1

Related Questions