8

Hi,

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?

BestRegards

Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116
Banshee
  • 15,376
  • 38
  • 128
  • 219

2 Answers2

24

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.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I have a model that exposes an array of `MyType`, that's got two properties, `Name` and `Value`. I want the user to be able to add and remove rows of name-value pairs, to the parent model, and it will all wait until the entire form is submitted and saved. How to?? – Shimmy Weitzhandler Apr 25 '13 at 01:07
2

You are trying to set the server-side property on the client - it won't work. Your view model exists only on the server when your view is rendered. Once the response is sent to the browser your class doesn't exist any more.

If you want to pass some data from client to server you have to:

  • post a form, or
  • make an AJAX call

Take a look at jQuery ajax method.

ViewModel is used to pass data from controller to view so the view can render HTML. After the HTML is rendered ViewModel is discarded. There is no point is setting ViewModel properties in the view as nothing will use them later on.

I believe you're coming from WebForms (UpdatePanel) background. The MVC is a totaly different concept/architecture. It works in a different way then WebForms / UpdatePanel.

Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126
  • Say that I got the following ModelView class hierarki MyClass>F>Order, I am sending the F property to a patial view, from here I can set the $('#F_Order') and get this value back to the service. I need to do the same thing in the main aspx file and to do that I have tried '#Model_F_Order' with no success? Maby I have to bind the property to a hidden field to make this work? – Banshee Mar 03 '11 at 14:00
  • What is a 'service' in 'get this value back to the service'? – Jakub Konecki Mar 03 '11 at 14:06
  • Sorry, the ModelView will be set on the service side where it will be sent to the view. The view is then sent to the client and when the client submits the defaultbinder will try to bind (in this case) to the ModelView. What I need is to set the property on this ModelView but I supose that all I need is really a hidden field bound to the proper property on the ModelView class. – Banshee Mar 03 '11 at 15:03