views:

58

answers:

2

Hello :)

Using MVC 2 I have been trying to make this record store project. Creating records work but updating them doesn't. No exceptions are thrown either.

I examined getchangeset() right before submitchanges() it shows all zeros.

Thanks for your reading and you help :)

The Edit view:

<% using (Html.BeginForm("Edit", "Song")) { %>
    <fieldset>          
            <%: Html.HiddenFor(model => model.SongID) %>
            <%: Html.HiddenFor(model => model.DownloadCount) %>
            <%: Html.HiddenFor(model => model.Rating) %>
            <%: Html.HiddenFor(model => model.TagMapID) %>

        <div class="editor-label">
            <%= Html.LabelFor(model => model.AlbumID) %>
        </div>
        <div class="editor-field">
            <%= Html.DropDownListFor(model => model.AlbumID, DataHelper.getAlbumList(Model.AlbumID)) %>
            <%= Html.ValidationMessageFor(model => model.AlbumID) %>
        </div>

        <div class="editor-label">
            <%= Html.LabelFor(model => model.FullName) %>
        </div>
        <div class="editor-field">
            <%= Html.TextBoxFor(model => model.FullName)%>
            <%= Html.ValidationMessageFor(model => model.FullName) %>
and so on...

Edit function

public ActionResult Edit(int id)
    {
        try
        {
            var model = songRepository.Song.Single(rec => rec.SongID == id);

            return View(model);
        }
        catch (Exception anyEx)
        {
            throw anyEx;
            //return View("Error")
        }
    }

Edit post function

[AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Edit(Song model)
    {
        try
        {
            if (ModelState.IsValid)
            {
                songRepository.SaveSong(model);
                TempData["adminmsg"] = "Song saved";
                return RedirectToAction("List", "Song");
            }
            else
            {
                TempData["adminmsg"] = "Song not saved";
                return View("Edit", model);
            }
        }
        catch (Exception anyEx)
        {
            throw anyEx;
        }
    }

This is what saveSong() looks like in SqlSongRepository

public void SaveSong(Song song)
    {
        if (song.SongID == 0)
        {
            songTable.InsertOnSubmit(song);
        }
        else
        {
            songTable.Attach(song);
            songTable.Context.Refresh(RefreshMode.KeepChanges, song);
        }

        songTable.Context.SubmitChanges();
    }

[Context things]

This is the context repository

public class ContextRepository : IContextRepository
{
    private string connStr;

    public ContextRepository(string connectionString)
    {
        this.connStr = connectionString;
    }

    public DataContext MasterContext
    {
        get { return new DataContext(connectionString); }
    }

    public string connectionString
    {
        get { return connStr; }
    }
}

In the song controller for example

public SongController(IContextRepository contextRepository)
    {
        this.contextRepository = contextRepository;
        MasterContext = this.contextRepository.MasterContext;
        DataHelper.InitContext(contextRepository);

        songRepository = new SqlSongRepository(contextRepository.connectionString);
    }

Then the songRepository is used as I posted before.

A: 

why not try

public void SaveSong(Song song)
{
    if (song.SongID == 0)
    {
        context.songTable.InsertOnSubmit(song); //adds as song
    }
    else
    {
        var _song = (from s in context.songTable
                   where s.ID == song.id
                   select s).Single();
        _song = song; //updates existing song
    }

    context.songTable.SubmitChanges();
}

EDIT:

Here's an example of how I update my Users table from my Repository (sorry it's in VB)

Public Class UserRepository : Implements IUserRepository
    Private dc As MyDBDataContext
    Public Sub New()
        dc = New MyDBDataContext
    End Sub

   Public Sub AddUser(ByVal openid As OpenID) Implements IUserRepository.AddUser
        Dim user As New User
        user.MemberSince = DateTime.Now
        openid.User = user

        dc.OpenIDs.InsertOnSubmit(openid)
    End Sub

    Public Sub UpdateUser(ByVal user As User) Implements IUserRepository.UpdateUser
        Dim _user = (From u In dc.Users
            Where u.ID = user.ID
            Select u).Single

        With _user
            .About = user.About
            .BirthDate = user.BirthDate
            .Email = user.Email
            .isClosed = user.isClosed
            .isProfileComplete = user.isProfileComplete
            .RegionID = user.RegionID
            .Reputation = user.Reputation
            .UserName = user.UserName
            .WebSite = user.WebSite
        End With

    End Sub
End Class

Then in my controller I decide if I'm adding or updating.

rockinthesixstring
Changing the instance referred to by a local variable does not "update existing song". Underscores don't make magic happen either.
David B
it's basically the same thing, I did try updating from a local variable, to no avail
aredkid
@DavidB - I didn't "just add underscores" - I removed the "attach" and replaced it with a search on a record and updating it with the new data. This is how I run my repository layers and it works fantastic.
rockinthesixstring
updated my answer with the way I run my Repo.
rockinthesixstring
well it's redundant though, I am not going to type out all the properties of my entities... especially when I have so many.
aredkid
A: 

Well, solved the problem. Changing

songTable.Context.Refresh(RefreshMode.KeepChanges, song);

to

songTable.Context.Refresh(RefreshMode.KeepCurrentValues, song);

did the trick.

I don't know why, it would be great if someone could explain.

Thanks!

aredkid