わかりました、ビューにオブジェクトを作成しようとしています。オブジェクトは、クライアント ID と姓名を含む完全な住所で構成されます。ページが最初に読み込まれると、クライアント情報は正常に表示されますが、データを投稿すると (ヒット送信)、クライアント データが消えて null になり、Web ページがクラッシュします。
これが私のクラスです
public class clientAddressEdit
{
//public Clients client {get; set;}
public long clientID { get; set; }
public String firstName { get; set; }
public String lastName { get; set; }
public Address address { get; set; }
}
私のモデル
namespace VolumeV2.Models
{
public class Address
{
[Required]
public int Id { get; set; }
[DataType(DataType.Text)]
[Display(Name = "Street Address")]
public string StreetAddress { get; set; }
[DataType(DataType.Text)]
[Display(Name = "Postal Code")]
public string PostalCode { get; set; }
[DataType(DataType.Text)]
public string City {get; set; }
[DataType(DataType.Text)]
public string Province {get; set;}
public virtual Clients client { get; set; }
}
}
AddressController からの私の Create メソッド
//
// GET: /Address/Create
[HttpGet]
public ActionResult Create(long id)
{
Clients client = db.Clients.Find(id);
editor = new clientAddressEdit();
editor.clientID = client.Id;
editor.firstName = client.FirstName;
editor.lastName = client.LastName;
editor.address = new Address();
ViewData["editor"] = editor;
return View(editor);
}
//
// POST: /Address/Create
[HttpPost]
public ActionResult Create(clientAddressEdit dto)
{
// dto is coming in with only the address and null values
// clientId, firstName and lastName
clientAddressEdit temp = new clientAddressEdit();
temp = dto;
if (ModelState.IsValid)
{
//add the address to the client
//fails here because clientId is null
var client = db.Clients.Find(temp.clientID);
client.Address = temp.address;
//save the address
db.Address.Add(temp.address);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(dto.address);
}
私のCreate View beginFormと何か関係があるのでしょうか??
@model VolumeV2.Models.DTOs.clientAddressEdit
@{
ViewBag.Title = "Create";
}
<h2>Create Address For @Html.DisplayTextFor(model => model.firstName) @Html.DisplayTextFor(model => model.lastName)</h2>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Address</legend>
<div class="editor-label">
@Html.LabelFor(model => model.address.StreetAddress)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.address.StreetAddress)
@Html.ValidationMessageFor(model => model.address.StreetAddress)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.address.PostalCode)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.address.PostalCode)
@Html.ValidationMessageFor(model => model.address.PostalCode)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.address.City)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.address.City)
@Html.ValidationMessageFor(model => model.address.City)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.address.Province)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.address.Province)
@Html.ValidationMessageFor(model => model.address.Province)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Address List", "Index")
@Html.ActionLink(Model.firstName+"'s Account","../Client/Details/"+ Model.clientID,"client")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}