好的,我正在尝试在我的视图中创建一个对象。该对象包含一个完整的地址,其中包含客户端ID以及名字和姓氏。当页面首次加载时,客户端信息正常,但是当我发布数据(点击提交)时,客户端数据消失并变为空,从而导致网页崩溃。
这是我的课程
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创建方法
//
// 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);
}
我的创建视图 是否可能与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")
}
答案 0 :(得分:0)
您的模型包含空值,因为您想要的值未在页面上显示。尝试将其添加到某个位置的视图中,以便回发clientID
:
@Html.HiddenFor(model => model.clientID)
如果没有这个,(客户端)模型上没有clientID
发送回服务器,因此导致null
。 address
很好,因为你有EditorFor
个。