Banshee Banshee - 1 year ago 72
Javascript Question

Setting property on the model object?


I am building a ASP.NET MVC site and have runned across a problem. In my project I got a modelview class that contains a couple of properties, for example :

public class myModelView
public int MyProperty1(){ get; set;}
public int MyProperty2(){ get; set;}
public int MyProperty3(){ get; set;}

This modelview class is bound to a typed view where I need to be able to set the properties. How do I do this with javascript/jquery? I have tried with Model.MyProperty1 = 1, but that does not work?


Answer Source

You cannot set server side values with javascript. You could bind those values to input fields (textboxes, hidden fields, textareas, dropdowns, ...) using HTML helpers and then using javascript you could modify the values of those input fields.

So for example if you have a hidden field:

<input type="hidden" name="foo" id="foo" value="bar" />

you could modify its value like this:

$('#foo').val('some new value');

Then when the containing form is submitted to the server the new value will be bound to your view model.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download