Myzifer
Myzifer

Reputation: 1125

How to submit changes to database table in mvc using linq to sql

I've typically done this using edmx's but this time in order to use cascading drop downs in my project I had to switch to use linq to sql.

Anyway here is what I've tried

[HttpPost]
    public ActionResult Modules(ModuleViewModel mvm, FormCollection fc)
    {
        AllCourseDetail ACD = _dc.AllCourseDetails.Where(x => x.IdACD == mvm.cd.IdACD).FirstOrDefault();
        ACD = mvm.cd;
        if (ModelState.IsValid)
        {                
            UpdateModel(mvm);
            _dc.AllCourseDetails.Where(w => w.IdACD == mvm.cd.IdACD);
            UpdateModel(mvm.cd);
            _dc.SubmitChanges();
            Session.Add("redirectedEditcompletedsubmission", "yes");
            return RedirectToAction("List");
        }
        else
        {
            Session.Add("redirectedEditvalidation", "yes");
            return RedirectToAction("Index", "Home");
        }
    }

At 1st I didn't have anything above the if statement and inside I only had updatemodel and submit changes but no matter what combination I try it just doesn't save.

Also mvm.cd is the AllCourseDetail table which is referenced in the viewmodel as cd and I have to use a view model as

Upvotes: 0

Views: 1067

Answers (2)

Myzifer
Myzifer

Reputation: 1125

Seems I had got it right from the start with all that was needed was updatemodel and submitchanges but just that it had issues accepting the viewmodel object of the table being assigned.

[HttpPost]
    public ActionResult Modules(ModuleViewModel mvm, FormCollection fc)
    {
        AllCourseDetail ACD = _dc.AllCourseDetails.Where(x => x.IdACD == mvm.cd.IdACD).FirstOrDefault();
        if (ModelState.IsValid)
        { 
            UpdateModel(ACD, "cd");
            _dc.SubmitChanges();
            Session.Add("redirectedEditcompletedsubmission", "yes");
            return RedirectToAction("List");
        }
        else
        {
            Session.Add("redirectedEditvalidation", "yes");
            return RedirectToAction("Index", "Home");
        }
    }

I'd forgotten that when passing in viewmodel objects that it doesn't need the full path when used in the UpdateModel(ACD, "mvm.cd"); but instead after looking back at the last time I had done so in mvc using edmx's that it was just "cd".

Ofc directly assigning the values to the table from the model table also worked but just not with assigning the model tables data to the table record and then updating/saving.

Upvotes: 0

Jakub Konecki
Jakub Konecki

Reputation: 46008

The problem is that you are

loading entities from DB

AllCourseDetail ACD = _dc.AllCourseDetails.Where(x => x.IdACD == mvm.cd.IdACD).FirstOrDefault();

then totally discarding them

ACD = mvm.cd;

then updating your model with model

UpdateModel(mvm);

and then running the query and doing nothing with the results

_dc.AllCourseDetails.Where(w => w.IdACD == mvm.cd.IdACD);

Sorry, but your method is a total mess...

I would suggest you take a look at NerdDinner example in order to learn about MVC.

Upvotes: 2

Related Questions