Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6835435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:13:55+00:00 2026-05-26T23:13:55+00:00

I have c# class say options more like AjaxOptions. public class options { public

  • 0

I have c# class say options more like AjaxOptions.

public class options
{
   public string Url {get;set;}
   public string httpMethod {get;set}
}

and a javascript function like this

function dosomething(obj)
{
   if (obj.Url!="" and obj.HttpMethod=="something")
    loadsomething();

}

Now in my Controller action

 public class mycontroller : controller
 {
    public ActionResult WhatToDo()
    {
       options obj = new options{Url="someurl"};
       return PartialView(obj);
    }


 }

in my view I need this object kind of string which i should be able to pass to my method.

@model options

<script>
   dosomething(@model.SomeFunctionToConverToString())

</script>

So I need this SomeFunctionToConverToString method which i will convert this object to string.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:13:56+00:00Added an answer on May 26, 2026 at 11:13 pm

    You should be able to use it like you would any other output of a model property in your view. Just reference the property that you want to pass in the JS function.

    @model options
    
    <script>
       dosomething('@(model.Url)');
    </script>
    

    See this post for more information on using Razor inside of JS

    EDIT – Something that might catch you is that if your URL get’s broken from the HTML encoding that Razor does using the above, you can use the @Html.Raw() function which will pass the Url property without HTML encoding it.

    <script>
       dosomething('@Html.Raw(model.Url)');
    </script>
    

    EDIT 2 – And another SO post to the rescue! You are going to most likely want to convert your model to JSON in order to use in a Javascript function. So…in order to do that – you will need something in your view model to handle a JSON object.

    public class optionsViewModel
    {
       public options Options{get;set;}
       public string JsonData{get;set;}
    }
    

    and in your controller:

    public class mycontroller : controller
     {
        public ActionResult WhatToDo()
        {
           options obj = new options{Url="someurl"};
           var myViewModel = new optionsViewModel;
           myViewModel.options = obj;
           var serializer = new JavaScriptSerializer();
           myViewModel.JsonData = serializer.Serialize(data);
           return PartialView(myViewModel);
        }
     }
    

    And finally the view:

    @model optionsViewModel
    
    <script>
       dosomething('@model.JsonData')
    
    </script>
    

    Using this method, then your function will work as expected:

    function dosomething(obj)
    {
       if (obj.Url!="" and obj.HttpMethod=="something")
        loadsomething();
    }
    

    EDIT 3 Potentially the simplest way yet? Same premise as edit 2, however this is using the View to JsonEncode the model. There are probably some good arguments on either side whether this should be done in the view, controller, or repository/service layer. However, for doing the conversion in the view…

    @model options
    
    <script>
       dosomething('@Html.Raw(Json.Encode(Model))');
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have class A with class A { final String foo() { //
say I have: class Test { public static int Hello = 5; } This
Let's say I have a class which has three properties as below. public class
Say we have class public class Entry<K, V> { public Entry(K k,V v) {
Say I run a simple single-threaded process like the one below: public class SirCountALot
Let's say I have the following class class Parent(object): Options = { 'option1': 'value1',
If I have class B : A {} I say that Class B inherited
I have a class that contains a dynamically allocated array, say class A {
Here is simplified version of my requirement I have a java class say Processor
I have a managed class library (say mylib.dll) and a 3rd party managed app

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.