1

I have a number of inputs on my page. I would like to save the changes to the model on the input blur, so as I change the value of each input it gets saved back to server, like Google contacts.

<input id="FirstName" name="FirstName">Jack</input>

I create a blur event using jquery to post the value back to the server. It posts a structure with the name of the input, the value and an id of the entity.

$.post(url, { id: "2", key: "FirstName", value: "Jack" }, successFuction);

In my controller I have:

public ActionResult EditField(int id, string key, string value)

I then retrieve the entity using EntityFramework with the id. I then wanted to update the property on the model for the field.

var entity = _db.Get(id);
entity[key] = value;
return Content "Success";

Which I obviously can't do! The only way I can think off is multiple methods for each field so EditName, EditAddress etc. which seems wrong. I want this method to be able to handle each property of the model.

What is a better way to structure the controller instead of writing multiple methods for each individual field?

4
  • What is a good way to achieve what I want to do? -> what do you want to do? Commented Feb 27, 2012 at 22:29
  • Save the value. I don't want to have to write a method for each field. Commented Feb 27, 2012 at 22:30
  • Your question seems to be more related to the database access technology that you are using (EF I guess) and to jQuery than to ASP.NET MVC. Please tag your question appropriately. I don't see what this has to do with ASP.NET MVC at all in fact. Commented Feb 27, 2012 at 22:31
  • I wouldn't say so. The question is about how to structure a controller. How I get the data or save it isn't important. I am simply trying to change a property on a model. This could be all in memory and I would have the same problem, no? Commented Feb 27, 2012 at 22:36

2 Answers 2

2
  1. You could post your entire form (e.g. first name, last name, etc.) on each blur for any of your fields (this should be fine since you're saving all changes as the user progresses on the form anyway). Unless you're really trying to save bytes, posting the whole form seems fine.
  2. You could just post the field name and then use reflection to look up the property of your object and set the value.
Sign up to request clarification or add additional context in comments.

2 Comments

I didn't even think about posting the whole form. Does seem overkill for changing one field though. Wouldn't using reflection be really slow? Plus it seems really hacky for something I would of thought would be popular to implement
@Terry - Personally, I wouldn't use reflection and I would just post the whole form. It's intuitive and easy to implement, which is always the winning scenario for me unless there's a big performance drawback. How these two situations perform would be up to you to test, but unless you're working on a site that has a lot of throughput, either of these two solutions will perform just fine.
0

I think that you can do it if you are willing to model the entity in a general way:

public class FieldEntity {
    public int Id { get; set; }
    public string Key { get; set; }
    public string Value { get; set; }
}

Then use it inside the context like:

var fieldEntity = db.Find(id);
fieldEntity.Key = key;
fieldEntity.Value = value;
db.SaveChanges();

However, it is usually better to structure data in a way that is meaningful. In the example you describe it looks like you might have a Person and Address entity. So why not have a Person entity that has a property Address?

2 Comments

The whole point is that one method could handle all fields. So I could post { key: "Address", value: "Blah" } and update the address property of the model.
Then @Ek0nomik is pointing in the right direction for you. At a field level, you could use reflection.

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.