0

I want to submit a dropdown value disabled:

View:

@Html.DropDownFor(model => model.Type_Id, Model.TypeDropDown)
@Html.HiddenFor(m => m.Type_Id)

Javascript:

$("#Type_Id").val("67").change();
document.getElementById("Type_Id").disabled = true;
$('#Type_Id').val(67);

But my value dont passed to c# controller.

3 Answers3

0

Once you disable a dropdown it wont submit it's value, so you need to Store your value.

See : Disable Dropdown

use a hiddenfor with nameparameter to store the value.

0

use hidden field to store the value of the disabled dropdown . In your code you have to manually set the value of the hidden field which is inside the same form before posting it to controller like below . You have to add the "Type_id_hidden" property in your model to access the value in controller.

@Html.Hidden("Type_id_hidden") .

But in your code you used the same id both hidden field and dropdown. set the hidden field before posting to controller

var selectedVal = $("#Type_Id").val();
$("#Type_id_hidden").val(selectedVal);
Niladri
  • 5,832
  • 2
  • 23
  • 41
  • I dont understand. I need to add new property in my model? long Type_id_hidden ? My hidden field needs to have same ID as dropdown? – Joao Rafael Sep 11 '17 at 10:13
  • add a new property `int Type_id_hidden` in your model which is you are binding in controller . in your code you have same id for hidden field and dropdown hence when you set the value using jquery it gets confused and the hidden field value is never sent to the controller – Niladri Sep 11 '17 at 10:20
  • you should have unique id for hidden field and dropdown to use jquery – Niladri Sep 11 '17 at 10:20
  • I have other problem with your solution. I use automapper to map my fields. Its safe mapping my entity to new field id? – Joao Rafael Sep 11 '17 at 10:25
  • Not sure if automapper has some other configuration but in general if you have a property with the same name as in UI and model they are binded by default during model binding. Once automapper has completed the model binding you can assign the value of "Type_id_hidden" to "Type_Id" in your model class. – Niladri Sep 11 '17 at 10:57
0

Use an additional hidden field with the same value as drop-down to submit the value and let the dropdown just for showing the value.

You cannot submit the value of a disabled field.

Ipsit Gaur
  • 2,872
  • 1
  • 23
  • 38