-1

This seems to be a common question. I have looked at many examples however I must be missing something somewhere. Below is my code for the 'ViewModel', and 'Controller'.

ViewModel:

    public class EditAddressViewModel
{
    public Guid AddressUI { get; set; }
    [Display(Name = "Billing Address?")]
    [UIHint("_IsStatus")]
    public bool IsBilling { get; set; }
    [Display(Name = "Shipping Address?")]
    [UIHint("_IsStatus")]
    public bool IsShipping { get; set; }
    [Display(Name = "Location Name")]
    public string LocationName { get; set; }
    [Display(Name = "Contact Name")]
    public string ContactName { get; set; }
    [Display(Name = "Address")]
    public string Line1 { get; set; }
    [Display(Name = "Address 2")]
    public string Line2 { get; set; }
    [Display(Name = "Country")]
    public int Country { get; set; }
    [Display(Name = "State")]
    public int State { get; set; }
    [Display(Name = "City")]
    public int City { get; set; }
    [Display(Name = "ZipCode")]
    public string ZipCode { get; set; }
    [Display(Name = "Contact Email")]
    [DataType(DataType.EmailAddress)]
    [StringLength(320)]
    [RegularExpression(@"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", ErrorMessage = "Enter a valid email address")]
    public string EmailAddress { get; set; }
    [Display(Name = "Phone Number")]
    [DataType(DataType.PhoneNumber)]
    [StringLength(12)]
    [RegularExpression(@"((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}", ErrorMessage = "Enter a valid phone number")]
    public string PhoneNumber { get; set; }
    [Display(Name = "Fax Number")]
    [DataType(DataType.PhoneNumber)]
    [StringLength(12)]
    [RegularExpression(@"((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}", ErrorMessage = "Enter a valid phone number")]
    public string FaxNumber { get; set; }
    public int CompanyId { get; set; }

    [Display(Name = "Select Country")]
    public int CountryId { get; set; }
    public IEnumerable<SelectListItem> Countries { get; set; }

    [Display(Name = "Select State")]
    public int StateId { get; set;  }
    public IEnumerable<SelectListItem> States { get; set; }

    [Display(Name = "Select Cit;y")]
    public int CityId { get; set; }
    public IEnumerable<SelectListItem> Cities { get; set; }

}

The Controller:

        // Customer Input
    // GET: Addresses/Edit/5
    [Authorize(Roles = "CompanyAdmin")]
    public ActionResult UserEdit(Guid guid)
    {
        if (guid == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Addresses addresses = db.Addresses.Find(guid);
        if (addresses == null)
        {
            return HttpNotFound();
        }

        EditAddressViewModel editAddress = new EditAddressViewModel()
        {
            AddressUI = addresses.AddressUI,
            LocationName = addresses.LocationName,
            Line1 = addresses.Line1,
            Line2 = addresses.Line2,
            Country = addresses.Country,
            State = addresses.State,
            City = addresses.City,
            ZipCode = addresses.ZipCode,
            PhoneNumber = addresses.PhoneNumber,
            FaxNumber = addresses.FaxNumber,
            CompanyId = addresses.CompanyId
        };
        ConfigureViewModel(editAddress);
        return View(editAddress);
    }

    public void ConfigureViewModel(EditAddressViewModel editAddressViewModel)
    {
        editAddressViewModel.Countries = db.Countries.Select(o => new SelectListItem()
        {
            Value = o.CountryId.ToString(),
            Text = o.CountryName
        });

        editAddressViewModel.States = db.States.Select(o => new SelectListItem()
        {
            Value = o.StateId.ToString(),
            Text = o.StateName
        });

        editAddressViewModel.Cities = db.Cities.Select(o => new SelectListItem()
        {
            Value = o.CityId.ToString(),
            Text = o.CityName
        });
    }

    // POST: Addresses/Edit/5
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult UserEdit(EditAddressViewModel model)
    {
        var userId = User.Identity.GetUserId();
        if (!ModelState.IsValid)
        {
            return View(model);
        }
        Addresses addresses = db.Addresses.Find(model.AddressUI);
        addresses.IsBilling = EditAddressViewModel.IsBilling;
        addresses.IsShipping = EditAddressViewModel.IsShipping;
        addresses.LocationName = EditAddressViewModel.LocationName;
        addresses.Line1 = EditAddressViewModel.Line1;
        addresses.Line2 = EditAddressViewModel.Line2;
        addresses.Country = EditAddressViewModel.Country;
        addresses.State = EditAddressViewModel.State;
        addresses.City = EditAddressViewModel.City;
        addresses.ZipCode = EditAddressViewModel.ZipCode;
        addresses.PhoneNumber = EditAddressViewModel.PhoneNumber;
        addresses.FaxNumber = EditAddressViewModel.FaxNumber;
        addresses.CompanyId = EditAddressViewModel.CompanyId;

        db.Entry(addresses).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("Index", "Customer", new { UserId = userId });

    }

The issue I am running into is in the controller. All of the entries for Example

addresses.IsBilling = EditAddressViewModel.IsBilling;

Show that "An object reference is required for the non-static field, method, or property". If I go to my 'ViewModel' and change the "public int or string" to be "public static int" then it goes away but then I get an error on the get Action that is cannot be tied to a static object. I am a little confused because this same scenario seems to work for another question on this site. In mine it does not. I have to be missing something somewhere. thanks for your help.

S.Purtan
  • 125
  • 1
  • 2
  • 11
  • `EditAddressViewModel` is the class(type) name. You need to use the object to read the properties of that. Use `addresses.IsBilling = model.IsBilling;` – Shyju Dec 09 '18 at 01:25

1 Answers1

1

When you say addresses.IsBilling = EditAddressViewModel.IsBilling; you are asking for a property of the class EditAddressViewModel You want to access a property of the object model. So I think you want addresses.IsBilling = model.IsBilling; etc.

Eric
  • 4,201
  • 5
  • 27
  • 36
  • 1
    While the answer is technically correct, having it only makes this question not auto-deletable when the question itself provides 0 value to the site. Typo questions => comment and vote-to-close/flag – Camilo Terevinto Dec 09 '18 at 01:28
  • 1
    @CamiloTerevinto If this site is for people who are struggling with issues and need help then I believe that the question is Valuable. Especially when this same scenario is answered on this site and work but in my case it does not. So is the answer that is marked correct on the other question really valid? Why in their case is it correct to call from the 'ViewModel' and set up the same way correct and work. But in mine it does not. Help is what people want and need, not someone telling people that is doesn't give value to the site. The site is important but it is here for a reason! Regards – S.Purtan Dec 09 '18 at 01:53
  • 1
    @S.Purtan That has nothing to do with the fact that the question is what is known here as a Typo question. Should you actually look into your code, you should be able to determine what's a variable and what's a class. There *is* a Close reason exactly for this – Camilo Terevinto Dec 09 '18 at 02:02
  • Sorry I guess some people just aren't as smart as you are! As beginners learning a new language it can get away from you. Maybe some day when I am a seasoned veteran like you, I will look down on those who are learning as well. – S.Purtan Dec 09 '18 at 05:35