I am using Asp.net MVC 3 and Json.net library.
I have view model class called FranchiseInfo with a couple of properties.
public class FranchiseInfo
{
public string FolderName { get; set; }
public string FullName { get; set; }
public List<string> TestDropDown { get; set; }
public void Initialize()
{
TestDropDown = new List<string>
{
"Test1",
"Test2"
};
}
public void SetDefaultValues()
{
FolderName = "SomeData";
FullName = "SomeOtherData";
}
}
I have a controller FranchiseData which is serializing view model data to Json.
public ActionResult FranchiseData(string network)
{
JsonNetResult jsonNetResult = new JsonNetResult { Formatting = Formatting.Indented };
FranchiseInfo franchiseInfo = new FranchiseInfo();
franchiseInfo.Initialize();
jsonNetResult.Data = franchiseInfo;
return jsonNetResult;
}
The controller has a single parameter called network. When the page first loads I want all the properties from the view model FranchiseInfo to be serialized and sent to the view. In that case the network value is null. So far, so good.
$.getJSON("Home/FranchiseData", function (data) {
// get all FranchiseInfo properties
}
When a specific event occurs I am making a new request to the same FranchiseData controller passing network parameter which binds with the contoller parameter.
onTemplateChange = function (value) {
var network = $("#networks :selected").val();
$.getJSON("Home/FranchiseData", { network: network }, function (data) {
// get only FolderName and FullName properties
});
}
Now I want to call the SetDefaultValues() method of the Franchise Info view model class which sets default values to some of my properties and send only these properties back to the view.
So basically I am using the same contoller FranchiseData for serializing view model data from FranchiseInfo. When the parameter network is null, all the properties of FranchiseInfo should be serialized (FolderName, FullName and TestDropDown). When I call the controller againg and pass a value to the network parameter, I want to call the SetDefaultValues() method, set some properties and serialize only these properties again and send them to the server (FolderName and FullName only).
Is there a way to do this? Any help will be greatly appreciated. Thank you!
Create a base class which contains the minimum amount of properties and inherit it.
Send back the base class when you want to use the less amount of properties and the subclass otherwise.