Tôi đã các lớp POCO sau:Mẫu Binding không làm việc
public class Location
{
public int LocationId { get; set; }
public string Name { get; set; }
public string Street { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
public string Country { get; set; }
public float? Latitude { get; set; }
public float? Longitude { get; set; }
public string PhoneNumber { get; set; }
public string EmailAddress { get; set; }
public string Website { get; set; }
public virtual ICollection<Program> Programs { get; set; }
public virtual ICollection<PlayerType> PlayerTypes { get; set; }
}
public class PlayerType
{
public int PlayerTypeId { get; set; }
public string Name { get; set; }
public int SortOrder { get; set; }
public bool IsActive { get; set; }
public virtual ICollection<Location> Locations { get; set; }
}
Và một Class Xem Mẫu
public class LocationViewModel
{
public Location Location { get; set; }
public IList<PlayerType> SelectPlayerTypes { get; set; }
public LocationViewModel()
{
Location = new Location();
}
}
Trong Tạo Mẫu của tôi, tôi đã xác định mô hình như
@model Locator.Models.LocationViewModel
Và có các trường như sau:
div class="editor-label">
@Html.LabelFor(model => model.Location.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Location.Name)
@Html.ValidationMessageFor(model => model.Location.Name)
</div>
Trong điều khiển của tôi để xử lý các POST Tôi có
[HttpPost]
public ActionResult Create(LocationViewModel location)
{
if (ModelState.IsValid) {
locationRepository.InsertOrUpdate(location.Location);
locationRepository.Save();
return RedirectToAction("Index");
}
location.SelectPlayerTypes = golferTypeRepository.All.Where(p => p.IsActive).ToList();
return View(location);
}
Vấn đề là tôi có một đối tượng Location nhưng không ai trong số các thuộc tính là để thiết lập các giá trị nhập vào biểu mẫu.
Tôi có làm gì sai ở đây không?
Cảm ơn
Tôi có thể tắt, nhưng tôi nghĩ rằng việc khởi tạo 'Vị trí' trong hàm tạo đang ném nó đi. Bạn có thể thử loại bỏ dòng đó (và khởi tạo thuộc tính bên ngoài chính mô hình) không? –
Tôi đã khởi tạo nó trong Create ActionResult (không phải bài đăng) và bây giờ khi tôi đăng bài, Vị trí là null. – Mike
Vâng, đó là những gì tôi hy vọng bạn sẽ thử. Oh well, đoán đó không phải là vấn đề. –