• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 70
  • Last Modified:

Javascript and asp.net mvc

I Guys,

I'm generating bar-code in my asp.net mvc application and I'm using jquery to do so.
Here is the plugin i'm using - jquery-barcode.js.

Now I'm returning list of itemlookup fields from the database and would like to show their barcode in the view.

Here is the example what I'm doing:

Controller
 public ActionResult Index()
        {
            CodeprojectEntities db = new CodeprojectEntities();
            var br = db.Items.Select(a => a.ItemLookupCode).Take(10).ToList();
            ViewBag.barcode = br;

            return View();
        }

Open in new window



view:
<div class="text-center">
    @foreach (var itm in ViewBag.barcode)
    {
        <div class="row">
            <div id="bcTarget" class="col-md-12">
                <input type="hidden" id="barid" value="@itm" />

            </div>
        </div>
    }
</div>


@section scripts{
<script src="~/Scripts/jquery-barcode.js"></script>
    <script>
        $(document).ready(function () {
            var barid = $("#barid").val();
            $("#bcTarget").barcode(barid, "code128", { barWidth: 2, barHeight: 30 });
        });
    
    </script>
}

Open in new window


I'm getting all the fields from the database but it shows just the first field in the view.
I assume the jquery function doesn't know how to read all the items which come from the foreach loop.

Please, can anybody help with this? I'm wondering if there is a solution ?

THanks,
0
Moti Mashiah
Asked:
Moti Mashiah
  • 8
  • 4
1 Solution
 
ste5anSenior DeveloperCommented:
Field? Do you mean row?

Just render your view:
<div class="text-center">
    @foreach (var itm in ViewBag.barcode)
    {
        <div class="row">
            <div id="bcTarget" class="col-md-12">
                <input type="text" id="barid" value="@itm" />
            </div>
        </div>
    }
</div>

Open in new window


How many rows do you get?
0
 
Moti Mashiah.NET DeveloperAuthor Commented:
Hi,

I think I didn't explain it right sorry.

I'm getting 10 rows from the server and sending just one column to the view(field).
Now in the view I'm getting all 10 rows and even see them, but what about when I run the jquery to make these row/fields as a barcode:

Please look at the full code I sent. I think you didn't pay attention to the concept here:
There is a jquery barcode generator:
@section scripts{
<script src="~/Scripts/jquery-barcode.js"></script>
    <script>
        $(document).ready(function () {
            var barid = $("#barid").val();
            $("#bcTarget").barcode(barid, "code128", { barWidth: 2, barHeight: 30 });
        });
    
    </script>
}

Open in new window



For now it returns just one row. I can see all the row if I just want to query them from the database without generate them to a barcode.

Please, let me know if you got my point.
0
 
Moti Mashiah.NET DeveloperAuthor Commented:
Here is the screen shot which might give you the full picture.
Untitled.png
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
ste5anSenior DeveloperCommented:
You're createing all rows with the same id. Thus you'll get only one barcode. The question is how that barcode plugin works. Somehow the barid parameter makes no sense at the first glance.

Maybe you need:

<div class="text-center">
    @foreach (var itm in ViewBag.barcode)
    {
        <div class="row">
            <div class="bcTarget" class="col-md-12">
                <input type="text" class="barid" value="@itm" />
            </div>
        </div>
    }
</div>

Open in new window


with

        $(document).ready(function () {
            $("barid").barcode($(this), "code128", { barWidth: 2, barHeight: 30 });
        });

Open in new window

0
 
Moti Mashiah.NET DeveloperAuthor Commented:
I'm getting the 10 row just fine the only issue is when I generate barcode from the plugin.

Please  - see this link http://barcode-coder.com/en/barcode-jquery-plugin-201.html
0
 
Moti Mashiah.NET DeveloperAuthor Commented:
I found something like this:
Still didn't work.
 $(document).ready(function () {

            $("#barid").each(function () {
                var b = $(this).val()
                $(this).barcode(b, "code128", { barWidth: 2, barHeight: 30 }).last().text();
                $(this).next("#bcTarget").text(b);
            }); 
        });

Open in new window

0
 
ste5anSenior DeveloperCommented:
Seems the plugin is a little bit picky. This works:

<!DOCTYPE html>
<html>
<body>
	<script type="text/javascript" src="jquery-3.1.0.min.js"></script>
	<script type="text/javascript" src="jquery-barcode.min.js"></script>
	<script type="text/javascript">
		$(document).ready(function () {					
			$(".barid").each(function(){
				$(this).barcode($.text(this),"code128",{barWidth:2,barHeight:30});
			});			
		});
	</script>
	<div class="barid">11111</div>
	<div class="barid">2222</div>
	<div class="barid">333</div>
</body>
</html>

Open in new window

0
 
Moti Mashiah.NET DeveloperAuthor Commented:
Didn't work for me I need to return to id="bcTarget" the id="barid" just give the value but the return barcode should go to: bcTarget

 <div class="row">
            <div id="bcTarget" class="col-md-12">
                <input type="hidden" id="barid" value="@itm" />
            </div>
        </div>
0
 
Moti Mashiah.NET DeveloperAuthor Commented:
I feel we are almost there :).
0
 
Moti Mashiah.NET DeveloperAuthor Commented:
Thank you for help soo far.
0
 
ste5anSenior DeveloperCommented:
No! Each element must have a unique id. But you can use a cascaded selector:

<!DOCTYPE html>
<html>
<body>
	<script type="text/javascript" src="jquery-3.1.0.min.js"></script>
	<script type="text/javascript" src="jquery-barcode.min.js"></script>
	<script type="text/javascript">
		$(document).ready(function () {					
			$("#barcodes .barid").each(function(){
				$(this).barcode($.text(this),"code128",{barWidth:2,barHeight:30});
			});			
		});
	</script>
	<div id="barcodes">
		<div class="barid">11111</div>
		<div class="barid">2222</div>
		<div class="barid">333</div>
	</div>
	<div id="no-barcodes">
		<div class="barid">11111</div>
		<div class="barid">2222</div>
		<div class="barid">333</div>
	</div>
</body>
</html>

Open in new window


So your view should look like:

<div id="barcodes" class="text-center">
    @foreach (var itm in ViewBag.barcode)
    {
        <div class="row">
            <div class="col-md-12">
                <div class="barid">@itm</div>
            </div>
        </div>
    }
</div>

Open in new window

0
 
Moti Mashiah.NET DeveloperAuthor Commented:
Thank you sooo much.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 8
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now