Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Jul 21, 2011 06:51 PM by bhsstudio
Member
68 Points
88 Posts
Jul 21, 2011 07:28 AM|LINK
I have a simple question.
I have the following html code
<a href="#" class="purchase"> <span>Purchase</span><em>@string.Format("{0:c}", Model.thisItem.ItemSalePrice)</em> </a>
But I want to use a Ajax.Actionlink to make it as a ajax post.
I have the following updated code
@Ajax.ActionLink("Purchase", "AddToCart", "Order", new { ItemID = Model.thisItem.ItemID }, new AjaxOptions { UpdateTargetId = "content" }, new { @class = "purchase" })
The question is how I can put the <span> tag into the actionlink?
Thank you everyone.
I understand that if I use something like
<a href="@Url.Action("AddToCart", "Order", new { ItemID = Model.thisItem.ItemID, ItemGUID=Model.thisItem.ItemCID })" class="purchase"> <span>Purchase</span><em>@string.Format("{0:c}", Model.thisItem.ItemSalePrice)</em> </a>
It will work, but I guess it is just a regular form posting, isn't it? If I want a ajax post, what should I do? Thanks again
All-Star
24675 Points
4250 Posts
Jul 21, 2011 07:33 AM|LINK
In the AjaxOptions set the HttpMethod to "Post"
new AjaxOptions { UpdateTargetId = "content", HttpMethod = "Post" },
Jul 21, 2011 07:46 AM|LINK
Hi, thank you for your reply. The question is where should I put the <span> tag when I am using Ajax.ActionLink? Thanks
Contributor
2427 Points
369 Posts
Jul 21, 2011 08:10 AM|LINK
One way is to write your own extension method.
using System.Web.Mvc; using System.Web.Mvc.Ajax; namespace HelperExtensionNameSpace { public static class HelperExtensions { public static MvcHtmlString RawActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, string controllerName, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes) { var repID = Guid.NewGuid().ToString(); var lnk = ajaxHelper.ActionLink(repID, actionName, controllerName, routeValues, ajaxOptions, htmlAttributes); return MvcHtmlString.Create(lnk.ToString().Replace(repID, linkText)); } } }
And in the view, use the below code. (Don't forget to include the namespace you have used for HelperExtensions class in to view)
@using HelperExtensionNameSpace @Ajax.RawActionLink(string.Format("<span>Purchase</span><em>{0:c}</em>", Model.thisItem.ItemSalePrice), "AddToCart", "Order", new { ItemID = Model.thisItem.ItemID }, new AjaxOptions { UpdateTargetId = "content" }, new { @class = "purchase" })
Jul 21, 2011 06:51 PM|LINK
This answer is wonderful, thanks a lot! I learn something new!
bhsstudio
Member
68 Points
88 Posts
Quick question about Ajax.ActionLink and span
Jul 21, 2011 07:28 AM|LINK
I have a simple question.
I have the following html code
<a href="#" class="purchase"> <span>Purchase</span><em>@string.Format("{0:c}", Model.thisItem.ItemSalePrice)</em> </a>But I want to use a Ajax.Actionlink to make it as a ajax post.
I have the following updated code
@Ajax.ActionLink("Purchase", "AddToCart", "Order", new { ItemID = Model.thisItem.ItemID }, new AjaxOptions { UpdateTargetId = "content" }, new { @class = "purchase" })The question is how I can put the <span> tag into the actionlink?
Thank you everyone.
I understand that if I use something like
<a href="@Url.Action("AddToCart", "Order", new { ItemID = Model.thisItem.ItemID, ItemGUID=Model.thisItem.ItemCID })" class="purchase"> <span>Purchase</span><em>@string.Format("{0:c}", Model.thisItem.ItemSalePrice)</em> </a>It will work, but I guess it is just a regular form posting, isn't it? If I want a ajax post, what should I do? Thanks again
raduenuca
All-Star
24675 Points
4250 Posts
Re: Quick question about Ajax.ActionLink and span
Jul 21, 2011 07:33 AM|LINK
In the AjaxOptions set the HttpMethod to "Post"
new AjaxOptions { UpdateTargetId = "content", HttpMethod = "Post" },
Radu Enuca | Blog
bhsstudio
Member
68 Points
88 Posts
Re: Quick question about Ajax.ActionLink and span
Jul 21, 2011 07:46 AM|LINK
Hi, thank you for your reply. The question is where should I put the <span> tag when I am using Ajax.ActionLink? Thanks
Raja Boopath...
Contributor
2427 Points
369 Posts
Re: Quick question about Ajax.ActionLink and span
Jul 21, 2011 08:10 AM|LINK
One way is to write your own extension method.
using System.Web.Mvc; using System.Web.Mvc.Ajax; namespace HelperExtensionNameSpace { public static class HelperExtensions { public static MvcHtmlString RawActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, string controllerName, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes) { var repID = Guid.NewGuid().ToString(); var lnk = ajaxHelper.ActionLink(repID, actionName, controllerName, routeValues, ajaxOptions, htmlAttributes); return MvcHtmlString.Create(lnk.ToString().Replace(repID, linkText)); } } }And in the view, use the below code. (Don't forget to include the namespace you have used for HelperExtensions class in to view)
@using HelperExtensionNameSpace @Ajax.RawActionLink(string.Format("<span>Purchase</span><em>{0:c}</em>", Model.thisItem.ItemSalePrice), "AddToCart", "Order", new { ItemID = Model.thisItem.ItemID }, new AjaxOptions { UpdateTargetId = "content" }, new { @class = "purchase" })bhsstudio
Member
68 Points
88 Posts
Re: Quick question about Ajax.ActionLink and span
Jul 21, 2011 06:51 PM|LINK
This answer is wonderful, thanks a lot! I learn something new!