I have a slight problem with my MVC project after the user has entered some details and then submitted the form, after the email has been sent its suppose to redirect them to a Thank you page, but for some reason it keeps looping and sending the email this
morning i recieved 100 + emails with the same details in.
The part where the users fill in the information is within a partial view two text fields and a button.
when the button is pressed it calls its own controller where the email class is called.
This is my partial view
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Free_Divert.SiteMasterModel>" %>
<link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
<link href="../../Content/jcontent.css" rel="stylesheet" type="text/css" />
<div class="free-divert-container">
<div class="free-divert-container-left">
<div class="ValidationSummary">
<%: Html.ValidationSummary(true)%>
</div>
<%--"PartialLandlineMobile", "LandLineMobile", FormMethod.Post, new { enctype = "multipart/form-data" }--%>
<% Html.EnableClientValidation(); %>
<% using (Html.BeginForm())
{ %>
<label id="lblLandline" class="Label">
<i>LandLine Number:</i></label><%= Html.TextBox("LandLineNumber", Model.LandLineNumber, new { maxlength = 11, size = "11", autocomplete = "off", @class = "textBox number", ID = "txtLandLine" })%>
<br />
<br />
<label id="lblMobile" class="Label">
<i>Mobile Number:</i></label><%= Html.TextBox("MobileNumber", Model.MobileNumber, new { maxlength = 11, size = "11", autocomplete = "off", @class = "textBox number", ID = "txtMobileNumber" })%>
<br />
<p>
<button type="submit" value="submit" class="free-divert-check-now-Button floatright"></button>
</p>
<%}%>
</div>
<div class="free-divert-container-right">
<div class="free-divert-speech-bubble-text">
<i>Increase your business potential, where ever you go your landline goes to!</i>
</div>
</div>
</div>
This is the model for the Partial View i have a customer validator on it to check for at least 1 value has been entered between the text fields.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using MVCCore;
using System.Globalization;
namespace Free_Divert
{
[EitherOr("LandLineNumber", "MobileNumber")]
public class SiteMasterModel
{
public string LandLineNumber { get; set; }
public string MobileNumber { get; set; }
}
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public sealed class EitherOrAttribute : ValidationAttribute
{
private const string _defaultErrorMessage = "Either '{0}' or '{1}' must have a value.";
private readonly object _typeId = new object();
public EitherOrAttribute(string primaryProperty, string secondaryProperty)
: base(_defaultErrorMessage)
{
PrimaryProperty = primaryProperty;
SecondaryProperty = secondaryProperty;
}
public string PrimaryProperty { get; private set; }
public string SecondaryProperty { get; private set; }
public override object TypeId
{
get
{
return _typeId;
}
}
public override string FormatErrorMessage(string name)
{
return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString,
PrimaryProperty, SecondaryProperty);
}
public override bool IsValid(object value)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);
object primaryValue = properties.Find(PrimaryProperty, true /* ignoreCase */).GetValue(value);
object secondaryValue = properties.Find(SecondaryProperty, true /* ignoreCase */).GetValue(value);
return primaryValue != null || secondaryValue != null;
}
}
}
And this is my controller where the email class is called etc, it goes on the return view section but then automatically goes back to the
if(modelstate.isvalid)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Free_Divert.Controllers
{
public class LandLineMobileController : Controller
{
//
// GET: /MyView/
[AcceptVerbs(HttpVerbs.Get)]
public PartialViewResult PartialLandlineMobile()
{
SiteMasterModel objSiteMaster = new SiteMasterModel();
return PartialView("PartialLandlineMobile", objSiteMaster);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult PartialLandlineMobile(SiteMasterModel objSiteMaster)
{
if (ModelState.IsValid)
{
SendEmail objEmail = new SendEmail();
objEmail.SendEligibleEmail(objSiteMaster);
return View("../Home/Thank_You", objSiteMaster);
}
return PartialView("PartialLandlineMobile", objSiteMaster);
}
}
}
Thank you all for your information, Iv done the changes and i get this error message
"Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'."
The partial view has been placed within the site master as its used on all of the pages through out the project this is my site master mid way down you will see the renderaction
<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>
<asp:ContentPlaceHolder ID="TitleContent" runat="server" />
</title>
<script src="../../Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.easing.min.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.jcontent.o.8.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.validate-vsdoc.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.validate.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.validate.min.js" type="text/javascript"></script>
<script src="../../Scripts/number.js" type="text/javascript"></script>
<link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
<link href="../../Content/jcontent.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="wrapper">
<div class="free-Divert-headertop">
<div class="free-divert-headertop-text">
Freephone 0844 8888 900 to see if you can get free call diverts to your mobile
</div>
</div>
<div class="free-divert-header">
<div id="title">
<div class="whitespace"></div>
<div class="free-divert-main-logo"></div>
<div id="menucontainer">
<ul id="menu">
<li class="homelink">
<%: Html.ActionLink("home", "Index", "Home")%></li>
<li class="howitworkslink">
<%: Html.ActionLink("how it works", "divert_my_landline", "HowitWorks")%></li>
<li class="customercommentslink">
<%: Html.ActionLink("customer comments", "free_divert_reviews", "CustomerComments")%></li>
<li class="contactuslink">
<%: Html.ActionLink("contact us", "contact_us", "ContactUs")%></li>
</ul>
</div>
<div class="clear"></div>
<div class="free-divert-Eligible">
<p>
Can you divert <span>YOUR</span> existing landline
to <span>YOUR</span> mobile for <span>FREE</span>....Check now for <span>FREE!</span>
</p>
</div>
<div>
<% Html.RenderAction("PartialLandlineMobile", "LandLineMobile"); %>
</div>
</div>
</div>
<div id="main">
<asp:ContentPlaceHolder ID="MainContent" runat="server" />
</div>
</div>
<div class="whitespace20"></div>
<div class="whitespace20"></div>
<div id="footer">
<fieldset>
<legend>Satisfied Customers...</legend>
<br /><br /><br /><br />
</fieldset>
<ul>
<li></li>
<li>Terms and Conditions</li>
<li>Privacy Policy</li>
<li>Cookie Policy</li>
<li>Copyright of Free Divert 2013</li>
</ul>
</div>
</body>
this is the home controller i have the sitemastermodel being returned due to all the pages have the partial view on them again im new to MVC so please help me correct my mistakes?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Free_Divert.Controllers
{
[HandleError]
public class HomeController : Controller
{
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Index()
{
SiteMasterModel objSiteMaster = new SiteMasterModel();
return View(objSiteMaster);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(SiteMasterModel objSiteMaster)
{
return View(objSiteMaster);
}
public ActionResult About()
{
return View();
}
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Thank_You(SiteMasterModel objSiteMaster)
{
return View(objSiteMaster);
}
}
}
i'm not quite sure but begin debugging by not sending objects to thank_you and try cleaning up thing by thing and see where it goes wrong
anyway somebody should check
<% Html.RenderAction("PartialLandlineMobile", "LandLineMobile"); %> i never used aspnet viewengine so i dunno if this is right but the ; looks weird to me ...
If i create the post method within the controller which i had done Partial View Controller below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Free_Divert.Controllers
{
public class LandLineMobileController : Controller
{
//
// GET: /MyView/
[AcceptVerbs(HttpVerbs.Get)]
public PartialViewResult PartialLandlineMobile()
{
SiteMasterModel objSiteMaster = new SiteMasterModel();
return PartialView("PartialLandlineMobile", objSiteMaster);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult PartialLandlineMobile(SiteMasterModel objSiteMaster)
{
if (ModelState.IsValid)
{
SendEmail objEmail = new SendEmail();
objEmail.SendEligibleEmail(objSiteMaster);
return RedirectToAction("Thank_You", "Home", new { objSiteMaster = objSiteMaster });
//return View("../Home/Thank_You", "Site.Master", objSiteMaster);
}
return View(objSiteMaster);//("PartialLandlineMobile", objSiteMaster);
}
}
}
If the modelstate is not valid it only returns the partial view and nothing else it looses its site master and all other contact thats on the page.....
This is incorrect, you can not pass objects as parameter, you need to pass individual properties as route data.
Harrison.Scott
If the modelstate is not valid it only returns the partial view and nothing else it looses its site master and all other contact thats on the page.....
Because you are returning only partial view, if you want to replace just the partial view in your main page then either you need to return the complete view along with layout or call the post method using ajax.
Because you are returning only partial view, if you want to replace just the partial view in your main page then either you need to return the complete view along with layout or call the post method using ajax.
Can you explain in a bit more detail please... im new to MVC i was thinking changing the partial view to be a part of the sitemaster as its been used on every page so it doesnt need to be a partial view does it...
partial views are fine but because you are trying to return it after post it will just display the partial view and not other stuff, you need to return a complete view including layout, I hope it clarifies.
Harrison.Sco...
Member
331 Points
456 Posts
MVC project doesnt redirect to another view after Sending email instead it keeps sending the emai...
Dec 19, 2012 11:07 AM|LINK
Hello,
I have a slight problem with my MVC project after the user has entered some details and then submitted the form, after the email has been sent its suppose to redirect them to a Thank you page, but for some reason it keeps looping and sending the email this morning i recieved 100 + emails with the same details in.
The part where the users fill in the information is within a partial view two text fields and a button.
when the button is pressed it calls its own controller where the email class is called.
This is my partial view
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Free_Divert.SiteMasterModel>" %> <link href="../../Content/Site.css" rel="stylesheet" type="text/css" /> <link href="../../Content/jcontent.css" rel="stylesheet" type="text/css" /> <div class="free-divert-container"> <div class="free-divert-container-left"> <div class="ValidationSummary"> <%: Html.ValidationSummary(true)%> </div> <%--"PartialLandlineMobile", "LandLineMobile", FormMethod.Post, new { enctype = "multipart/form-data" }--%> <% Html.EnableClientValidation(); %> <% using (Html.BeginForm()) { %> <label id="lblLandline" class="Label"> <i>LandLine Number:</i></label><%= Html.TextBox("LandLineNumber", Model.LandLineNumber, new { maxlength = 11, size = "11", autocomplete = "off", @class = "textBox number", ID = "txtLandLine" })%> <br /> <br /> <label id="lblMobile" class="Label"> <i>Mobile Number:</i></label><%= Html.TextBox("MobileNumber", Model.MobileNumber, new { maxlength = 11, size = "11", autocomplete = "off", @class = "textBox number", ID = "txtMobileNumber" })%> <br /> <p> <button type="submit" value="submit" class="free-divert-check-now-Button floatright"></button> </p> <%}%> </div> <div class="free-divert-container-right"> <div class="free-divert-speech-bubble-text"> <i>Increase your business potential, where ever you go your landline goes to!</i> </div> </div> </div>This is the model for the Partial View i have a customer validator on it to check for at least 1 value has been entered between the text fields.
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using MVCCore; using System.Globalization; namespace Free_Divert { [EitherOr("LandLineNumber", "MobileNumber")] public class SiteMasterModel { public string LandLineNumber { get; set; } public string MobileNumber { get; set; } } [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)] public sealed class EitherOrAttribute : ValidationAttribute { private const string _defaultErrorMessage = "Either '{0}' or '{1}' must have a value."; private readonly object _typeId = new object(); public EitherOrAttribute(string primaryProperty, string secondaryProperty) : base(_defaultErrorMessage) { PrimaryProperty = primaryProperty; SecondaryProperty = secondaryProperty; } public string PrimaryProperty { get; private set; } public string SecondaryProperty { get; private set; } public override object TypeId { get { return _typeId; } } public override string FormatErrorMessage(string name) { return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString, PrimaryProperty, SecondaryProperty); } public override bool IsValid(object value) { PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value); object primaryValue = properties.Find(PrimaryProperty, true /* ignoreCase */).GetValue(value); object secondaryValue = properties.Find(SecondaryProperty, true /* ignoreCase */).GetValue(value); return primaryValue != null || secondaryValue != null; } } }And this is my controller where the email class is called etc, it goes on the return view section but then automatically goes back to the
if(modelstate.isvalid)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace Free_Divert.Controllers { public class LandLineMobileController : Controller { // // GET: /MyView/ [AcceptVerbs(HttpVerbs.Get)] public PartialViewResult PartialLandlineMobile() { SiteMasterModel objSiteMaster = new SiteMasterModel(); return PartialView("PartialLandlineMobile", objSiteMaster); } [AcceptVerbs(HttpVerbs.Post)] public ActionResult PartialLandlineMobile(SiteMasterModel objSiteMaster) { if (ModelState.IsValid) { SendEmail objEmail = new SendEmail(); objEmail.SendEligibleEmail(objSiteMaster); return View("../Home/Thank_You", objSiteMaster); } return PartialView("PartialLandlineMobile", objSiteMaster); } } }CPrakash82
All-Star
18284 Points
2841 Posts
Re: MVC project doesnt redirect to another view after Sending email instead it keeps sending the ...
Dec 19, 2012 11:14 AM|LINK
You need to use Redirect for redirect and not the view.
sp00k
Participant
1916 Points
435 Posts
Re: MVC project doesnt redirect to another view after Sending email instead it keeps sending the ...
Dec 19, 2012 11:20 AM|LINK
[AcceptVerbs(HttpVerbs.Get)] public PartialViewResult PartialLandlineMobile() { SiteMasterModel objSiteMaster = new SiteMasterModel(); return PartialView("PartialLandlineMobile", objSiteMaster); } [AcceptVerbs(HttpVerbs.Post)] public ActionResult PartialLandlineMobile(SiteMasterModel objSiteMaster) { if (ModelState.IsValid) { SendEmail objEmail = new SendEmail(); objEmail.SendEligibleEmail(objSiteMaster); return View("../Home/Thank_You", objSiteMaster); } return PartialView("PartialLandlineMobile", objSiteMaster); }put a breakpoint on return view("../Home/Thank_You",objSiteMaster);
also you should change this to a redirecttoaction instead of return view in some other controller directory
so
[AcceptVerbs(HttpVerbs.Get)] public PartialViewResult PartialLandlineMobile() { SiteMasterModel objSiteMaster = new SiteMasterModel(); return PartialView("PartialLandlineMobile", objSiteMaster); } [AcceptVerbs(HttpVerbs.Post)] public ActionResult PartialLandlineMobile(SiteMasterModel objSiteMaster) { if (ModelState.IsValid) { SendEmail objEmail = new SendEmail(); objEmail.SendEligibleEmail(objSiteMaster); return RedirectToAction("ThankYou", "Home", new {objSiteMaster = objSiteMaster}); //return View("../Home/Thank_You", objSiteMaster); } return PartialView("PartialLandlineMobile", objSiteMaster); }last code will probaply work fine but don't forget to make a simple action in your homecontroller that returns View("Thank_You", objSiteMaster);
Harrison.Sco...
Member
331 Points
456 Posts
Re: MVC project doesnt redirect to another view after Sending email instead it keeps sending the ...
Dec 19, 2012 11:31 AM|LINK
Thank you all for your information, Iv done the changes and i get this error message
"Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'."
The partial view has been placed within the site master as its used on all of the pages through out the project this is my site master mid way down you will see the renderaction
<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title> <asp:ContentPlaceHolder ID="TitleContent" runat="server" /> </title> <script src="../../Scripts/jquery-1.7.1.min.js" type="text/javascript"></script> <script src="../../Scripts/jquery.easing.min.js" type="text/javascript"></script> <script src="../../Scripts/jquery.jcontent.o.8.js" type="text/javascript"></script> <script src="../../Scripts/jquery.validate-vsdoc.js" type="text/javascript"></script> <script src="../../Scripts/jquery.validate.js" type="text/javascript"></script> <script src="../../Scripts/jquery.validate.min.js" type="text/javascript"></script> <script src="../../Scripts/number.js" type="text/javascript"></script> <link href="../../Content/Site.css" rel="stylesheet" type="text/css" /> <link href="../../Content/jcontent.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="wrapper"> <div class="free-Divert-headertop"> <div class="free-divert-headertop-text"> Freephone 0844 8888 900 to see if you can get free call diverts to your mobile </div> </div> <div class="free-divert-header"> <div id="title"> <div class="whitespace"></div> <div class="free-divert-main-logo"></div> <div id="menucontainer"> <ul id="menu"> <li class="homelink"> <%: Html.ActionLink("home", "Index", "Home")%></li> <li class="howitworkslink"> <%: Html.ActionLink("how it works", "divert_my_landline", "HowitWorks")%></li> <li class="customercommentslink"> <%: Html.ActionLink("customer comments", "free_divert_reviews", "CustomerComments")%></li> <li class="contactuslink"> <%: Html.ActionLink("contact us", "contact_us", "ContactUs")%></li> </ul> </div> <div class="clear"></div> <div class="free-divert-Eligible"> <p> Can you divert <span>YOUR</span> existing landline to <span>YOUR</span> mobile for <span>FREE</span>....Check now for <span>FREE!</span> </p> </div> <div> <% Html.RenderAction("PartialLandlineMobile", "LandLineMobile"); %> </div> </div> </div> <div id="main"> <asp:ContentPlaceHolder ID="MainContent" runat="server" /> </div> </div> <div class="whitespace20"></div> <div class="whitespace20"></div> <div id="footer"> <fieldset> <legend>Satisfied Customers...</legend> <br /><br /><br /><br /> </fieldset> <ul> <li></li> <li>Terms and Conditions</li> <li>Privacy Policy</li> <li>Cookie Policy</li> <li>Copyright of Free Divert 2013</li> </ul> </div> </body>this is the home controller i have the sitemastermodel being returned due to all the pages have the partial view on them again im new to MVC so please help me correct my mistakes?
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace Free_Divert.Controllers { [HandleError] public class HomeController : Controller { [AcceptVerbs(HttpVerbs.Get)] public ActionResult Index() { SiteMasterModel objSiteMaster = new SiteMasterModel(); return View(objSiteMaster); } [AcceptVerbs(HttpVerbs.Post)] public ActionResult Index(SiteMasterModel objSiteMaster) { return View(objSiteMaster); } public ActionResult About() { return View(); } [AcceptVerbs(HttpVerbs.Get)] public ActionResult Thank_You(SiteMasterModel objSiteMaster) { return View(objSiteMaster); } } }sp00k
Participant
1916 Points
435 Posts
Re: MVC project doesnt redirect to another view after Sending email instead it keeps sending the ...
Dec 19, 2012 11:49 AM|LINK
i'm not quite sure but begin debugging by not sending objects to thank_you and try cleaning up thing by thing and see where it goes wrong
anyway somebody should check
<% Html.RenderAction("PartialLandlineMobile", "LandLineMobile"); %> i never used aspnet viewengine so i dunno if this is right but the ; looks weird to me ...Harrison.Sco...
Member
331 Points
456 Posts
Re: MVC project doesnt redirect to another view after Sending email instead it keeps sending the ...
Dec 19, 2012 12:33 PM|LINK
This is driving me mad :(,
If i create the post method within the controller which i had done Partial View Controller below
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace Free_Divert.Controllers { public class LandLineMobileController : Controller { // // GET: /MyView/ [AcceptVerbs(HttpVerbs.Get)] public PartialViewResult PartialLandlineMobile() { SiteMasterModel objSiteMaster = new SiteMasterModel(); return PartialView("PartialLandlineMobile", objSiteMaster); } [AcceptVerbs(HttpVerbs.Post)] public ActionResult PartialLandlineMobile(SiteMasterModel objSiteMaster) { if (ModelState.IsValid) { SendEmail objEmail = new SendEmail(); objEmail.SendEligibleEmail(objSiteMaster); return RedirectToAction("Thank_You", "Home", new { objSiteMaster = objSiteMaster }); //return View("../Home/Thank_You", "Site.Master", objSiteMaster); } return View(objSiteMaster);//("PartialLandlineMobile", objSiteMaster); } } }If the modelstate is not valid it only returns the partial view and nothing else it looses its site master and all other contact thats on the page.....
CPrakash82
All-Star
18284 Points
2841 Posts
Re: MVC project doesnt redirect to another view after Sending email instead it keeps sending the ...
Dec 19, 2012 12:54 PM|LINK
This is incorrect, you can not pass objects as parameter, you need to pass individual properties as route data.
Because you are returning only partial view, if you want to replace just the partial view in your main page then either you need to return the complete view along with layout or call the post method using ajax.
sp00k
Participant
1916 Points
435 Posts
Re: MVC project doesnt redirect to another view after Sending email instead it keeps sending the ...
Dec 19, 2012 01:35 PM|LINK
hmmm see what happens when they put me on f****** apple xcode for the last months :P
Harrison.Sco...
Member
331 Points
456 Posts
Re: MVC project doesnt redirect to another view after Sending email instead it keeps sending the ...
Dec 20, 2012 08:30 AM|LINK
Can you explain in a bit more detail please... im new to MVC i was thinking changing the partial view to be a part of the sitemaster as its been used on every page so it doesnt need to be a partial view does it...
Im new to MVC so please bare with.
CPrakash82
All-Star
18284 Points
2841 Posts
Re: MVC project doesnt redirect to another view after Sending email instead it keeps sending the ...
Dec 20, 2012 12:20 PM|LINK
partial views are fine but because you are trying to return it after post it will just display the partial view and not other stuff, you need to return a complete view including layout, I hope it clarifies.