0

好的,我正在尝试在我的视图中创建一个对象。该对象由带有客户 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 的 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);
    }

我的创建视图 可能与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")
}
4

1 回答 1

0

您的模型包含空值,因为您想要的值未显示在页面上。尝试将此添加到某处的视图中,以便将clientID其发回:

@Html.HiddenFor(model => model.clientID)

没有这个clientID,(客户端)模型就没有发送回服务器,因此它会导致null. address很好,因为你有EditorFor他们的。

于 2013-03-04T06:41:45.610 回答