MaylorTaylor
MaylorTaylor

Reputation: 5051

MVC - Add to model based on HTML attrib

I'm not sure how to go about doing this.

I'm creating custom forms where the order of the questions/items can be rearranged on the fly. Doing so will update a attribute on the item in the html code.

This is what each item looks like before any questions are 'set' to the form.

<li class="field item" order="">
  ...more html for item
</li>

and here is how it looks once someone drags the item to 'creation area'

<li class="field item" order="1">
  ...more html for item
</li>

if they were to add another item below that item it would be

<li class="field item" order="1">
  ...more html for item
</li>
<li class="field item" order="2">
  ...more html for item
</li>

Naturally, if these items were rearranged the order attribute would update as well to show the correction order.

My question is how would i set that order number to my model? I'm assuming this will require some javascript which i'm not too amazing at.

Upvotes: 0

Views: 29

Answers (1)

beautifulcoder
beautifulcoder

Reputation: 11340

You basically need an Order property inside the model.

int Order { get; set; }

Then you can:

@Html.HiddenFor(m => m[i].Order)

In JavaScript, you'll need to find like this:

var el = document.getElementById('[0].Order');

Watch for what gets loaded in the DOM to see how to find your elements in JavaScript.

Upvotes: 1

Related Questions