ASP.Net MVC View Question

Associate
Joined
25 Feb 2007
Posts
905
Location
Midlands
Hi all,

I'm just learning ASP.Net MVC, and have followed a getting started guide on the www.asp.net site.

I'm up to this point and have a question.

In the example linked above, how would I add a Create action for the Enrolments? I want to be able to add (and probably edit/delete etc.) enrolments from the Student details view.

Thanks,
Matt
 
Associate
OP
Joined
25 Feb 2007
Posts
905
Location
Midlands
Hi,

Thanks for your reply - I already have what you've described. I am able to Create/Edit/Delete both Students and Enrolments.

However, in the tutorial I linked to, the Student Detail view had been modified to show all Enrolments related to that Student. I want to be able to add additional enrolments for the student from the Student Details view.

Thanks,
Matt
 
Associate
OP
Joined
25 Feb 2007
Posts
905
Location
Midlands

This looks to be what I need - I've tried to implement it, but I'm just getting the Create form for the Student, rather than an Enrolment...

Where have I gone wrong? Is this because I'm not passing the correct data to the Controller, or have I specified the wrong Controller?
 
Associate
OP
Joined
25 Feb 2007
Posts
905
Location
Midlands
This looks to be what I need - I've tried to implement it, but I'm just getting the Create form for the Student, rather than an Enrolment...

Where have I gone wrong? Is this because I'm not passing the correct data to the Controller, or have I specified the wrong Controller?

Resolved!

I needed to use @Html.ActionLink("Add Enrolment", "Create", "Enrolment", "", new { id = Model.ID })

Notice the extra double quotes after "Enrolment" - I was passing in the name of the Controller as the routeValues parameter.

That's sorted it though, thanks for the help!
 
Associate
OP
Joined
25 Feb 2007
Posts
905
Location
Midlands
Ah yes it looks like I was too eager with my post!

I've changed it now, and it seems to be working correctly, however the Student ID on the Create view doesn't change accordingly if that makes sense? I.e. the Student ID drop down doesn't change with the Student ID i've passed in from the Student View
 
Associate
OP
Joined
25 Feb 2007
Posts
905
Location
Midlands
All of the Controllers and Views have been created using a Scaffold item, so I imagine that this functionality isn't implemented yet.

Looking at the Controller, the ViewBag is used to populate the SelectList. At the moment it's just selecting all possible students - how do I pass in the ID to the selectedValue parameter?
 
Associate
OP
Joined
25 Feb 2007
Posts
905
Location
Midlands
Think I've sorted it - I amended the Create method to accept a parameter with the same name as the Parameter from the URL (not sure if this matters?) and then passed this in to the selectedValue parameter of the SelectList method.

Thanks for your help!
 
Associate
OP
Joined
25 Feb 2007
Posts
905
Location
Midlands
Final question I hope! I've amended the Enrolment so that each has a cost associated to it.

I want to display the total cost next to each student - I've added this to the Student Index view:
Code:
@Html.DisplayFor(modelItem => item.Enrolments.Sum(e => e.EnrolmentCost))

This gives me the following error:

Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.

Any hints? :)
 
Associate
OP
Joined
25 Feb 2007
Posts
905
Location
Midlands
Thought I'd add my next question into here rather than open a new thread!

In the example above, when adding a new Enrolment, I want to be able to choose a Year and then only be able to select courses valid for that year - will I have to do this in JS or can it be done in the Controller?
 
Back
Top Bottom