locked
How to read the model m=>m.value1 values for the entire server side model in javascript/jquery? RRS feed

  • Question

  • User2142845853 posted

    hi

    I have a details page in MVC with about 1000 <dt><dd> table entries. So many that I want to 'hide' the ones that don't have data in them. It appears impossible with the <dl> <dt> <dd> </dl> setup because even if I add <dd id="myvalue1"> sdfascy3423 </dd> and try to use the reference 

    var someval1 = document.getElementById("myvalue1");

    if I try to extract the value with

     var somedata1 = someval1.value;

    Nothing happens, except we see in F12 that it failed, someval1.value is undefined.  Cannot get the data value from the dt/dd no matter what. Tried using reference[0].innerhtml or? undefined.

    I dont want to have to go into each of the 1000 <dd>'s and add a unique ID, the goal is just to see if the value of the model data is null or not. So now it becomes how to reference the model data in the page and do this test?  

    For other pages I built a foreach loop using keyword SOME, which worked great to find nulls, made an array of the model value names and passed them in using SOME to the function, returning whether that array was all null or if at least one model was found with data.  

    Assumed this would work in the DETAILS page, but it does not. Found I cant even get data to test.  Another possible way would be to try 

    var model = @Html.Raw(Json.Encode(Model));

    And then use some kind of code to iterate each group, just cant seem to read any data out

    Sunday, February 18, 2018 4:04 PM

Answers

  • User-474980206 posted

    only form elements (input, select, textarea) have a value property. all other dom elements have child nodes. you can convert the child nodes to a string via the innerHTML, innerText or textContent properties. only form elements are included post back.

    rendering the viewmodel as javascript model:

    var model = @Html.Raw(Json.Encode(Model));

    is perfectly valid (though I'd use newtonsoft rather than the webhelpers), and probably a better approach if you need to know null vs blank. you need to know if camel case is set to know if the propertyNames start with upper or lower case. all collections will be javascript arrays, and dictionaries will be mapped objects.  

    a simple view source will give you all the answers. but 

      @Model.Value1 

    translates to the javascript

      model.Value1

     

      

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 18, 2018 6:52 PM