这似乎是一个常见问题。我看了很多例子,但是我肯定在某处缺少了一些东西。下面是我的“ ViewModel”和“ 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; }
}
控制器:
// 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 });
}
我遇到的问题在控制器中。 “示例”的所有条目
addresses.IsBilling = EditAddressViewModel.IsBilling;
表明“非静态字段,方法或属性需要对象引用”。如果我转到“ ViewModel”并将“ public int或string”更改为“ public static int”,则它消失了,但是在get Action上出现错误,该错误无法绑定到静态对象。我有些困惑,因为这种情况似乎可以解决该站点上的另一个问题。在我看来,事实并非如此。我必须在某处丢失一些东西。谢谢您的帮助。
答案 0 :(得分:1)
说addresses.IsBilling = EditAddressViewModel.IsBilling;
时,您要的是类的属性 EditAddressViewModel
您要访问对象 model
的属性。所以我认为您想要addresses.IsBilling = model.IsBilling;
等。