0

I want to edit the data of a specific field. The problem I got is that the field name is a string, for example.

In my view I have the parameter like this:

<div class="editable" id="cnso">@Model.cnso</div>

I get that field and call my controller via Ajax using jeditable:

$('.editable').editable('/Req/UpdateField', {
    tooltip: 'Click to edit...',
    submitdata : {
        nreqid : function() {
            return $("#nreqid").val();
        }
    }
});

My controller got the name of the field (cnso) in the string id. The problem is to update my model. Some code of my controller.

public string UpdateField(string id, string value, int nreqid)
{
    /*
     *        id - id of the field. This value can be used on the
     *             server side to determine what property of the
     *             company should be updated.
     *     value - text that is entered in the input field.
     *    nreqid - this is additional parameter that will
     *             be added to the request. Value of this parameter
     *             is a value of the hidden field with an id "ReqId".
     */
    ModelState.Clear();

    if (ModelState.IsValid)
    {
        //db.Entry(req).State = EntityState.Modified;
        //db.SaveChanges();
        Req req = db.Req.Find(nreqid);
        req."id" = "2";  // <--- Problem here !!!
    }
}

My model object, Req, has the field cnso, and my id string has the "cnso", so how can I select the cnso from my string id?

2
  • What does this Req object look like? Is it correct to say your problem is you wish to update a field in req where that field matches your string name? Commented Apr 12, 2012 at 20:09
  • @AFinkelstein, the important part is the Object Req has the field cnso. My string id has the string "cnso" that i want to assign. Commented Apr 12, 2012 at 20:17

1 Answer 1

2

Using reflection you could do:

Type t = typeof(Req);
PropertyInfo p = t.GetProperty(id);
if(p != null)
    p.SetValue(req, "2", null);

Alternatively, if there's not too many properties, you could make a different Update action for all the properties you need. I don't know the circumstances you need this for, but updating individual properties in a table one a time might not be the best design.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.