1

I am an old man learning new tricks. I come from asp.net web forms, and so I have a learning curve to MVC5, and little experience in javascript. That being said . . . here is my issue.

Each time a client makes a transaction on the form, I need to create a unique transaction number. I can think of two ways of doing this. One would be to use the unique ID in the table. However, that does not yet exist until the record is submitted from the form to the table. I guess we could query the table for a record count, and then add 1. . . but, the tutorials that involved that (online) seemed overly complicated for what I want to do. Grab a recordcount and pass it in the Viewbag seems simple enough. But I can't find a basic tutorial that does that simply enough.

And so, I moved on to JavaScript to create a random Number. And then pass that value to the appropriate html TextBoxFor. I am almost there . . . but not quite. Here is my JavaScript

                        var min = 1398799;
                        var max = 9999999;
                        var random = Math.floor(Math.random() * (+max - +min)) + +min;
                        $("#RequsitionNumber").val($("RO"+random));

I am trying to pass the value of 'random' to the object 'RequisitionNumber' What it is passing currently is "[object Object]"

I think the last line of code needs to be different.

I am also open to going back and doing things via controller . . . getting the table row count, and passing it as viewbag . . . . either is okay.

ExecChef
  • 387
  • 2
  • 13
  • `And so, I moved on to JavaScript to create a random Number. And then pass that value to the appropriate html TextBoxFor` This isn't possible. `TextBoxFor` runs **server side** (i.e. before JS is involved). You can't use JS code (that runs later) to influence server side code (that runs earlier). Why do you want to do this in JS rather than C#? – mjwills Feb 25 '19 at 20:43
  • `Each time a client makes a transaction on the form, I need to create a unique transaction number.` A common solution to this is to generate the transaction number when it is saved to the database. – mjwills Feb 25 '19 at 20:45
  • @mjwills TextBoxFor is an empty text box. When a user selects an item from the first dropdown on the form, it triggers the JS change event - which is where this number generator comes into play. If that makes sense? – ExecChef Feb 25 '19 at 20:52
  • @mjwills I am willing to do this in C# in the controller and pass the variable into the view (Viewbag) if that makes more logical sense. – ExecChef Feb 25 '19 at 20:53
  • @mjwills My life would be much easier if I could create the trans num after the record is submitted. Unfortunately, the specs are that the user can see the trans number during data entry. – ExecChef Feb 25 '19 at 20:54
  • 2
    You could use `Guid`, those are more or less [guaranteed to be unique](https://stackoverflow.com/questions/39771/is-a-guid-unique-100-of-the-time#39776) that way you won't have to hassle with your own randomness generators. – Wubbler Feb 25 '19 at 21:06
  • Thank you @Wubbler. I decided to use Guid. I stored it as a ViewBag in my controller and pass it to the view. That seems to be the best and fastest solution, as well as eliminate the hassle with my own random generator - as you suggested. – ExecChef Feb 26 '19 at 13:40

0 Answers0