Rob Rob - 3 months ago 5x
Ajax Question

Submitting Ajax form but nothing sent to controller?

I am using the following viewModel on my view:

public class CourseViewModel
public IEnumerable<Course_page> Course_page { get; set; }
public IEnumerable<course_section> Course_section { get; set; }
public IEnumerable<course_subsection> Course_subsection { get; set; }
public IEnumerable<Enrollment> Enrollments { get; set; }

In my view I have the following ajax form:

@using (Ajax.BeginForm("updateprogress", "User", new { EnrollmentID = Model.Enrollments.Single().EnrollmentID }, new AjaxOptions
HttpMethod = "POST",

}, new { id = "ajaxForm" }))
@Html.HiddenFor(Model => Model.Enrollments.Single().progress1, new { id = "scenecheck" })

I have an animation generated by a program called Hype, within the animation are scenes and the current scene number is called using a javascript function with the value populating the form above in the 'progress1' field:

function updateprogress(hypeDocument, element, event) {

var scenenumber = hypeDocument.currentSceneName();

hypeDocument.getElementById('scenecheck').value = scenenumber;



I have some other javascript which listens for the scene change and calls the function above whenever it does change.

When I change HiddenFor to TexboxFor on the page I can see that everything is working correctly within the view, the scene number is present in the textbox.

My controller action when this form submits is as follows:

public ActionResult updateprogress(int EnrollmentID, CourseViewModel model)
Enrollment enrollment = db.enrollment.Find(EnrollmentID);
Mapper.Map(model, enrollment);

return null;


I'm using Automapper to map the change to the viewmodel back to the actual model, only a single field is to change in the viewmodel. This might not be the ideal way to do it, I'm still trying to work out how I should approach this.

My two issues are:

1 - The form does not appear to pass anything back to the controller when it submits. So the 'model' object is empty, null. This leads me to believe that what I thought I understood about what I was doing was wrong. Why is the updated field not being passed back to the controller?

2 - Is this the best method for updating the value of a single field in the viewmodel?



You have a very complex model, the internal model binder is not going to know what to do with it because you have an IEnumerable of an object and you're editing(or hiding in this case) progress1 of the first Enrollment. You'll have to create a custom model binder.

What's happening on the view is it's storing this input tag of progress1 but when the controller gets it, it's not going to have any idea which Enrollment object it belongs to.

Try just passing in the first Enrollment to your view for editing.