Question

I try to get a value in my controller, it's set in the view (in a hidden field) but once in the post method the field is null. My model :

 public partial class Customer
{
    public int CustomerId { get; set; }
    public string CompanyName { get; set; }


    public int? CustomerAddressId { get; set; }
    [ForeignKey("CustomerAddressId")]
    public virtual CustomerAddress CustomerAddress { get; set; }

    public int? CustomerAddressBillingId { get; set; }
    [ForeignKey("CustomerAddressBillingId")]
    public virtual CustomerAddressBilling CustomerAddressBilling { get; set; }

    public int? CustomerBankId { get; set; }
    [ForeignKey("CustomerBankId")]
    public virtual CustomerBank CustomerBank { get; set; }

    public virtual ICollection<CustomerLink> CustomerLink { get; set; }
}

My view :

@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <hr />
    @Html.ValidationSummary(true)
    @Html.HiddenFor(model => model.CustomerId)
    @Html.HiddenFor(model => model.CustomerAddressId)
    @Html.HiddenFor(model => model.CustomerAddressBillingId)
    @Html.HiddenFor(model => model.CustomerBankId)

    <div class="form-group">
        @Html.LabelFor(model => model.CompanyName, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.TextBoxFor(model => model.CompanyName, new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.CompanyName)
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            @Html.ActionLink("Cancel", "Index", null, new { @class = "btn btn-default" })
            @Html.ActionLink("Address", "EditAddress", new { id = Model.CustomerId }, new { @class = "btn btn-default" })
            @Html.ActionLink("Billing Address", "EditAddressBilling", new { id = Model.CustomerId }, new { @class = "btn btn-default" })
            @Html.ActionLink("Bank Details", "EditBank", new { id = Model.CustomerId }, new { @class = "btn btn-default" })
            @Html.ActionLink("Links", "EditLink", new { id = Model.CustomerId }, new { @class = "btn btn-default" })
            <input type="submit" value="Save" class="btn btn-success" />
        </div>
    </div>
</div>
}

And my controller method :

 // GET: /Customer/Edit/5
    public ActionResult Edit(int id)
    {
        Customer customer = _db.GetCustomer(id);
        if (customer == null)
        {
            return HttpNotFound();
        }

        return View(customer);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(Customer customer)
    {
        if (ModelState.IsValid)
        {
            _db.Edit(customer);
            _db.Save();
            return RedirectToAction("Index");
        }
        return View(customer);
    }

What's really weird is that the CustomerId value is not null in the controller, but i do exactly the same thing with CustomerAddressId, and this one is null. Any suggestion?

Was it helpful?

Solution

Finally i found out, my properties was not properly binded in my model ^^

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top