Problem with calling a PartialView from a View when clicking a button.
I have a problem with calling a PartialView from a View when clicking a button. When I click the button the full View is rendered instead of the PartialView I need.
This will call either of these two PartialViews, depending on if the user is part of the program or not
_JoinReferralProgram.cshtml
@inherits UmbracoViewPage<PropelHome.PropelModels.ReferralModel>
<div id="referralFormPartial">
<hr />
<h3>Join Referral Program</h3>
<p>You are not a member of the referral program yet. Would you like to join?</p>
<button id="joinPrgrm">Join Program</button>
<hr />
</div>
_ReferralForm.cshtml
@inherits UmbracoViewPage<PropelHome.PropelModels.ReferralModel>
@using Umbraco.Web
<div id="formToSend">
<div>
<p>Congratulations! You are now registered for the referral program.</p>
<p>Your Referral Code: @ViewBag.ReferralCodes.ReferralCode</p>
</div>
<hr />
<div>
<h3>Send Invitations</h3>
<p>Please enter the emails of one or more people you wish to invite to register.</p>
</div>
@using (Ajax.BeginForm("SubmitReferralForm", "ReferralSurface", null, new AjaxOptions
{
HttpMethod = "POST",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "formToSend"
}))
{
@Html.AntiForgeryToken()
<fieldset>
<div class="form-group">
@Html.LabelFor(m => m.Email1)
@Html.TextBoxFor(m => m.Email1)
@Html.ValidationMessageFor(m => m.Email1)
</div>
<div class="form-group">
@Html.LabelFor(m => m.Email2)
@Html.TextBoxFor(m => m.Email2)
@Html.ValidationMessageFor(m => m.Email2)
</div>
<div class="form-group">
@Html.LabelFor(m => m.Email3)
@Html.TextBoxFor(m => m.Email3)
@Html.ValidationMessageFor(m => m.Email3)
</div>
<div class="form-group">
@Html.LabelFor(m => m.Email4)
@Html.TextBoxFor(m => m.Email4)
@Html.ValidationMessageFor(m => m.Email4)
</div>
<div class="form-group">
@Html.LabelFor(m => m.Email5)
@Html.TextBoxFor(m => m.Email5)
@Html.ValidationMessageFor(m => m.Email5)
</div>
</fieldset><br /><br />
<fieldset>
<button type="submit" class="btn btn-success btn-lg margin-right use-ajax">Send Invitations</button>
</fieldset><br /><br />
}
</div>
This is my controller: ReferralSurfaceController.cs
using System.Web.Mvc;
using Umbraco.Web.Mvc;
using PropelHome.PropelModels;
using System;
using PropelHome.PropelData;
namespace PropelHome.PropelControllers
{
public class ReferralSurfaceController : SurfaceController
{
public const string PARTIAL_VIEW_FOLDER = "~/Views/Partials/Referral/";
/// <summary>
/// Renders the Referral Form:
/// @using (Html.BeginUmbracoForm<ReferralSurfaceController>("Index"))
/// </summary>
// GET: ReferenceSurface
public ActionResult RenderReferralForm()
{
if (ViewBag.ReferralCodes != null)
{
return PartialView(PARTIAL_VIEW_FOLDER + "_ReferralForm.cshtml");
}
return PartialView(PARTIAL_VIEW_FOLDER + "_JoinReferralProgram.cshtml");
}
public ActionResult GetReferralCodeAndForm()
{
SetupReferralCodes();
return PartialView(PARTIAL_VIEW_FOLDER + "_ReferralForm.cshtml");
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SubmitReferralForm(ReferralModel model)
{
if (ModelState.IsValid)
{
AddReferralEmailAddress(model);
TempData["ReferralSuccess"] = true;
return PartialView(PARTIAL_VIEW_FOLDER + "_ReferralFormSuccess.cshtml");
}
return PartialView(PARTIAL_VIEW_FOLDER + "_ReferralFormError.cshtml");
}
private void AddReferralEmailAddress(ReferralModel model)
{
try
{
//Generate an email list object to send
string referral_code = model.ReferralCode;
string email_1 = model.Email1;
string email_2 = model.Email2;
string email_3 = model.Email3;
string email_4 = model.Email4;
string email_5 = model.Email5;
TempData["Message"] = "Your invitation has been sent successfully!";
//SetupReferralCodes();
}
catch (Exception)
{
//Throw an exception if there is a problem sending the invites
//Console.WriteLine("Error sending invitations. Please contact the administrator. ");
TempData["Message"] = "An error ocurred while sending the invitations. Please contact the administrator.";
}
}
private void SetupReferralCodes()
{
ViewBag.ReferralCodes = new SelectList(Data.Referrals, "ReferralId", "ReferralCode");
}
}
}
Please help! I have tried several things to no avail.
Hi Dennis, I did try that. But if I set the Layout to 'null', then I can't see any of my parent view where I have the rest of my page like my submenus, main navigation bar, etc.
Problem with calling a PartialView from a View when clicking a button.
I have a problem with calling a PartialView from a View when clicking a button. When I click the button the full View is rendered instead of the PartialView I need.
This is my code:
Parent View - ReferralPage.cshtml:
This will call either of these two PartialViews, depending on if the user is part of the program or not
_JoinReferralProgram.cshtml
_ReferralForm.cshtml
This is my controller: ReferralSurfaceController.cs
Please help! I have tried several things to no avail.
I think the problem is with this line:
Or with this code:
Or both! I don't know.
Thank you!
-Lando Bee
Hi Lando.
Have you tried adding the following at the top of your partial view?
@{ Layout = null; }
Hi Dennis, I did try that. But if I set the Layout to 'null', then I can't see any of my parent view where I have the rest of my page like my submenus, main navigation bar, etc.
Any other suggestions are very much appreciated.
Thanks!
-LandoB
is working on a reply...