Couple of n00b Questions

Last post 04-14-2008 8:28 AM by chrismcabz. 14 replies.

Sort Posts:

  • Hmm [^o)] Couple of n00b Questions

    04-10-2008, 9:36 AM
    • Loading...
    • chrismcabz
    • Joined on 03-26-2008, 4:23 PM
    • Scotland
    • Posts 10

    I'm just beginning ASP.NET, and getting very comfortable with the MVC stuff, thanks to my PHP/Rails MVC background. Excuse me if these are simple questions, but I've been banging my head against them all day, so would appreciate some guidance.:

     Question 1:

     I have an action which saves a form to the server, then should update the appropriate record with the server path to the file. I can upload the file OK; it saves fine. However, I cannot get the record to update. I get a "Specified cast is not valid" exception. In the code below I have removed the upload logic.

    public void AddFile(int id)
    {
    data.Candidate = db.Candidates.Single(x => x.ID == id);
    // File Upload Logic
    // SavedPath is a string representing the server file location

    data.Candidate.CV = SavedPath;
    //This bit doesn't work...
    db.SubmitChanges();
    }

      

    Question 2

    After the record updates, I would like to send the user back to the page they came from (not just render the view, which would be easy). As I'm pulling out the same data (candidate), surely this is easy to do and I'm just missing something obvious. At the moment I'm rendering a new view which has a response.redirect in the code-behind constructor. It works, but it's not very elegant... Any better suggestions?

     Thanks in advance for any help :-)

    Filed under: , , ,
  • Re: Couple of n00b Questions

    04-10-2008, 9:52 AM
    • Loading...
    • srulyt
    • Joined on 02-02-2008, 6:16 PM
    • Posts 209

     

    chrismcabz:

    Question 2

    After the record updates, I would like to send the user back to the page they came from (not just render the view, which would be easy). As I'm pulling out the same data (candidate), surely this is easy to do and I'm just missing something obvious. At the moment I'm rendering a new view which has a response.redirect in the code-behind constructor. It works, but it's not very elegant... Any better suggestions?

    you can call RedirectToAction() from your controller method

  • Re: Couple of n00b Questions

    04-10-2008, 9:56 AM
    • Loading...
    • paul.vencill
    • Joined on 02-01-2006, 12:57 PM
    • Gaithersburg, MD
    • Posts 681

    Need more info to debug #1.  Is your viewdata strongly typed to have a Candidate member?  You might set a breakpoint on the data.Candidate line and walk through the code to see what values and data types everything is as you go. 

  • Re: Couple of n00b Questions

    04-10-2008, 10:09 AM
    • Loading...
    • chrismcabz
    • Joined on 03-26-2008, 4:23 PM
    • Scotland
    • Posts 10

    Thanks for the replies.

    data.Candidate is setup like this (hope it helps):

        public class CandidatesControllerViewData
    {
    public Candidate Candidate { get; set; }
    public IEnumerable Candidates { get; set; }
    public IEnumerable Vacancies { get; set; }
    public IEnumerable Offers { get; set; }
    public Vacancy Vacancy { get; set; }
    public Offer Offer { get; set; }
    public User Creator { get; set; }
    public string Error { get; set; }
    }

    public class CandidatesController : Controller
    {
    public CandidatesControllerViewData data = new CandidatesControllerViewData();
    // Controller continues...
    }

      As for RedirectToAction() - How do I get it to redirect to (for example) /Controller/Action/ID, rather than just /Controller/Action/?

    Thanks guys!

  • Re: Couple of n00b Questions

    04-10-2008, 12:10 PM
    • Loading...
    • atp
    • Joined on 01-29-2007, 2:56 PM
    • Posts 4

    You can redirect like this: RedirectToAction(new System.Web.Routing.RouteValueDictionary(new { controller = "abc", action = "Add", id = 1234 }));

     

    Hope it helps

  • Re: Couple of n00b Questions

    04-10-2008, 12:37 PM
    • Loading...
    • paul.vencill
    • Joined on 02-01-2006, 12:57 PM
    • Gaithersburg, MD
    • Posts 681

    yeah, nothing there that jumps out as wrong.  I assume you only have one type called "Candidate" or that there's no namespace issues that might be causing your CandidatesControllerViewData to be a different 'Candidate' type than the one in the database, right?

    Did you try stepping through it like I suggested?

     

  • Re: Couple of n00b Questions

    04-10-2008, 2:57 PM
    • Loading...
    • chrismcabz
    • Joined on 03-26-2008, 4:23 PM
    • Scotland
    • Posts 10

    atp:

    You can redirect like this: RedirectToAction(new System.Web.Routing.RouteValueDictionary(new { controller = "abc", action = "Add", id = 1234 }));

    Hope it helps

     

     

    Thanks for that - exactly what I was looking for :-)

  • Re: Couple of n00b Questions

    04-10-2008, 3:08 PM
    • Loading...
    • chrismcabz
    • Joined on 03-26-2008, 4:23 PM
    • Scotland
    • Posts 10

    paul.vencill:

    yeah, nothing there that jumps out as wrong.  I assume you only have one type called "Candidate" or that there's no namespace issues that might be causing your CandidatesControllerViewData to be a different 'Candidate' type than the one in the database, right?

    Did you try stepping through it like I suggested?

     

     I didn't manage a step-through while at the office; I'll try it tomorrow morning - I'll let you know the results.  There are no other types called Candidate either. Strange stuff...

  • Re: Couple of n00b Questions

    04-11-2008, 4:30 AM
    • Loading...
    • chrismcabz
    • Joined on 03-26-2008, 4:23 PM
    • Scotland
    • Posts 10

     OK, stepping through shows everything as I'd expect it.

    • When we setup data.Candidate, CV is null;
    • At db.SubmitChanges(), CV is set to the value of SavedPath.

    The image below was taken while debugging:

    Step-Through Output 

  • Re: Couple of n00b Questions

    04-11-2008, 5:45 AM
    • Loading...
    • chrismcabz
    • Joined on 03-26-2008, 4:23 PM
    • Scotland
    • Posts 10

     edited - ignore.

  • Re: Couple of n00b Questions

    04-11-2008, 9:57 AM
    • Loading...
    • paul.vencill
    • Joined on 02-01-2006, 12:57 PM
    • Gaithersburg, MD
    • Posts 681

     wow, I'm at a loss.  Have you tried something simple like using .Single<Candidate>(...) or using the explicit .Cast<> operator?  I know it shouldn't be that, but I'm thinking they might give you something more specific error-wise...  I'm also vaguely wondering if using the .Attach method on the datacontext wouldn't be appropriate, if you're fetching and storing in two separate calls(?). 

  • Re: Couple of n00b Questions

    04-11-2008, 12:51 PM
    • Loading...
    • Dimebrain
    • Joined on 07-13-2007, 11:41 PM
    • Posts 72

    I think might be an issue with your LINQ entity, not MVC. What does your .dbml file look like? Are you using non-PK relationships?

    Some day I'll be able to answer more questions than I ask.
  • Re: Couple of n00b Questions

    04-14-2008, 5:27 AM
    • Loading...
    • chrismcabz
    • Joined on 03-26-2008, 4:23 PM
    • Scotland
    • Posts 10

    Dimebrain:

    I think might be an issue with your LINQ entity, not MVC. What does your .dbml file look like? Are you using non-PK relationships?

     

     

    Yes I am - each Candidate has a "Vacancy_Reference" FK. Each Reference is unique within the Vacancies table, but each Vacancy might have many Candidates

    Interestingly enough, I just found out LINQ isn't firing the SQL Server 2005 Trigger which generates this reference when a new Vacancy is added (so that's a new, semi-related issue).

    DBML looks like this:

    DBML file 

  • Re: Couple of n00b Questions

    04-14-2008, 6:41 AM
    Answer
    • Loading...
    • Dimebrain
    • Joined on 07-13-2007, 11:41 PM
    • Posts 72

    Okay,

    As for the trigger, if you haven't already, make sure for any database-generated property in your map that you've set AutoSync to OnInsert and Auto-Generated to true, this lets LINQ know that you're interested in populating values for those properties through the database, which is not set explicitly even if SQL knows to do that behind the scenes.

    The reason I asked about non-PK relationships is that LINQ to SQL has known issues handling them and the end result is often an Invalid Cast Exception. You might want to read this forum thread: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1738489&SiteID=1, and see if you can eliminate the cast exception using a more linear data arrangement or by following advice that's located there.

    Some day I'll be able to answer more questions than I ask.
  • Re: Couple of n00b Questions

    04-14-2008, 8:28 AM
    • Loading...
    • chrismcabz
    • Joined on 03-26-2008, 4:23 PM
    • Scotland
    • Posts 10

    Dimebrain:

    Okay,

    As for the trigger, if you haven't already, make sure for any database-generated property in your map that you've set AutoSync to OnInsert and Auto-Generated to true, this lets LINQ know that you're interested in populating values for those properties through the database, which is not set explicitly even if SQL knows to do that behind the scenes.

    I've set these as you suggested, but the trigger still won't fire. I've checked inserting via other methods (such as the management console), and it works there. I'm going to investigate other ways of building the reference, rather than spend too long on this.

    Dimebrain:

    The reason I asked about non-PK relationships is that LINQ to SQL has known issues handling them and the end result is often an Invalid Cast Exception. You might want to read this forum thread: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1738489&SiteID=1, and see if you can eliminate the cast exception using a more linear data arrangement or by following advice that's located there.

     

    Thank you thank you thank you! I got the DB guys to change the relationship to a straight PK relationship, redid my LINQ model and it works great!

Page 1 of 1 (15 items)