add custom properties to knockout model object - asp.net-mvc

I am working with knockout and mvc. I make a call to an mvc controller to retrieve json that I create a model object from and apply it to a div:
myModel = ko.mapping.fromJS(items);
ko.applyBindings(myModel , document.getElementById("my-container"));
I am wondering how can I extend my myModel object (which is populate from json from the controller) to have custom properties based upon values on the object returned from my controller?

Generally you would want to use the create callback as described in this section of the documentation: http://knockoutjs.com/documentation/plugins-mapping.html#customizing_object_construction_using_create
It is also possible to simply add properties to myModel after creating it via the mapping plugin in your example.

Related

How to disable model binding for one action method in asp.net mvc

We are using custom model binders, how to disable model binding for one action method, so that i can directly post data from angular http post request,
I have taken different/new class as parameter for action method.
but due to custom model binders loosing posted data.
I don't think you can disable custom model binding for an action, but you can do it by (model) class type.
I'm guessing you've got something like this:
ModelBinders.Binders.DefaultBinder = new CustomModelBinderClass();
The Binders Property is a dictionary mapping (model) types to model binder classes, so if you can use a unique model type in this one scenario, you should be able to change back to the default binder for that model type like this:
var defaultBinder = new DefaultModelBinder();
ModelBinders.Binders.Add(typeof(ModelThatNeedsDefaultBinding), defaultBinder);

How to fetch xml file data to grid in mvc application

I need to create a mvc application which contain on grid view, that contains the data of the xml file, so I need to read xml file data and display that data in grid using mvc
We cannot write code for that. But I can suggest you that use XMLSerialization and map all XML data to Property Class and pass object of that property class to view as a model.
And in your view you can simply use all properties of that model.
References:
How to serialize and deserialize XML
List Model Binding in MVC

How to make a RequiredAttribute for create not for edit of model

I have a model containing a property of HttpPostedFileBase type, and I have created a custom validation attribute which implements RequiredAttribute to make some validations. It works perfectly when you create the model. However I don't want to make any validation when you edit the model (optional HttpPostedFileBase property when you edit the model). How do I make it?
Your attribute is simple metadata added to a property of a class. This attribute has no information about what you do with the class, i.e. if you're using this class to edit or to create a new entity.
So, the answer is that you cannot do that directly in the attribute. So the only solution is to "bypass" the attribute where you're using it, if you're editing the value. There are several alternatives:
use two different models, one for editing, the other for creating. And decorate the property only in the model for creating
remove the validation, or the error, using some code to do it: for example, you can remove the property error from the ModelState in a post action for the Edit action.
Obviously the easiest is 1. You can use a base class with all the fields, but those which have different treatement, and then inherit it for creation or edition. It's not strange to represent the same data with different models when you use MVC: a model for viewing, a model for showing the editor, a model for receiving the result of an edition... so, using several different models is not a problem at all. You can use AutoMapper or ValueInjecter to simplify moving data between entities and models.

Issue with Html.EditorFor on MVC3

I have a view with a form that's is typed to a viewmodel called AddEditItemVM. This viewmodel has the following property:
public List<Category> Categories{get;set;}
What im trying is two things:
Render a checkbox foreach Category in the generic category list of my viewmodel.
Make that when the form is posted receive, in my controller action, the property Categories instantiated (into the instance of AddEditItemVM)
About the first point, i would like to use any helper (if exists) that renders a group of checkboxes using lambda expressions like (m=>m.Categories), instead to render the checkbox with a foreach into the view.
About the second point, i read that there is one feature in MVC called Custom Model Binders. These get values from ValueProviders (querystring, cookies, or Form values) and creates the necessary instances passing it to specific action called after a form was posted. Should i create one custom model binder in order to receive my property Categories instantiated?
This might work
CheckboxList in MVC3.0

Posting multiple values using MVC

I have a model with a property that points to a file that contains HTML. My strongly typed view to this model uses a custom HTML helper method to resolve and return the HTML from the file. Works great so far.
The HTML read from each file will contain various controls whose values I need to retrieve when the form is POSTed.
What would be the best way to have access to the POSTed control values in my controller method?
I would prefer a non jQuery solution, but I am not sure if the MVC framework can provide these values to me? Can it provide a list of key/value pairs to the controller somehow?
You could use the FormCollection in ASP.NET MVC.
public ActionResult SomeAction(FormCollection form) {
...
}
You have essentially two options.
1) Use the old fashioned Request variables as all we have done in ASP.NET web forms.
For example in your controller action method you can retrieve any value present on the form with the following method
public ActionResult SomeAction() {
var request = this.ControllerContext.HttpContext.Request;
bool boolParam = bool.Parse( request["boolParam"] ?? "false" );
}
2) Create a custom Model Binder to let the framework pack those values in a custom class object.
This method would be a little bit more difficult at the beginning because you have to create a custom Model Binder but it favour readability on your controller code. For further details on creating custom model binders give a look at the following links (you can find more with a simple search)
Custom Model Binder for Complex composite objects
Custom Model Binder and More UI Validation in ASP.NET MVC
A Custom ASP.NET MVC Model Binder for Repositories
Hope it helps
Is the content of the HTML files dynamic or known at design time? If you know it now, you could have each one post to it's own action and then strongly type the parameters.

Resources