I am trying to save the Identities UserID in a create action.

Controller GET request is as follows:

// GET: Owners/Create
public ActionResult Create()
{
    ViewBag.RegUser = User.Identity.GetUserId();
    return View();
}

View is as follows:

@Html.HiddenFor(model => model.RegUserID, new { @value = ViewBag.RegUser })

Controller POST request is as follows:

// POST: Owners/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for 
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "OwnerID,OwnerName,ContactName,PhysicalAddress1,PhysicalAddress2,PhysicalCity,PhysicalState,PhysicalCountry,PhysicalPostCode,PostalAddress1,PostalAddress2,PostalCity,PostalState,PostalCountry,PostalPostCode,Phone,Mobile,Fax,Email,RegUserID")] Owner owner)
{
    if (ModelState.IsValid)
    {
        db.Owners.Add(owner);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(owner);
}

However when the record is saved the RegUserID is null.

If I break on the @Html Helper were the value is assigned to model.RegUserID I can see the UserID in a view:

ViewBag.RegUser "7318611e-7e2e-4ee2-9c7b-51b20f0806d8"  dynamic {string}

What am I doing wrong?

Related posts

Recent Viewed