locked
ADO.NET ENTITY MVC - Update table in database using Entity.State Modified if i have FOREIGN KEY to another object in database RRS feed

  • Question

  • User-1278384752 posted

    Hi, I have problem with update object in database. 

    I don't have errors on screen but in database my object wasn't updated. 

    I think that's not working because my Supplier have foreign key from Adress table.

    Does somebody know how to update using Entity state modified if a have a foreign key to another table?

    I appreciate any help! 

    This is my classes in Model from database:

    public partial class Supplier
    {
    public int SupplierID{ get; set; }
    public string Name{ get; set; }
    public string Phone{ get; set; }
    public string Email { get; set; }
    public Nullable<int> TownID{ get; set; }
    public Nullable<int> StreetID{ get; set; }
    public Nullable<int> AdressNumber{ get; set; }

    public virtual Adress Adress { get; set; }

    }

    public partial class Town
    {
    public int TownID{ get; set; }
    public string Name{ get; set; }
    }

    public partial class Street
    {
    public int TownID{ get; set; }
    public int StreetID{ get; set; }
    public string Name{ get; set; }
    }

    public partial class Adress
    {
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Adress()
    {
    this.Supplier= new HashSet<Supplier>();
    }

    public int TownID{ get; set; }
    public int StreetID{ get; set; }
    public int AdressNumber{ get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Supplier> Suppliercs{ get; set; }
    }

    This is my View:

    @model FpisNada.Models.Supplier
    @{
    ViewBag.Title = "Index";

    Layout = null;
    }

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

    @Html.TextBoxFor(model => model.SupplierID, new { @placeholder = "pib dobavljaca", style = " float:left" })

    <div class="col-md-9">
    @if (ViewBag.ListTown!= null)
    {
    @Html.DropDownListFor(m => m.TownID, ViewBag.ListTown as SelectList, "--select town--", new { @class = "form-control", style = " float:left" })

    }

    @Html.DropDownListFor(m => m.StreetID, new SelectList(""), "--select street--", new { @class = "form-control", style = " float:left" })


    <div class="container">


    @Html.TextBoxFor(model => model.AdressNumber, new { @class = "form-control"})
    @Html.TextBoxFor(model => model.Email, new { @class = "form-control" })
    @Html.TextBoxFor(model => model.Name, new { @class = "form-control" })
    @Html.TextBoxFor(model => model.Phone, new { @class = "form-control"})
    </div>
    </div>
    <input type="submit" value="Edit" />
    }

    My controller method:

    [HttpGet]
    public ActionResult Edit(int id)
    {

    Supplier supplier= db.Supplier.Find(id);

    return View(supplier);
    }
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit( Supplier supplier)
    {

    try
    {
    if (ModelState.IsValid)
    {

    db.Entry(supplier).State = EntityState.Modified;

    db.SaveChanges();
    return RedirectToAction("ChangeSupplier");
    }
    }
    catch (DataException /* dex */)
    {
    //Log the error (uncomment dex variable name after DataException and add a line here to write a log.)
    ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
    }
    return View(supplier);
    }

    Sunday, March 26, 2017 1:17 PM

All replies