0

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

4
  • What does this line of code do? _dc.AllCourseDetails.Where(w => w.IdACD == mvm.cd.IdACD); It appears to be a Linq statement, but the output isn't assigned to anything. Have a look here: stackoverflow.com/a/1427970 Commented Mar 9, 2012 at 16:51
  • @RobertHarvey I was trying that out there in case I could use it to make sure the submit knew which record it was making changes too, as I said I tried a number of things most of which I didn't expect to work but tried them anyway just in case. Commented Mar 9, 2012 at 16:56
  • Have a look at the question I linked. It illustrates the general pattern for what you are trying to do. As Jakub correctly points out, NerdDinner has code examples for this sort of thing. See here: asp.net/mvc/tutorials/nerddinner/… Commented Mar 9, 2012 at 16:57
  • @RobertHarvey not sure where or not I'm understanding it correctly but it seems I've already done what was needed and it hadn't worked hence this question, as I said right below the code I had at 1st only had "updatemodel(mvm.cd)" which is the table reference in my viewmodel for the database table I want updated and then below that I had _dc.SubmitChanges(); and nothing happened so was this what you were refering too? Commented Mar 13, 2012 at 16:14

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

As I stated in my question I knew half the code probabely was useless but I just included it to show everything I had tried as the initial attempt of just updatemodel(mvm.cd) and dc.submitchanges had failed so sure I'll check out nerddinner to see if shows anything different but is all I should have then - updatemodel and submitchanges?
0

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.

1 Comment

took forever to post this as the proxy connection I was on refused to allow this post to be submitted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.