0

I want to use a view model as input to an Edit form. Call it -

public class StudentEditViewModel
{
}

My Edit action method creates the object and passes it to the Edit view, which has @model set to StudentEditViewModel. This works fine - all data is displayed as expected.

BUT, when I post the changes (using ) things fall apart. Here is the skeleton of my action handler:

[HttpPost]
public ActionResult Edit(StudentEditViewModel student)
{
}

The problem is that "student" is null. All the online examples appear to do it this way, but I'm obviously missing something. Can anyone help me out?

I'll be happy to provide more details if necessary. Thanks in advance.

3
  • Can you please include the view that is calling the Edit action. Commented Apr 18, 2014 at 22:47
  • Does your ViewModel have complex items? Because if this is the case, you need to add contructor code to initialize those complex properties. Commented Apr 18, 2014 at 22:58
  • Without view code, it's impossible to tell what's wrong. Commented Apr 19, 2014 at 4:21

3 Answers 3

2

Your StudentEditViewModel needs properties (E.g public string name { get; set;}) because the StudentEditViewModel should have a property basis for it to have a value and in your view, use the basic LINQ syntax

using(Html.BeginForm())
{
    @html.TextBoxFor(u => u.name)
    <input type="submit"/>
}

Try adding also with a non-Data annotation ActionResult and check out the breakpoints. This was my mistake before when I tried to program. Hope I can help you.

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

Comments

1

Are you using Explicitly typed View Model? In this case you can do it by jquery as follows:

$("#btnSave").click(function (event) {
        event.preventDefault();

        if ($("#form1").valid()) {

            var formInput = $('#form1').serializeObject();

            $.ajax({
                type: 'POST',
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                url: 'your controller action url be here',
                data: JSON.stringify(formInput)
            })
            .done(function (data, textStatus, jqXHR) {

                showMsg('page-message', 'success', 'Success!', 'The item was saved.');
                $('#PriorityDateId').val(data);
                window.location.href = 'after save where you want to redirect(give url)';
                })
          .fail(function (jqXHR, textStatus, errorThrown) {

              showResponse('page-message', 'error', 'Error!', jqXHR.responseText);
          });
            }
    });

If need more info, let me know..

Comments

1

Your model should contain some properties like so:

model:

public class StudentEditViewModel
{
//Sample Properties
public string Name{get;set;}
public int Age {get;set;}
}

And your view should look something like this.

@model Nameofyourproject.Model.StudentEditViewModel

@using(Html.BeginForm())
{
@Html.LabelFor(m => m.Name)    
@Html.TextBoxFor(m => m.Name) // this where you will enter a value for the Name property

@Html.LabelFor(m => m.Age)    
@Html.TextBoxFor(m => m.Age) // this where you will enter a value for the Age property
    <input type="submit"/> //this will submit the values to your action method.
}

you will be able to get this values now in your action method

[HttpPost]
public ActionResult Edit(StudentEditViewModel student)
{
var name = student.Name;
var age = student.Age;

}

Comments

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.