Is there any way to use jquery code inside razor?

15,630

Solution 1

I read it carefully and understand what you want to do. However your example is what is confusing. (it seems to everyone that answered)

What you've done in the second example should work just fine you could wrap your jQuery in a <text></text> element if that helps as well. If there's only ever one value you could just

<script type='text/javascript'>
    @{ var x = ((SelectList)ViewData["Gruplar"]).First(); } 
    $("#GroupC_grup_name").attr('value', '@x.Text');
    $("#GroupC_id").attr('value', '@x.Value');
</script>

There are any number of ways you could accomplish razor within jquery. It's basically the same as razor with html.

It you want to execute razor on the client side then that's not going to happen. Razor is executed before it gets sent to the client.

Solution 2

The code you suggest should work, the code below uses a string array instead of a SelectList but the principle should be be the same, the code below has been tested and works -

@{String[] testarr = {"hello","bye"};}
@foreach(String sel in testarr){
 @:$("#GroupC_grup_name").attr('value', '@sel');
 @:$("#GroupC_id").attr('value', '@sel');
}

Solution 3

As I understand it, you want to update the elements when a SELECT item is selected?

Create a regular select list and add the following jquery code:

<script type="text/javascript">
$(function () {
    $('#selectListId').change(function() {
        $selectedItem = $("option:selected", this);
        $("#GroupC_grup_name").val(selectedItem.html());
        $("#GroupC_id").val(selectedItem.val());
    });

});
</script>
Share:
15,630

Related videos on Youtube

Berker Yüceer
Author by

Berker Yüceer

Computer science associate class programmer.. I'm still a learner and ever will be. Mostly using Javascript, Node.js, jQuery, C#, .Net MVC.. but some times I also head for Die Hard VB when needed. I have experience with IBM iSeries AS400 system and SQL, Mongo, SOAP, RabbitMQ. I also have experience on Stocking and Certificate programs. Usually working with WebServices and most of time spend on FrontEnd. Really like debugging.. It helps me to understand others style/way of thinking. Till now I've done all the projects only by my self but I really wanna get into team projects. Reason I've been alone so far is; I was the only guy that knew other programming languages/systems than the teams I worked with so that caused me to work in FullStack mode.

Updated on June 04, 2022

Comments

  • Berker Yüceer
    Berker Yüceer over 1 year

    Here is my jQuery code with Razor:

        $("#GroupC_grup_name").attr('value', '@foreach (SelectListItem sel in (SelectList)ViewData["Gruplar"]){ @sel.Text }')
        $("#GroupC_id").attr('value', '@foreach(SelectListItem sel in (SelectList)ViewData["Gruplar"]){ @sel.Value }')
    

    I want to merge these together, as in I dont want to call for ViewData["Gruplar"] twice.

    Can I do something like this?

     @foreach(SelectListItem sel in (SelectList)ViewData["Gruplar"]){
         @:$("#GroupC_grup_name").attr('value', '@sel.Text');
         @:$("#GroupC_id").attr('value', '@sel.Value');
     }
    

    I've tried everything I can think of but still can't achieve what I want.

  • jgauffin
    jgauffin about 12 years
    I don't think that he wanted to overwrite valueattribute for #GroupC_grup_name X number of times..
  • Berker Yüceer
    Berker Yüceer about 12 years
    ur code is worthy but this is not my issue! umm i think im being misunderstood. selectlist has nothing to do but getting my values here.. im trying to add jquery code inside razor thats my issue is there a way to do that? its not important if i use a selectlist or a model to retrive some data.
  • Berker Yüceer
    Berker Yüceer about 12 years
    first of all i will not overwrite with the action that im gonna do! Also my selectlist only for carrying data value and text fields seperately, its a list yes but it only contains 1 item. So what i was trying is injecting some jquery code inside razor and thanks @ipr101 its working with a string array with some overwriting as @jgauffin said it overwrites! so it should be like (@sel.Value to GroupC_id and @sel.Text to GroupC_grup_name) also if u can show a way to do it with a data carrier like ViewData["Gruplar"] i'd like to know it.
  • jgauffin
    jgauffin about 12 years
    It's very important to know what you want to do since Razor code is run in the webserver and the jQuery code is run in the webbrowser. they can not work together. Razor can only generate jQuery code (that later will be run in the browser)