1

I have an View page, and two partial pages, one to add a single announcement, and the other to display an announcement.

In the list of displayed announcements, there is an edit actionlink, that gets the Id of the selected item in the list, and pulls back the data to the other partial, in order for the user to update it.

Problem being that the partial is being loaded outside of the view, but for the life of me I can't find/remember how to get the partial back to the view page.

How can I achieve this?

Update:

I have tried using Ajax.ActionLink(...) as suggested for the edit button, but cannot get it to work, it is still loading the partial as its own page, and not within the view page.

@Ajax.ActionLink(
        "AJAX Edit",
        "Edit",
        "Announcements",
        new { id = Model.Id },
         new AjaxOptions { UpdateTargetId = "newAnnouncementContainer" })

Controller:

public ActionResult Edit(int? id)
        {
            if (id != null)
            {
                var qry = (from a in db.DbAnnouncement
                           where a.Id == id
                           select a).SingleOrDefault();
                return PartialView("~/Views/Shared/Partials/AnnouncementPartial.cshtml", qry);
            }
            else
            {
                return PartialView("~/Views/Shared/Partials/AnnouncementPartial.cshtml", new Announcement());
            }
        }

View page:

<h2>Announcements</h2>
<div>
    <div class="announcementTable">
        <input type="button" id="announcementBtn" value="Add Announcement" />
        <div id="newAnnouncementContainer">
            @{Html.RenderPartial("~/Views/Shared/Partials/AnnouncementPartial.cshtml", new _4DStatusMVCWeb.Models.Announcement());}
        </div>
        <br />
        <div class="announcementTableRow">
            <div></div>
            <div class="announcementHeader announcementTableCell">Title</div>
            <div class="announcementHeader announcementTableCell announcementDetails">Details</div>
            <div class="announcementHeader announcementTableCell">Created</div>
            <div class="announcementHeader announcementTableCell">Updated</div>
            <div class="announcementHeader announcementTableCell annoucementVersion">V#</div>
            <div class="announcementHeader announcementTableCell">Start</div>
            <div class="announcementHeader announcementTableCell">Expiry</div>
            <div class="announcementHeader announcementTableCell">CMDS</div>
        </div>
        @foreach (var a in Model.AnnouncementList)
        {
            <div class="announcementTableRow">
                @{Html.RenderPartial("~/Views/Shared/Partials/AnnouncementListItemPartial.cshtml", a); }
            </div>
        }
    </div>
</div>

Announcement List Partial page:

@using (Html.BeginForm())
{
    <div>@Html.HiddenFor(mi => mi.Id)</div>
    <div class="announcementTableCell">@Html.DisplayFor(mi => mi.Title)</div>
    <div class="announcementTableCell announcementDetails">@Html.Raw(Model.Details)</div>
    <div class="announcementTableCell">@Html.DisplayFor(mi => mi.DTCreated)</div>
    <div class="announcementTableCell">@Html.DisplayFor(mi => mi.LastUpdated)</div>
    <div class="announcementTableCell annoucementVersion">@Html.DisplayFor(mi => mi.VersionNum)</div>
    <div class="announcementTableCell">@Html.DisplayFor(mi => mi.StartDT)</div>
    <div class="announcementTableCell">@Html.DisplayFor(mi => mi.ExpiryDT)</div>
    <div class="announcementTableCell">
        @Html.ActionLink("Edit", "Edit", new { id = Model.Id })
        @Html.ActionLink("Delete", "Delete", new {  id = Model.Id })
    </div>
}

Submit partial page:

@using (Html.BeginForm("Create", "Announcements"))
{
    <div class="newAnnouncementTableRow1">
        <div>@Html.LabelFor(m => m.Title)</div>
        <div>@Html.EditorFor(m => m.Title)</div>
        <label>Status Populater</label>
        <select id="statusDropdown">
            <option value="0"> - Select - </option>
            <option value="1">Hosting - Down</option>
            <option value="2">Hosting - Degraded</option>
            <option value="3">Hosting - Impaired</option>
            <option value="4">Hosting - Restored</option>
            <option value="5">Connection - Down</option>
            <option value="6">Connection - Degraded</option>
            <option value="7">Connenction - Impaired</option>
            <option value="8">Connection - Restored</option>
        </select>
        <div>@Html.LabelFor(m => m.Details)</div>
        <div>@Html.EditorFor(m => m.Details)</div>
    </div>
    <div class="newAnnouncementTableRow2">
        <label>Status Type</label>
        <select id="groupId">
            <option value="1">Facilities</option>
            <option value="2">Technical</option>
            <option value="3">Network</option>
            <option value="4">General</option>
        </select>
        <div>@Html.LabelFor(m => m.StartDT)</div>
        <div>@Html.EditorFor(m => m.StartDT)</div>
        <div>@Html.LabelFor(m => m.ExpiryDT)</div>
        <div>@Html.EditorFor(m => m.ExpiryDT)</div>
        <div>@Html.LabelFor(m => m.Enabled)</div>
        <div>
            @Html.RadioButtonFor(m => m.Enabled, 1)Yes
            @Html.RadioButtonFor(m => m.Enabled, 0)No
        </div>
        <br/>
        <input type="submit" formaction="Announcements/Create/" value="Create" />
        <input type="submit" formaction="Announcemens/Clear/" value="Clear" />
    </div>
}
PurpleSmurph
  • 2,055
  • 3
  • 32
  • 52
  • `Ajax.BeginForm`? You want Ajax not whole page Html load right? – Thomas Boby Feb 05 '16 at 15:26
  • @ThomasBoby I want the Announcement partial to load with the Announcement data to load into the ViewPage. So I'll be updating the Announcement Partial that's already on the page. – PurpleSmurph Feb 05 '16 at 15:28
  • Why do you even have Forms? I don't see a submit... One way of doing this is with Ajax forms where you set your replacetarget as the containing div. If you want two submits you'll need to set up your own javascript Ajax calls however. See http://stackoverflow.com/a/7295909/3175307 – Thomas Boby Feb 05 '16 at 15:32
  • @ThomasBoby sorry missed the partial I'm trying to copy the data into. I saw that Q when I was searching for a solution, brain is a bit fried (late Friday) but I'll revisit and see if I can get it to work this time. – PurpleSmurph Feb 05 '16 at 15:40
  • Basically, Ajax.BeginForm or Ajax.ActionLink is what you want :) Both have a hook on the return that places it into the current page HTML. If you want to replace rows of your table you'll have to add a unique id into the div and pass that id to the ajax target parameter – Thomas Boby Feb 05 '16 at 15:43
  • Brilliant - will have a look later, if it's the same I'll close the Q as a dupe, thanks for your help. – PurpleSmurph Feb 05 '16 at 15:44

0 Answers0