1

I am using knockout and the model I'm using has alot of items in it. I am posting it to a controller like so:

ajaxRequest("post", "/api/care/saveevent?student=" + self.StudentId(),self.CurrentEvent())
                .done(function (allData) {
                    alert("ran ok");
                })
                .fail(function () {
                    alert("An error occurred");
                });

And my controller has this:

public string SaveEvent(object data, int student)
{
    return "test";
}

I want to use object so that I don't have to replicate everything in the knockout model on the controller or anywhere else so I can modify it once.

It posts ok and when debugging everything in the object data is correct. However I don't use object normally and not sure how to get it's contents as is, simply putting data.PropertyName causes intellisense errors.

What's the correct way to get its contents and is what I'm trying to do impossible?

user1166905
  • 2,612
  • 7
  • 43
  • 75

3 Answers3

4

If you want to avoid a strongly typed (C#) model class, you can use the dynamic type.

I would, however, recommend you create a class with the properties you're model binding against. You will have to make changes in two places then, true, but you get all the advantages of IntelliSense etc. (Besides, simply adding more properties to your client-side JavaScript code without modifying the server side will probably be insufficient in most cases anyway.)

Marius Schulz
  • 15,976
  • 12
  • 63
  • 97
0

If all you want is a partial update, using OData is my preferred way.

Here is some discussion about it.

Code copied from that example:

[AcceptVerbs("PATCH")]
public void Patch(int id, Delta<Person> person)
{
    var personFromDb = _personRepository.Get(id);
    person.Patch(personFromDb);
    _personRepository.Save();
}

You use the PATCH http verb to send a partial model. In this way, you reduced the network overhead while still make the best of a typed class.

When you make the ajax call, just remember to use patch verb.

$.ajax({
    url: 'api/person/1',
    type: 'PATCH',
    data: JSON.stringify(obj),
    dataType: 'json',
    contentType: 'application/json',
    success: function(callback) {            
       //handle errors, do stuff yada yada yada
    }
});

The benefit is you can pass in an obj with only the properties with changed values. But it will still bind with your typed class.

Community
  • 1
  • 1
Blaise
  • 21,314
  • 28
  • 108
  • 169
0

I'd recommend using a ViewModel to handle the data object.

Given a ViewModel, such as:

public class SaveEventViewModel
{ 
    public DateTime Start { get; set; }
    public DateTime End { get; set; }
}

Your controller would then be similar to:

public string SaveEvent(SaveEventViewModel data, int student)
{
    SaveEvent(data, student); // I am assuming this will occur
    return JsonConvert.SerializeObject(data);
}

The benefit to defining the ViewModel, is that you could then easily access all of the members of your model, from allData (client-side).

allData.Start
allData.End

Just wanted to give an alternative, as the dynamic approach will work fine - but I prefer the ViewModel approach.

(Note that JsonConvert is from the Newtonsoft.Json library)

Neil.Allen
  • 1,606
  • 1
  • 15
  • 20