Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7657299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:59:41+00:00 2026-05-31T12:59:41+00:00

I have a problem with editing data in the controller. My two models look

  • 0

I have a problem with editing data in the controller. My two models look like this:

[Table("Zielgruppen")]
public class Zielgruppe
{
    public int Id { get; set; }
    public string Zielgruppenname { get; set; }
    public Bezug Bezug { get; set; }
}

and

public class Bezug
{
    public int Id { get; set; }
    public string Bezugsname { get; set; }
}

The functions in my controller are:

public ActionResult Edit(int id)
{
    Zielgruppe zielgruppe = _db.Zielgruppe.Include("Bezug").Single(z => z.Id == id);
    ViewBag.BezugsId = new SelectList(_db.Bezug, "Id", "Bezugsname", zielgruppe.Bezug.Id);
    return View(zielgruppe);
}

[HttpPost]
public ActionResult Edit(Zielgruppe aktualisierteZielgruppe)
{
    if(ModelState.IsValid)
    {
        aktualisierteZielgruppe.Bezug = _db.Bezug.Find(aktualisierteZielgruppe.Bezug.Id);
        _db.Entry(aktualisierteZielgruppe).State = EntityState.Modified;
        _db.SaveChanges();
        return RedirectToAction("Index");
    }
    ViewBag.BezugsId = new SelectList(_db.Bezug, "Id", "Bezugsname", aktualisierteZielgruppe.Bezug.Id);
    return View();            
}

My Problem is that if i change aktualisierteZielgruppe.Bezug the changes won’t get save in the database.

and this is my edit.cshtml:

@model Medien_Archiv.Models.Zielgruppe

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Zielgruppe</legend>

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

        <div class="editor-label">
            @Html.LabelFor(model => model.Zielgruppenname)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Zielgruppenname)
            @Html.ValidationMessageFor(model => model.Zielgruppenname)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Bezug)
        </div>
        <div class="editor-field">
            @Html.DropDownListFor(model => model.Bezug.Id, ViewBag.BezugsId as SelectList)   
        </div>
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:59:43+00:00Added an answer on May 31, 2026 at 12:59 pm

    This is one of the cases where entity framework falters – tracking updated associations.

    I’m assuming that you’re not doing any fancy model binding here (ie, model binder pulling from the database).

    if(ModelState.IsValid)
    {
        aktualisierteZielgruppe.Bezug = _db.Bezug.Find(aktualisierteZielgruppe.Bezug.Id);
    
        // The problem is here.  EF doesn't mark associations as modified
        // the same way it tracks scalar types.
        _db.Entry(aktualisierteZielgruppe).State = EntityState.Modified;
        _db.SaveChanges();
        return RedirectToAction("Index");
    }
    

    Here’s the more conventional way:

    [HttpPost]
    public ActionResult Edit(int id, FormCollection collection)
    {
        // Get the original record to edit from the database.
        var gruppe = _db.Zielgruppe.Include("Bezug").Single(z => z.Id == id);
    
        // This will attempt to do the model binding and map all the submitted 
        // properties to the attached entity.
        if (TryUpdateModel(grupppe))
        {
            _db.SaveChanges();
            return RedirectToAction("Index");
        }
    }
    

    Also note that this method of binding to entities leaves you open to over-posting attacks. You can mitigate this attack vector by white-listing model properties during model binding, or bind against a view model and then map.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem with cakephp's Session->write method. If I set a value like $_SESSION['..']
i have problem to correctly bind data to WPF Chart. When i'm setting ItemsSource
I've been modifying/editing parts of the Android platform, but have run into a problem
I have a modal view controller that creates core data changes in it's own
The problem is quite basic. I have a JTable showing cached data from a
I have a problem with saving data from Ext.data.DirectStore to server Here is my
I have the following problem: I would like to visualize a discrete and a
I have a script that performs some simple editing which works fine, the problem
I need some help on this problem. It is about ASP.NET MVC3. I have
I have a detail controller set up for editing tableviewcells. When I finish editing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.