All I am trying to do is create a random number in the Controller and pass it to the View. However when I run the application the View only displays 'System.Random' not a generated value.
Here is my controller:
// GET: /Products/Create
public ActionResult Create()
{
Random randomID = new Random(Guid.NewGuid().GetHashCode());
randomID.Next(20, 5000);
ViewBag.random = randomID.ToString();
ViewData["random"] = randomID.ToString();
TempData["random"] = randomID.ToString();
return View();
}
I tried the ViewBag, ViewData, and TempData and they all display 'System.Random.'
Here is my View:
@model application.Models.Product
@{
ViewBag.Title = "Create Product";
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.Product_ID, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.Product_ID, new { @readonly = "readonly" })
@Html.TextBox("random", (string)@ViewBag.random, new { @readonly = true })
@ViewBag.random
@ViewData["random"]
@TempData["random"]
@Html.ValidationMessageFor(model => model.Product_ID)
</div>
</div>
I'm sorry the View is a little messy but I tried all the approaches I could find. What am I missing? I really don't want to have the change the Model. I tried Googling this for hours and nothing can solve my problem. Also is this the simplest approach to creating a random ID number for a product? Any help would be appreciated THANK YOU!
Aucun commentaire:
Enregistrer un commentaire