title: Best way not to repeat yourself while using X-editable in MVC ASP.net
body: <p>I have just started using a nice js library to edit form elements in place. 
  <a href="http://vitalets.github.io/x-editable/" rel="nofollow">Here</a> is the link for the library.</p>
  
  <p>It works well that you can just right in place update your form elements. However you will need to create an action for each individual form element.</p>
  
  <p>An example :</p>
  
  <pre><code>&lt;a href="#" class="EditableSection" data-type="text" data-pk="@Model.id" data-name="Name" data-url="@Url.Action("_EditName", "User", new { Model.id})" data-title="Edit Name"&gt;@Model.Name&lt;/a&gt;
  
  &lt;a href="#" class="EditableSection" data-type="text" data-pk="@Model.id" data-name="MiddleName" data-url="@Url.Action("_EditMiddleName", "User", new { Model.id})" data-title="Edit Middle Name"&gt;@Model.MiddleName&lt;/a&gt;
  
  &lt;a href="#" class="EditableSection" data-type="text" data-pk="@Model.id" data-name="SurName" data-url="@Url.Action("_EditSurName", "User", new { Model.id})" data-title="Edit SurName"&gt;@Model.SurName&lt;/a&gt;
  </code></pre>
  
  <p>In UserController I believe that I will need to have 3 different actions for each and most of the inside of the action similar apart from the the element which gets updated.</p>
  
  <p><strong>here is one of the actions</strong> :</p>
  
  <pre><code>        [HttpPost]
          public ActionResult _EditUserName(int id, int pk, string value, string name)
          {
  
              var user= this._userRep.First(o =&gt; o.id== pk);
              if (user!= null &amp;&amp; user.id== id)
              {
                  if (!string.IsNullOrEmpty(value))
                  {
  
                      user.UserName= value; //this is the only line changes from one to another action respectively   user.MiddleName = value or user.SurName = value 
  
                      this._userRep.Update(user);
                      this._userRep.SaveChanges();
                      return new HttpStatusCodeResult(HttpStatusCode.OK);
                  }
                  return Json(new { status = "error", msg = "You cannot leave blank" });
  
              }
  
              return  Json(new {status="error",msg="You cannot leave blank"});
          }
  </code></pre>
  
  <p><strong>I can see 2 options to improve make the code DRY.</strong></p>
  
  <p><strong>First</strong> one is create one action for each form element and put all the common task in a separate method to call on each action. (Still I believe too much typing and mess)</p>
  
  <p><strong>Second</strong> option what I focus on but I could not figure it out which I would like to get your help here :</p>
  
  <p>Create a common action as _EditUserDetails and use condition something below :</p>
  
  <pre><code>        [HttpPost]
          public ActionResult _EditUserDetails(int id, int pk, string value, string name)
          {
  
              var user= this._userRep.First(o =&gt; o.id== pk);
              if (user!= null &amp;&amp; user.id== id)
              {
                  if (!string.IsNullOrEmpty(value))
                  {
  
                    if(name=user.UserName.toString())
                      user.UserName= value; 
                    else if(name=user.MiddleName.toString())
                        user.MiddleName= value; 
                    else if(name=user.MiddleName.toString())
                     user.SurName= value; 
  
                      this._userRep.Update(user);
                      this._userRep.SaveChanges();
                      return new HttpStatusCodeResult(HttpStatusCode.OK);
                  }
                  return Json(new { status = "error", msg = "You cannot leave blank" });
  
              }
  
              return  Json(new {status="error",msg="You cannot leave blank"});
          }
  </code></pre>
  
  <p><strong>I cannot get the name of the user object variable name for the comparison and also I am not sure this is the correct way of checking it</strong>.</p>
  
  <p><code>if(name=user.UserName.toString())</code> // name is equal to form element name. and I know <code>user.UserName.toString()</code> returns the value instead of 'UserName'</p>
  
  <p><strong>here is instead of user.UserName value I just would like to use the name itself which is  UserName for the comparison?
  I can create a static strongly typed class to use in this conditions but do you think this is necessary?  why I cannot just use the name of the object variables for comparison?</strong></p>
  
  <p>What would be your best practise for this?</p>