title: ASP-MVC: How to take an object property from a controller and pass it into view using custom html helper?
body: <p>I am struggling to find out how to take an object property from a controller, let's say I want to take the ID of an object being displayed in the view, to be used in a custom HTML Helper that I made. Then how to use this ID to be used as the value of a hidden  html tag in the view.</p>
  
  <p>Here is the html helper:</p>
  
  <pre><code>public static IHtmlString AddLink&lt;TModel&gt;(this HtmlHelper&lt;TModel&gt; htmlHelper, string linkText, string containerElement, string counterElement, string collectionProperty, Type nestedType)
          {
              var ticks = DateTime.UtcNow.Ticks;
              var nestedObject = Activator.CreateInstance(nestedType);
              var partial = htmlHelper.EditorFor(x =&gt; nestedObject).ToHtmlString().JsEncode();
              partial = partial.Replace("id=\\\"nestedObject", "id=\\\"" + collectionProperty + "_" + ticks + "_");
              partial = partial.Replace("name=\\\"nestedObject", "name=\\\"" + collectionProperty + "[" + ticks + "]");
              var js = string.Format("javascript:addNestedForm('{0}','{1}','{2}','{3}');return false;", containerElement, counterElement, ticks, partial);
              TagBuilder tb = new TagBuilder("a");
              tb.Attributes.Add("href", "#");
              tb.Attributes.Add("onclick", js);
              tb.InnerHtml = linkText;
              var tag = tb.ToString(TagRenderMode.Normal);
              return MvcHtmlString.Create(tag);
          }
  </code></pre>
  
  <p>And here is the controller method, which is an edit function of a Product object:</p>
  
  <pre><code>public ActionResult Edit(int id = 0)
          {
              //Product product = _db.Products.Find(id);
  
              Product product = _db.Products
                  .Include(i =&gt; i.Parts)
                  .Where(i =&gt; i.Id == id)
                  .SingleOrDefault();
  
              if (product == null)
              {
                  return HttpNotFound();
              }
  
              return View(product);
          }
  </code></pre>
  
  <p>And for the view, where I used the html helper function is here:</p>
  
  <pre><code>&lt;p&gt;
      @Html.AddLink("Add More Part", "#parts", ".part", "Parts", typeof(percobaan2.Models.Part))
  &lt;/p&gt;
  </code></pre>
  
  <p>Thanks in advance.</p>