1

Scenario:

A partial view has a model, I need to use the value from the model in the javascript which is in seperate .js file.

Currently, I'm using javascript inline so that the value in the model can be used, but what if, the javascript is moved to a seperate file. In this case how do I get those values.

Code

@model IEnumerable<BookSpec.DomainEntities.ContactModel.ContactDataModel>
<label for="SpecFinder">Contact</label>
<select id="SpecFinder" name="state">
@foreach (var name in Model)
{
    <option value="@name.GroupID">@name.GroupName</option>
}
</select>

<script type="text/javascript">
    $(document).ready(function () {
        $("#SpecFinder").change(function(){
            getData(this.value,'@Model.ProductID');
        });
    })
</script>

This is my current example code looks like, and I want to completely move the inline javascript to a seperate file. How can I do it, I also need the values from the model.

2
  • 1
    Add it to a data-productid attribute of the element Commented Jul 16, 2018 at 10:04
  • @StephenMuecke, Thanks for the response, It's ok for one or two, in some case, I need to use like all the properties in a model, In that case, this would not be feasible right? but this is what I thought initially. Commented Jul 16, 2018 at 10:07

2 Answers 2

2

Declare global javascript variable model on view as below. Then you can use it anywhere.

<script type="text/javascript">
    var model = @Html.Raw(Json.Encode(Model));
</script>

I will recommend to use above solution. But if you still want to not include any script in partial view then you can render the model inside any hidden div. And access its text and convert it to object using JSON.parse as below. Code like below is not a good practice and like patch work.

HTML

<div id="model" style="display:none;">
    @Html.Raw(Json.Encode(Model))
</div>

Script

var model = JSON.parse($("#model").text());
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @Karan, Is there any way not to use javascript in the partial view?
@RanjithVaradan I think Karan asked you to declare that globally (where your partial view will be incorporated) so your JS will be able to access it cause of it being in global scope. you can also assign the model object to a window object
@ZeeshanAdil I think assigning the model object to a window object is a bad idea, since using global variables like this is discouraged.
@rpfc I agree with you on this. global variables should generally be avoided.
0

I think @Karan response is a good option. But if you want no inline Javascript, you could assign your model to a hidden HTML input and then retrive the value in whatever other external JS file you want.

With something like Html.Hidden, for example:

@Html.Hidden("myModel", new Microsoft.Web.Mvc.MvcSerializer().Serialize(model, SerializationMode.Signed));

You can choose another serialization mode in the SerializationMode enum.

(Code not tried, but should be close)

2 Comments

You cannot assign a complex object to a hidden input (unless you were to serialize it)
Thank you @StephenMuecke. The complex object must be serialized (answer edited).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.