Access property of an object of type [Model] in JQuery

744 views Asked by At

So in my MVC project I have a custom Model called Survey, which contains a handful of properties. In the Survey Controller, I am saving Survey to a Session variable, so that the values of the survey's properties are persisted per session.

I want to be able to manipulate the DOM of a View based on the values of the session survey's properties. But I'm having trouble with how to access those.

I did find this relatively recent question that seems very similar but doesn't have an answer: Cannot access properties of model in javascript

Here's what I have so far: In the View I am getting the session's survey like so:

     <input type="hidden" name="activeS" value="@HttpContext.Current.Session("Survey")" />

Then in the Section Scripts at the bottom I have this script to get that value and do something with it:

 @Section Scripts
@Scripts.Render("~/bundles/jqueryval")

<script type="text/javascript">
    $(function () {
        var survey = $("[name=activeS]").val();
        $("[name=mddbccu][value=" + survey.mddbccu + "]").prop('checked', true);
    })
</script>

 End Section

If I insert "alert(survey);" after "var survey..." It does give me an alert that displays the type that the survey object is. So it looks like the survey is being retrieved fine. But if I try "alert(survey.mddbccu);" the alert simply says "undefined".

Note that the line after that ("$([name=mddbccu]...") I know works - having previously set a variable to a specific value, using that the appropriate item is checked. But in attempting to get the value of this particular property of the survey, nothing is checked.

So how do I get the values of the survey's properties here? Thank you!

1

There are 1 answers

1
Brad C On BEST ANSWER

Your approach would work with some hackery and workarounds but it is not in the spirit of MVC. Here is how you could accomplish it in the MVC way. Basically you move all the heavy lifting (parsing the item from the session) 0 to the controller and store the results in a ViewModel. This keeps the logic out of the view and makes for much cleaner and easier to maintain code.

If you have a ViewModel:

public ActionResult Survey()
{
    SurveyViewModel model = new SurveyViewModel();

    Survey surveySession = HttpContext.Current.Session("Survey") as Survey; // youll have to do extra null checks and such here

    // map other properties from the survey object retrieved from the session to your viewmodel here!
    model.mddbccu = surveySession.mddbccu;
    model.otherProperty = surveySession.otherProperty

    return View(model);
}

If you are just using the Survey object as the model inside the view then its even simpler:

public ActionResult Survey()
{
    Survey model = HttpContext.Current.Session("Survey") as Survey;
    return View(model);
}

Then, MVC magically selects stuff for you depending on what you have set in the controller. If you are using @RadioButtonFor(m => m.mddbccu, "three") then the radio will be selected if the value "three" was put into the property mddbccu in the controller.