Note that there are some explanatory texts on larger screens.

plurals
  1. POTracking the number of times Ajax.ActionLink was called
    primarykey
    data
    text
    <p>In brief: is it possible to track the number of times an Ajax.ActionLink method was called?</p> <p>Now for context. I've got a simple model:</p> <pre><code>public class Person { public string Name { get; set; } public List&lt;Address&gt; Addresses { get; set; } } public class Address { public string City { get; set; } public string Country { get; set; } } </code></pre> <p>So, a person can have many addresses. On the Create page, I want the user to click a button that allows them to add as many Addresses as they want, dynamically.</p> <p>I used this page as a reference in learning how to bind dynamically to a list: <a href="http://haacked.com/archive/2008/10/23/model-binding-toa-list.aspx" rel="nofollow">http://haacked.com/archive/2008/10/23/model-binding-toa-list.aspx</a>,</p> <p>With that as a reference, here are my classes:</p> <p>HomeController:</p> <pre><code> // // GET: /Home/ public ActionResult Index() { return View(); } [HttpPost] public ActionResult Create(Person p) { return View(p); } [OutputCache(NoStore = true, Duration = 0)] public ActionResult AjaxAddAddress() { TempData["key"] = DateTime.Now.Ticks.GetHashCode(); return PartialView("~/Views/Shared/EditorTemplates/Address.cshtml", new Address()); } </code></pre> <p>Index view:</p> <pre><code>@model ModelTest.Models.Person &lt;div&gt; @using (Html.BeginForm("Create", "Home")) { &lt;div&gt;Name: @Html.TextBoxFor(m =&gt; m.Name)&lt;/div&gt; &lt;div id="ajaxAddressBox"&gt;&lt;/div&gt; &lt;p&gt;@Ajax.ActionLink("Add Another Address", "AjaxAddAddress", new AjaxOptions { UpdateTargetId = "ajaxAddressBox", InsertionMode = InsertionMode.InsertAfter, HttpMethod = "GET" })&lt;/p&gt; &lt;input id="btnSubmit" type="submit" value="Create" /&gt; } &lt;/div&gt; </code></pre> <p>Create View (just to confirm the model binded okay):</p> <pre><code>@model ModelTest.Models.Person &lt;div&gt; &lt;p&gt;You entered person: @Model.Name.&lt;/p&gt; &lt;p&gt;He has @Model.Addresses.Count total addresses. @foreach (var c in Model.Addresses) { &lt;p&gt;City: @c.City, Country: @c.Country&lt;/p&gt; } &lt;/div&gt; </code></pre> <p>Address editor template:</p> <pre><code>@model ModelTest.Models.Address &lt;p&gt;&lt;input type="hidden" name="Addresses.Index" value="@TempData["key"]" /&gt; City: @Html.TextBoxFor(x =&gt; x.City, new { Name = "Addresses[" + TempData["key"] + "].City" } ) Country: @Html.TextBoxFor(x =&gt; x.Country, new { Name = "Addresses[" + TempData["key"] + "].Country" })&lt;/p&gt; </code></pre> <p>It seems to work ok, so I hope I'm doing this right so far. I'm new to MVC so please let me know if anything is totally wrong.</p> <p>But I need it to do more. Ideally, I'd like to add a label that says "Address #(index)" for each line. But more important, I need to restrict the user to only adding, eg, 5 addresses. Either way, I'd like to track the number of times that Ajax.ActionLink, or the method AjaxAddAddress was called. Plus, in the future I'll need an edit page that also requires that restriction. Thus, if an existing person has 3 addresses, they can add only 2 more.</p> <p>Any advice? It seems simple but I'm not sure how best to approach it. If I used a hidden field, how do you pass that value in Ajax.ActionLink and read it in my AjaxAddAddress method? Can you make a local client variable somehow?</p> <p>I suppose a Session variable would work, but I always get nervous using that, not sure how long it lives or how reliable it is.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload