mboyd4546
mboyd4546

Reputation: 23

MVC Model Object losing value

ok... I'm Stumped

    public ActionResult addSite(SiteViewModel aModel)
    {
        if (ModelState.IsValid)
        {
            siteID = Guid.NewGuid().ToString();
            aModel.siteId = siteID;

            AddSite2Azure();

            return RedirectToAction("manageProfile", "User");
        }
        else { return View(aModel); }
    }


    private void AddSite2Azure()
    {
        EmPmSiteEntity aSite = aEnty.AssetRegistry.CreateSite(new EmPmSiteEntity()
        {
            UserId = aUserId,
            Id = aModel.siteId,
            Name = aModel.siteName,
            ZipCode = aModel.siteZip,
        });
    }

When Debugging, aModel.siteID has a guid at the end of my actionResult. But when we get to the next method, the value of aModel.siteID is "null"

Upvotes: 1

Views: 258

Answers (1)

Rob Bell
Rob Bell

Reputation: 3675

It appears as though you have two scopes for aModel - one at the class level (not shown in your code), and one at the method level (passed as a parameter to addSite(...)).

You're setting the value of the method-level variable in addSite(). To use this value in AddSite2Azure(), either pass the method-level aModel to AddSite2Azure(), or set the class-level aModel in addSite() by using this.aModel.

Upvotes: 1

Related Questions