0

I have a view which is nothing more than several hundred text boxes that are all generated from a buncha TextBoxFor's. The original model is populated by deserializeing a json blob (string) saved in the DB. The user has the ability to change any one (or all) of the values, and when the Save button is clicked, I want to just return all the textbox values to a Json string and save that string back to the DB.

In the view...

function SaveAll() {
    var model = '@Html.Raw(Json.Encode(Model))';
    // Send the string to the server ...
}

While that does a great job of taking the model as delivered to the View and making a Json string out of it, it does not reflect any user changes to the model after it was delivered to the view.

I am REALLY hoping there is a simple way to get all the current textbox values and turn them into a Json string, without having to reference all hundred+ of them individually, and, of course, to maintain the structure of the original model.

Here's a sample of one of the Text Boxes after being rendered into HTML:

<input 
    class="form-control input-sm"
    id="LeadPricingModel_CampaignType_Exclusive"
    name="LeadPricingModel.CampaignType.Exclusive" 
    value="25">

The name, LeadPricingModel.CampaignType.Exclusive, represents the structure of the model.

Casey Crookston
  • 13,016
  • 24
  • 107
  • 193

1 Answers1

0

You cannot manipulate your c# model from JavaScript.Your model rendered by the server. Only once. When your page is created. To send data from your form or etc. use JSON.stringify(object)

blckt
  • 81
  • 8
  • "You cannot manipulate your c# model from JavaScript" Yes, of course. And that's not what I'm trying to do. I just want to get a Json string that is structured like the model, but contains all of the most recent values. – Casey Crookston Jan 23 '17 at 16:54