ASP.Net MVC View Question

Associate
Joined
25 Feb 2007
Posts
903
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
Joined
28 Nov 2004
Posts
1,232
Location
Birmingham
In very simple terms, this is what you'd need:

In your controller, you would need an Action for "Create", "Edit" and "Delete".
Each action wouild have a corresponding view of the same name.

Create and Edit views would be forms matching your Enrolement model setup.

The Create view you would pass in a new version of your Enrolement model.
The form would submit to a [HttpPost] attributed version of the Create Action in the controller where you would handle the validation and entry to the database.

The Edit action you would pass an id in the querystring, get the relevant "Enrolement" entry from the database into a model and pass that the to view.
The form would submit to a [HttpPost] attributed version of the Edit Action in the controller where you would handle the validation and entry to the database.

The Delete action would again accept an id which I would then use to load the item from the database (to confirm it actually exists) then in the view I'd display a confirmation message for the user (e.g. "Are you sure you want to delete 'x'?")
When this view is submitted it will again pass the ID to the "[HttpPost]" version of the page which will delete the entry from the database.
 
Associate
OP
Joined
25 Feb 2007
Posts
903
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
 
Soldato
Joined
28 Oct 2006
Posts
12,456
Location
Sufferlandria
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.

You need a link in the Student Detail view to the "create" action on the Enrolements controller. You also need to supply the ID of the student so that it knows which student to add the enrolment to.
The link would look something like this:

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

Add Enrolment = The link text
Create = the action
Enrolment = the controller
new { id=student.ID } = the parameters to pass to the action. I have just guessed at the names, you'll need to check that "id" is the correct parameter that the Create action is expecting.
 
Last edited:
Associate
OP
Joined
25 Feb 2007
Posts
903
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
903
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!
 
Soldato
Joined
28 Oct 2006
Posts
12,456
Location
Sufferlandria
Oh sorry, my code is wrong.

The method which accepts a controller argument (to allow you to link to an anction in a different controller) and parameters is in the form:

@html.Actionlink(linktext, actionName, controllerName, routeValues, htmlAttributes)

so you need to include the htmlAttributes to make it work (even if this is null).
So try this:

@Html.ActionLink("Add Enrolment", "Create", "Enrolment", new { id = Model.ID }, null)
 
Soldato
Joined
28 Oct 2006
Posts
12,456
Location
Sufferlandria
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!

I think the htmlAttributes and routeValues are the wrong way round here.
Add a breakpoint in the Create action of Enrolment and check that it gets the correct id passed to it.

The htmlAttributes are for the html of the link on your page. So you can add a class or something for styling the link.
The routeValues are the parameters that are passed when you click the link.
 
Associate
OP
Joined
25 Feb 2007
Posts
903
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
 
Soldato
Joined
28 Oct 2006
Posts
12,456
Location
Sufferlandria
How does the select list get populated?

You would either set the selected item in the "enrolment/create" controller before it gets passed to the view or you would pass the id to the view using ViewBag and set the selected item there.
 
Associate
OP
Joined
25 Feb 2007
Posts
903
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
903
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
903
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
903
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?
 
Soldato
Joined
28 Oct 2006
Posts
12,456
Location
Sufferlandria
JS would be best. You need to send all of the courses to the view from the controller because you dont know which year the user will select until after the view has opened (and the user has actually selected a year).

You could do it by submitting a form when the user selects a year and passing that to a controller then filtering the courses with that year and returning to the view, but that makes a lot of page loads and will slow things down for the user.

If it's quite simple and a course can only be selected in one year (course names might be something like "Mathematics - 2013 intake", "Mathematics - 2014 intake") then it's easy enough to do in js.
If it's more complex and you need to access the database to check which courses are available for each year, then you might have to do it in a controller. In this case, you'd be best to use asynchronus calls and return partial views so that you dont have to reload the whole page each time the year is selected. That's probably complicating things too much at this stage though. Get it working with normal page submissions and loads then worry about performance once you've got it working.
 
Last edited:
Back
Top Bottom