0

I have a ViewModel composed of 3 models. I also have a jquery script where I make a reference to a property, but this property appears in more than 1 model.

<script type="text/javascript"> $document.ready(function () { var items = "<option value='0'>Select</option>"; $('#DistrictID').html(items); }); </script> 

How can I reference that DistrictID belongs to a specific model?

like -

$('#SpecificModel.DistrictID').html(items); 

1 Answer 1

2

I am not sure what you really want to accomplish, but based on your question, you want to get the value on the property you have on your Model with 3 Models inside:

Try this on your Javascript section:

 <script type="text/javascript"> $document.ready(function () { var items = "<option value='0'>Select</option>"; var itemWithViewProperty = @Html.Raw(Json.Encode(Model.SpecificModel.DistrictID)); //Now you can use this variable 'itemWithViewProperty' and prints its value like: Console.Log(itemWithViewProperty); //But accessing it like $('#DistrictID').html(items); will not work since its an element ID you are passing here and not a variable value }); 

Make sure to specify the specific model after the Model word.

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, I don't understand how you want me to implement this. You want me to replace this line "var items = "<option value='0'>Select</option>";" for what you suggest? Can you please edit your answer to how the code would look like?
I updated my answer. Let me know if this what you want so that I can delete it if I misunderstood your question @LuisAlbertoDelgadodelaFlo

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.